Summary of some common performance problems in SQL Server

Source: Internet
Author: User

1. To optimize the query, try to avoid full table scanning. First, consider creating an index on the columns involved in where and order.

2. Avoid using left join and null values as much as possible. Left join consumes more resources than inner join because it contains data that matches null (non-existent) data. Therefore, you can rewrite the query so that the query does not use any inner join, the returned result is returned.
For example, there are two tables:
Product (product_id int not null, product_type_id int null ,...), in the product table, product_id is an integer greater than 0. product_type_id is associated with the product_type table, but can be empty because some products have no category.
Product_type (product_type_id not null, product_type_name null,...), product category table
If you want to associate two tables and then query the product content, you will immediately think of using inner join. However, there is a way to avoid using inner join:
Add a record in product_type: 0 ,'',..., set product_type_id of product to not null. If the product does not have a category, set product_type_id to 0, so that you can use inner join for the query.

3. Try to avoid using it in the WHERE clause! = Or <> operator. Otherwise, the engine may discard the index for full table scanning.

4. avoid using or to join conditions in the WHERE clause as much as possible. Otherwise, the engine may discard the index and perform a full table scan. For example, if a table t, key1, or key2 has an index, the following stored procedure is required:
Create procedure select_proc1 @ key1 Int = 0, @ key2 Int = 0
As
Begin
Select key3 from t
Where (@ key1 = 0 or key1 = @ key1)
And (@ key2 = 0 or key2 = @ key2)
End
Go
This stored procedure may cause a full table scan and can be modified as follows:
Create procedure select_proc2 @ key1 Int = 0, @ key2 Int = 0
As
Begin
If @ key1 <> 0 and @ key2 <> 0
Select key3 from t
Where key1 = @ key1 and key2 = @ key2
Else
If @ key1 <> 0
Select key3 from t where key1 = @ key1
Else
Select key3 from t where key2 = @ key2
End
Go
After the change, the code is added, but the efficiency is improved.

5. Use in and not in with caution, for example:
Select ID from t where num in (1, 2, 3)
For continuous values, you can use between instead of in:
Select ID from t where num between 1 and 3

6. The following query will also cause a full table scan:
Select ID from t where name like '% ABC %'
To improve efficiency, you can consider full-text search.

7. If a parameter is used in the WHERE clause, a full table scan is performed. Because SQL parses local variables only at runtime, the optimizer cannot postpone the selection of the access plan to runtime; it must be selected at compilation. However, if an access plan is created during compilation, the value of the variable is still unknown and thus cannot be used as an input for index selection. The following statement performs a full table scan:
Select ID from t where num = @ num
You can change it to force query to use the index:
Select ID from T with (index name) where num = @ num

8. Avoid performing expression operations on fields in the WHERE clause as much as possible. This will cause the engine to discard the use of indexes for full table scanning. For example:
Select ID from t where num/2 = 100
Should be changed:
Select ID from t where num = 100*2

9. Avoid performing function operations on fields in the WHERE clause as much as possible, which will cause the engine to stop using the index for full table scanning. For example:
Select ID from t where substring (name, 1, 3) = 'abc' -- id whose name starts with ABC
Select ID from t where datediff (day, createdate, '2017-11-30 ') = 0 -- '2017-11-30' generated ID
Should be changed:
Select ID from t where name like 'abc %'
Select ID from t where createdate> = '2014-11-30 'and createdate <'2014-12-1'

10. do not perform functions, arithmetic operations, or other expression operations on the left side of "=" in the WHERE clause. Otherwise, the system may not be able to correctly use the index.

11. when using an index field as a condition, if the index is a composite index, you must use the first field in the index as the condition to ensure that the system uses the index, otherwise, the index will not be used, and the field order should be consistent with the index order as much as possible.

12. Do not write meaningless queries. If you need to generate an empty table structure:
Select col1, col2 into # T from t where 1 = 0
This type of code will not return any result set, but will consume system resources, should be changed to this:
Create Table # T (...)

13. In many cases, replacing in with exists is a good choice:
Select num from a where num in (select num from B)
Replace the following statement:
Select num from a where exists (select 1 from B where num = A. Num)

14. not all indexes are valid for queries. SQL queries are optimized based on the data in the table. When there is a large number of duplicate data in the index column, SQL queries may not use indexes, for example, if a table contains sex fields, male and female are almost half of each other, indexing sex does not play a role in query efficiency.

15. the more indexes, the better. Although the index can improve the efficiency of the SELECT statement, it also reduces the efficiency of insert, update, and delete, because the index may be rebuilt during insert or update operations, therefore, you need to carefully consider how to create an index, depending on the actual situation. It is recommended that the number of indexes in a table be no more than 6. If there are too many indexes, consider whether the indexes on some columns that are not frequently used are necessary.

16. update the clustered index data column should be avoided as much as possible, because the order of the clustered index data column is the physical storage order of the table records. Once the column value changes, the order of the entire table record will be adjusted, it will consume a considerable amount of resources. If the application system needs to frequently update the clustered index data column, consider whether to create the index as a clustered index.

17. use numeric fields whenever possible. If fields containing only numerical information are not designed as numeric fields, this will reduce query and connection performance and increase storage overhead. This is because the engine compares each character in the string one by one during query and connection processing, and only one comparison is required for the number type.

18. use varchar/nvarchar to replace Char/nchar as much as possible, because the storage space of the variable-length field is small first, it can save storage space (a fixed-length field requires a fixed-length storage space (7.0 and later versions) even if the data is null). Secondly, for queries, searching in a relatively small field is obviously more efficient, and more records may be stored on each page (8 KB, this can also reduce I/O consumption and improve performance.

19. Do not use select * from t anywhere, replace "*" with a specific field list, and do not return any fields that are not used.

20. Try to use table variables instead of temporary tables. If the table variable contains a large amount of data, note that the index is very limited (only the primary key index ).

21. Avoid frequent creation and deletion of temporary tables to reduce the consumption of system table resources.

22. Temporary tables are not unavailable. Using them appropriately can make some routines more effective. For example, when you need to reference large tables or a data set in common tables repeatedly. However, it is best to use the export table for one-time events.

23. when creating a temporary table, if a large amount of data is inserted at one time, you can use select into instead of create table to avoid creating a large number of logs to increase the speed. If the data volume is small, to ease system table resources, create table first and then insert.

24. if a temporary table is used, you must explicitly delete all temporary tables at the end of the stored procedure. First truncate the table and then drop the table, so that the system table can be locked for a long time.

25. Avoid using a cursor whenever possible, because the efficiency of the cursor is poor. If the cursor operation has more than 10 thousand rows of data, you should consider rewriting.

26. Use a cursor-based method or a temporary table

 

Original post address:
Http://community.csdn.net/Expert/topic/4428/4428715.xml? Temp =. 6633112.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.