Database SQL Optimization Summary

1. To optimize the query, try to avoid full table scan. First, you should consider building indexes on the columns involved in where and order by.
2. Try to avoid the null value judgment of the field in the where clause, otherwise the engine will give up the use of the index and perform a full table scan, such as:

select id from t where num is null

It is best not to leave NULL in the database, and fill the database with NOT NULL as much as possible.

Remarks, descriptions, comments, etc. can be set to NULL. For others, it is best not to use NULL.

Don't think that NULL does not need space, such as: char(100) type, when the field is created, the space is fixed, regardless of whether to insert a value (including NULL), it will occupy 100 characters of space, if it is varchar For such variable-length fields, null does not take up space.
You can set a default value of 0 on num, make sure there are no null values ​​in the num column in the table, and then query like this:

select id from t where num = 0

3. Try to avoid using the != or <> operator in the where clause, otherwise the engine will give up the use of the index and perform a full table scan.
4. Try to avoid using or to connect conditions in the where clause. If a field has an index and a field does not have an index, it will cause the engine to give up using the index and perform a full table scan, such as:

select id from t where num=10 or Name = 'admin'

You can query like this:

select id from t where num = 10
union all
select id from t where Name = 'admin'

5. In and not in should also be used with caution, otherwise it will result in a full table scan, such as:

select id from t where num in(1,2,3)

For consecutive values, use between instead of in:

select id from t where num between 1 and 3

Many times using exists instead of in is a good choice:

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)

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 a parameter is used in the where clause, it will also cause a full table scan. Because SQL resolves local variables only at runtime, the optimizer cannot defer the choice of an access plan to runtime; it must choose it at compile time. However, if the access plan is built at compile time, the value of the variable is unknown and cannot be used as an input for index selection. The following statement will perform a full table scan:

select id from t where num = @num

You can instead force the query to use the index:

select id from t with(index(索引名)) where num = @num

8. The expression operation on the field in the where clause should be avoided as much as possible, which will cause the engine to give up the use of the index and perform 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. You should try to avoid functional operations on fields in the where clause, which will cause the engine to give up the use of indexes and perform full table scans. Such as:

select id from t where substring(name,1,3) = 'abc' - id whose name starts with abc
select id from t where datediff(day,createdate,’2005-11-30′) = 0    -–‘2005-11-30’    --生成的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 on the left side 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, 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 should be used. As much as possible, make the field order consistent with the index order.
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 kind of code will not return any result set, but will consume system resources, it should be changed to this:
create table #t(…)

13. Update statement, if only 1 or 2 fields are changed, do not update all fields, otherwise frequent calls will cause obvious performance consumption and bring a lot of logs.
14. For multiple JOIN tables with a large amount of data (hundreds are considered large here), you must first paginate and then JOIN, otherwise the logical read will be very high and the performance will be poor.
15. select count(*) from table; such a count without any conditions will cause a full table scan, and it has no business meaning, so it must be avoided.

16. The more indexes 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 during insert or update, so how to build an index needs to be carefully considered. As the case may be. The number of indexes in a table should not exceed 6. If there are too many indexes, you should consider whether it is necessary to build indexes on some infrequently used columns.
17. Avoid updating the clustered index data column 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, which will consume considerable resources. If the application system needs to update the clustered index data column frequently, it needs to consider whether the index should be built as a clustered index.
18. Use numeric fields as much as possible. If the fields only contain numeric information, try not to design them as character fields, which will reduce the performance of query and connection and increase the storage overhead. This is because the engine compares each character of the string one by one when processing queries and joins, whereas only one comparison is required for numbers.
19. Use varchar/nvarchar instead of char/nchar as much as possible, because first of all, the storage space of variable-length fields is small, which can save storage space. Secondly, for queries, the search efficiency in a relatively small field is obviously higher.
20. Don't use select * from t anywhere, replace "*" with a list of specific fields, and don't return any fields that are not used.
21. Try to use table variables instead of temporary tables. If the table variable contains a lot of data, be aware that the indexes are very limited (only the primary key index).
22. Avoid frequent creation and deletion of temporary tables to reduce the consumption of system table resources. Temporary tables are not unusable, and their proper use can make certain routines more efficient, for example, when a large table or a dataset in a frequently used table needs to be repeatedly referenced. However, for one-time events, it is better to use an export table.
23. When creating a new temporary table, if a large amount of data is inserted at one time, you can use select into instead of create table to avoid causing a large number of logs to improve the speed; if the amount of data is not large, in order to ease the resources of the system table, you should first create table, then insert.
24. If temporary tables are used, all temporary tables must be explicitly deleted at the end of the stored procedure, first truncate table, and then drop table, which can avoid long-term locking of system tables.
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 rows, then you should consider rewriting.
26. Before using the cursor-based method or the temporary table method, you should look for a set-based solution to solve the problem, and the set-based method is usually more efficient.
27. Like temporary tables, cursors are not unavailable. Using FAST_FORWARD cursors on small datasets is often preferable to other row-by-row processing methods, especially when several tables must be referenced to obtain the required data. Routines that include "totals" in the result set are usually faster than using cursors. If development time allows, try both the cursor-based approach and the set-based approach to see which one works better.
28. Set SET NOCOUNT ON at the beginning of all stored procedures and triggers, and set SET NOCOUNT OFF at the end. There is no need to send a DONE_IN_PROC message to the client after each statement of stored procedures and triggers is executed.
29. Try to avoid large transaction operations and improve system concurrency capabilities.
30. Try to avoid returning a large amount of data to the client. If the amount of data is too large, you should consider whether the corresponding demand is reasonable.

Practical case study
: splitting large DELETE or   INSERT statements, batch submitting SQL statements Stop responding. Because these two operations will lock the table, once the table is locked, no other operations can enter.
  Apache will have many child processes or threads. Therefore, it works quite efficiently, and our server does not want to have too many child processes, threads and database links, which are a huge amount of server resources, especially memory.
  If you lock your table for a period of time, say 30 seconds, then for a site with a high traffic volume, the accumulated access processes/threads, database links, number of open files in these 30 seconds may not only It will only make your WEB service crash, and it may also make your entire server hang immediately.
  So, if you have a big deal, you must split it, using LIMIT oracle(rownum), sqlserver(top) condition is a good way. Here is a mysql example:

while(1){

   // only do 1000 entries each time

   mysql_query(“delete from logs where log_date <= ’2012-11-01’ limit 1000”);

   if(mysql_affected_rows() == 0){

     // delete complete, exit!
     break;
  }

// Each time it is suspended for a period of time, the table is released for other processes/threads to access.
usleep(50000)

}

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325049136&siteId=291194637