简体   繁体   English

在SQL中优化选择,删除,更新查询性能

[英]Optimizing Select,delete,update query performance in SQL

i've got a question about which query is better with higher performance in sql 我有一个问题,关于哪个查询在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 . 几乎所有时候sql performance都与查找有关。 In other words , how fast data can be retrieved by sql database. 换句话说,sql数据库可以多快地检索数据。 In other times, it's about how fast you can insert or remove . 在其他时间,它是关于您可以多快插入删除

So, how do you make sql server perform fast? 那么,如何使sql server快速执行?
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. 因此,在查询中,如果为column1创建index ,则sql数据库可以快速从索引中查找值,并使selectupdatedelete查询运行得更快。

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 表中的(SizeOfKey属性x noofrecords)是决定是否使用索引的更好参数

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.

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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