Reprinted: Sql server data ten million large SQL query optimization

Sql server data ten million large SQL query optimization

Disclaimer: This article is a blogger original article, follow the  CC 4.0 BY-SA  copyright agreement, reproduced, please attach the original source link and this statement.
This link: https://blog.csdn.net/u013938578/article/details/81412091

1. query optimization, should try to avoid full table scan, should first consider indexing by the column involved in where and order.


2. fields should be avoided to a null value is determined in the where clause, will cause the engine to give up using the index and a full table scan, such as: select id from t where num is null may be provided on a default value of 0 num, to ensure num table column value is not null, then this query: select id from t where num = 0


3 should be avoided in the where clause! = Or <> operator, otherwise the engine will give up using the index and a full table scan.


4 should be avoided in the where clause to connect or condition will cause the engine to give up using the index and a full table scan, such as: select id from t where num = 10 or num = 20 can do a query: select id from t where num = 10 union all select id from t where num = 20


5.in should be used with caution and not in, otherwise it will lead to a full table scan, such as: select id from t where num in (1,2,3) for continuous values ​​between can not use in the: select id from t where num between 1 and 3


6. The following query will also lead to a full table scan: select id from t where name like '% Lee%' To improve efficiency, can be considered full-text search.


7. If the parameters in the where clause, will lead to a full table scan. Because SQL at runtime will only resolve local variables, but the optimizer can not defer the choice of access plan to run; it must choose at compile time. If, however, establish access plan at compile time, the value of the variable is unknown, and therefore can not be used as an index entry selected. As the following statement will perform full table scan: select id from t where num = @ num can be changed to force the query using the index: select id from t with (index (index name)) where num = @ num


8. fields should be avoided to operate in the where clause expressions, which will cause the engine to give up using the index and a full table scan. Such as: select id from t where num / 2 = 100 should be changed to: select id from t where num = 100 * 2


9. The field should be avoided to a function operation in the where clause that will cause the engine to give up using the index and full table scan. Such as: select id from t where substring (name, 1,3) = 'abc', name beginning to id abc should read:

select id from t where name like ‘abc%’


10. Do not carry out functions, arithmetic operations, or other expressions in the where clause "=" left, or the system may not work properly indexed.


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


12. Do not write the query does not make sense, such as the need to create an empty table structure: select col1, col2 into #t from t where 1 = 0

This code does not return any result sets, but consumes system resources and should be replaced by this: 
the Create the Table #t (...)


13. often used in place exists is a good choice: select num from a where num in (num from b select)

Replaced with the following statement: 
SELECT NUM from A WHERE EXISTS (SELECT. 1 from B WHERE NUM = a.num)


14. Not all indexes are valid query, SQL query optimization is performed according to data in the table when the index column has a lot of duplication of data, SQL queries may not go to use the index, such as a table has a field sex, male, almost half of each female, even if the index built on sex also no effect on the query efficiency.


15. The index is not possible, the corresponding index can certainly improve the efficiency of select, but also reduces the efficiency of insert and update, because it is possible when the insert or update will rebuild the index, the index needs to be carefully considered how to build, As the case may be. An index number table is best not more than six, if too much you should consider some of the less frequently used to build the index column if necessary.


16. The update should be avoided as much as possible clustered index data column, the column because the order of the index data is clustered physical storage order recorded in the table, once the column will result in the adjustment value changes in the order of recording the entire table, will consume considerable resources. If applications require frequent updates clustered index data columns, you need to consider whether it should be built for the clustered index index.


17 make use of numeric fields, if only the fields containing numerical information is not possible for the character design, which reduces the performance of the connections and queries, and increases storage costs. This is because the engine when processing queries and connections one by comparing each character in the string, and for numeric comparison purposes only once is enough.


18. The possible use varchar / nvarchar instead of char / nchar, because first of all variable-length fields small storage space, you can save storage space, followed by the query, in a relatively small field of search efficiency is clearly higher.


19. Do not place any use select * from t, with a specific list of fields instead of "*", do not return any of the fields with less than.


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


21. Avoid frequent create and delete temporary tables, system tables to reduce the consumption of resources.


22. A temporary table is not unusable, they can make appropriate use of certain routines more efficient, for example, when it is necessary a large table or tables commonly duplicate references a data set. However, for a one-time event, it is best to use export table.


23. When the new temporary table, if one inserts a large amount of data, it may be used instead of select into create table, to avoid a large number of log, in order to increase speed; if small data, in order to ease the system resource table, should create table, then insert.


24. If you use a temporary table to be sure all the temporary table explicit deleted at the end of the stored procedure, first truncate table, then drop table, to avoid locking the system tables a long time.


25. Try to avoid using a cursor, because the poor efficiency of the cursor, if the cursor operation more than 10,000 lines, you should consider rewriting.


26. Use the cursor before the method or methods based on temporary tables, you should look for set-based solutions to solve the problem, usually more efficient set-based method.


27. temporary tables, cursors are not unusable. Use FAST_FORWARD cursor on small data sets are usually better than other progressive treatment methods, particularly in reference to several tables must be in order to obtain the required data. In the result set includes "total" than usual routines performed by using the cursor speed fast. If the development time permits, cursor-based methods and can be set based approach to try to see which method is better.


28. Set SET NOCOUNT ON at the beginning of all the stored procedures and triggers, SET NOCOUNT OFF disposed at the end. DONE_IN_PROC not need to send a message to the client after each statement is executed and triggers stored procedure.


29. Try to avoid large transaction operations, improve system concurrency.


30. Avoid returned to the client a large amount of data, if the data is too large, you should consider the corresponding demand is reasonable.

Guess you like

Origin www.cnblogs.com/studycode/p/11619752.html