简体   繁体   中英

Optimizing Select,delete,update query performance in SQL

i've got a question about which query is better with higher performance in sql

SELECT

1st comparison:

Declare @Variable VARCHAR(MAX)<BR>
SELECT top 1 @VARIABLE = COLUMN2 FROM TABLE WHERE COLUMN1 like 'Any value'


2nd comparison:

Declare @Variable varchar(MAX)
select top 1 @variable = t.column2 from table t where t.column1 like 'any value'

update

1st comparison:
UPDATE T set column2 = 'any value' from table T where column1 = 'any value'
2nd comparison:

UPDATE TABLE SET COLUMN2 = 'any value' where column 1 = 'any value'

delete

1st comparison:

delete t
from table t
where column1 = 'any value'


2nd comparison

delete from table where column1 = 'any value'

I just need your opinion on which query is better, and if there is a better way to optimize my queries performance, can someone tell me how is it?

The comparison doesn't matter for performance.

Almost all the time sql performance is about lookup . In other words , how fast data can be retrieved by sql database. In other times, it's about how fast you can insert or remove .

So, how do you make sql server perform fast?
By making it's look up perform faster and you do that by providing with an index, similar to the one in any book, indexing can help you quickly find chapters.

So, in your query, if you create an index for column1 than the sql database can quickly seek the value from the index and make your select , update and delete query run faster.

Having an alias just makes your code readable. It has nothing to do with performance.

Queries under comparison you posted differ only in syntax of writing them. There won't be any different in performance for both the queries. Syntax parser will generate same syntax tree for these queries.

You can think of them as

int sum = x + y;

can also be written as

int sum = y + x;

syntax tree for them would be

   sum
  -   -
x       y

Difference in performance can be observed only by indexing, statistics collected for tables.

Some of the highlights about indexes

  1. indexes has nothing to do with no of records present in a table instead better parameter to classify index performance is size of a row.

  2. (SizeOfKey attribute x noofrecords) in a table is better parameter to decide whether to use index or not

eg

no of records 5000, key column is Age with data type as byte i.e. 1 byte
total size = 5000 bytes i.e. not even 1 page - Indexes won't be useful.

no of records 5000, key column is money i.e. 8 bytes
total size = 5000 * 8bytes = 40000 bytes = 40000/8Kb = 5 pages (1 page = 8 kb)
Index won't be very helpful

no of records 50000, key column is byte i.e. 
50000 bytes = 7 pages, index won't be very helpful

no of records 50000, key column is bigint i.e.
(50000*8bytes)/8Kb = 50 pages, index can be help up to some extent.

no of records 500000, key column is bigint i.e.
(500000*8bytes)/8Kb = 500 pages, index is most likely to be helpful depending upon your search arguments.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM