1. To optimize the query, you should try to avoid full table scanning, and first consider creating an index on the columns involved in where and order by.
2. Try to avoid null value judgment on fields in the where clause, otherwise it will cause the engine to abandon the use of indexes and perform full table scanning, such as:
select id from t where num is null
You can set the default value of 0 on the num, make sure there is no null value in the num column in the table, and then query like this:
select id from t where num=0
3. Try to avoid using != or <> operators in the where clause, otherwise the engine will abandon the use of indexes and perform full-table scanning.
4. You should try to avoid using OR in the where clause to join the condition, otherwise it will cause the engine to abandon the use of indexes and perform a full table scan, such as:
select id from t where num=10 or num=20
You can query like this:
select id from t where num=10
union all
select id from t where num=20
5.in and not in should also be used with caution, otherwise it will lead to full table scanning, such as:
select id from t where num in(1,2,3)
For continuous values, don't use in if you can use between:
select id from t where num between 1 and 3
6. The following query will also result in a full table scan:
select id from t where name like '%abc%'
To improve efficiency, consider full-text search.
7. If you use a parameter in a where clause, it will also cause a full table scan. Because SQL only resolves local variables at runtime, but the optimizer cannot defer the selection of access plans to runtime; It must be selected at compile time. However, if an access plan is established at compile time, the value of the variable is still unknown and therefore cannot be used as an input item for index selection. The following statements will be scanned in full:
select id from t where num=@num
You can force the query to use an index instead:
select id from t with(index(index(index name)) where num=@num
8. Try to avoid expressing fields in the where clause, which will cause the engine to abandon the use of indexes in favor of full table scanning. For example:
select id from t where num/2=100
should be changed to:
select id from t where num=100*2
9. Try to avoid performing function operations on fields in the where clause, which will cause the engine to abandon the use of indexes in favor of full table scanning. For example:
select id from t where substring(name,1,3)='abc' --name id that starts with abc
select id from t where datediff(day,createdate,'2005-11-30')=0--'2005-11-30' generated id
should be changed to:
select id from t where name like 'abc%'
select id from t where createdate>='2005-11-30' and createdate<'2005-12-1'
10. Do not perform functions, arithmetic operations, or other expression operations to the left of the "=" in the where clause, otherwise the system may not be able to use the index correctly.
11. When using an index field as a condition, if the index is a composite index, then the first field in the index must be used as a condition to ensure that the system uses the index, otherwise the index will not be used, and the order of the fields should be consistent with the index order as much as possible.
12. Don't write some meaningless queries, such as generating an empty table structure:
select col1,col2 into #t from t where 1=0
This type of code does not return any result set, but it consumes system resources, so it should be changed to something like this:
create table #t(...)
13. Many times it is a good choice to replace in with exists:
select num from a where num in(select num from b)
Replace with 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 is based on the data in the table to optimize the query, when the index column has a large amount of data duplication, SQL queries may not use the index, such as a table has a field sex, male, female is almost half each, then even if the index is built on sex, it will not play a role in query efficiency.
15. The more indexes are not the better, the index can certainly improve the efficiency of the corresponding select, but it also reduces the efficiency of insert and update, because the index may be rebuilt when insert or update, so how to build an index needs to be carefully considered, depending on the specific situation. It is best not to have more than 6 indexes in a table, and if there are too many, consider whether it is necessary to build indexes on some infrequently used columns.
16. Avoid updating clustered index data columns as much as possible, because the order of clustered index data columns is the physical storage order of table records, and once the column value changes, it will lead to the adjustment of the order of the entire table records, which will consume considerable resources. If your application needs to update clustered index columns frequently, you need to consider whether you should build the index as a clustered index.
17. Try to use numeric fields, and try not to design fields that only contain numerical information as characters, which will reduce the performance of queries and connections, and increase storage overhead. This is because the engine compares each character in the string one by one when processing queries and joins, whereas for numeric types, it only needs to be compared once.
18. Use varchar/nvarchar instead of char/nchar as much as possible, because firstly, the longer field storage space can save storage space, and secondly, for queries, the search efficiency in a relatively small field is obviously higher.
19. Don't use select * from t anywhere, replace "*" with a specific list of fields, and don't 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 frequently creating and deleting temporary tables to reduce the consumption of system table resources.
22. Temporary tables are not unusable, and using them appropriately can make some routines more effective, for example, when you need to repeatedly reference a dataset in a large table or a commonly used table. However, for one-time events, it's best to use an export table.
23. When creating a temporary table, if the amount of data inserted at one time is large, then you can use select into instead of create table to avoid causing a large number of logs to improve speed; If the amount of data is not large, in order to ease the resources of the system table, you should create table first and then insert.
24. If a temporary table is used, be sure to explicitly delete all temporary tables at the end of the stored procedure, truncate the table first, and then drop the table, so as to avoid the system table being locked for a long time.
25. Try to avoid using the cursor, because the efficiency of the cursor is poor, if the data operated by the cursor exceeds 10,000 lines, then you should consider rewriting.
26. Set-based solutions should be looked for to solve problems before using cursor-based or temporary table methods, which are often more effective.
27. Like temporary tables, cursors are not unusable. Using FAST_FORWARD cursors for small datasets is often better than other row-by-row processing methods, especially if you have to reference several tables to get the data you need. Routines that include "total" in the result set are usually faster than those that are executed with the cursor. If development time permits, both cursor-based and set-based methods can be tried to see which works better.
28. Set SET NOCOUNT ON at the beginning of all stored procedures and triggers, and SET NOCOUNT OFF at the end. There is no need to send DONE_IN_PROC messages to the client after executing each statement of the stored procedure and trigger.
29. Try to avoid large transaction operations and improve the concurrency capacity of the system.
30. Try to avoid returning large data to the client, if the data volume is too large, you should consider whether the corresponding demand is reasonable. |