How to improve the efficiency of MySQL query???

Source: Internet
Author: User
Tags mysql query

1. To optimize the query, avoid full-table scanning as far as possible, and first consider establishing an index on the columns involved in the Where and order by.

 2. You should try to avoid null values in the WHERE clause, otherwise it will cause the engine to discard full table scans using the index, such as:  select ID from t where the NUM is null  can set default value on Num 0 , make sure that the NUM column in the table does not have a null value, and then query:  select ID from where num=0 3. You should try to avoid using the! = or <> operator in the WHERE clause, otherwise the engine discards the use of the index for 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 discard full table scans using the index, such as:  select ID from t where num=10 or num=20  can be queried like this: Sele The CT ID from the t where num=10 union ALL select IDs from T where num=20 5.in and not in are also used with caution, otherwise it will result in a full table scan, such as:  select ID from t where num in (between)   for consecutive values, do not use in:  select ID from t where num between 1 and 3 6. The following query will also cause Full table scan:  select ID from t where name like '%abc% '   to improve efficiency, full-text indexing can be considered.  7. If you use a parameter in the WHERE clause, it also causes a full table scan. Because SQL resolves local variables only at run time, the optimizer cannot defer the selection of access plans to run time; 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 selected as an input for the index. The following statement will perform a full table scan: The  select ID from where [email protected]  can be changed to force the query to use the index:  select ID from the T with (Index ( Index name) where [email protected] 8. You should try to avoid expression of a field in the Where clause, which causes theThe full table scan is discarded using the index. For example:  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 function operations on fields in the WHERE clause. This causes the engine to discard the full table scan using the index. such as: 

Select ID from t where substring (name,1,3) = ' abc '--name ID starting with ABC
Select ID from t where DATEDIFF (day,createdate, ' 2005-11-30 ') =0--' 2005-11-30 ' generated ID
should read:
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 "=" in the WHERE clause, or the index may not be used correctly by the system.  11. When using an indexed field as a condition, if the index is a composite index, you must use the first field in the index as a condition to guarantee 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, such as the need to generate an empty table structure:  select col1,col2 into #t the from T where 1=0  such code does not return any result set, but consumes system resources, Should be changed to 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:  select num From a Where exists (select 1 from b where Num=a.num)  14. Not all indexes are valid for queries, and SQL is optimized for queries based on the data in the table, and when there is a large amount of data duplication in the index columns, SQL queries may not take advantage of indexes, such as tables with fields Sex,male and female almost half, so even indexing on sex does not work for query efficiency.  15. The index is not the more the better, although the index can improve the efficiency of the corresponding select, but also reduce the efficiency of insert and UPDATE, because the INSERT or update when the index may be rebuilt, so how to build the index needs careful consideration, depending on the situation. The number of indexes on a table should not be more than 6, if too many you should consider whether some of the indexes that are not commonly used are necessary.  16. You should avoid updating clustered index data columns as much as possible, because the order of the clustered index data columns is the physical storage order of the table records, which can consume considerable resources once the column values change to the order in which the entire table records are to be adjusted. If your application needs to update clustered index data columns frequently, you need to consider whether the index should be built as a clustered index.  17. Use numeric fields as much as possible, and if fields with numeric information are not designed as character types, this can degrade query and connection performance and increase storage overhead. This is because the engine is working on queries and connectionsEach character in the string is compared one at a time, but only once for a numeric type.  18. Use Varchar/nvarchar instead of Char/nchar as much as possible, because the first variable-length field has a small storage space and can save storage space, and secondly, in a relatively small field, search efficiency is obviously higher for queries.  19. Do not use SELECT * from t anywhere, use a specific field list instead of "*", and do not return any fields that are not available.  20. Try to use table variables instead of temporary tables. If the table variable contains a large amount of data, be aware 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 unusable, and they can be used appropriately to make certain routines more efficient, for example, when you need to repeatedly reference a dataset in a large table or a common table. However, for one-time events, it is best to use an export table.  23. When you create a new temporary table, if you insert a large amount of data at one time, you can use SELECT INTO instead of CREATE table to avoid creating a large number of logs to increase the speed, and if the amount of data is small, to mitigate the resources of the system tables, create table first, 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 table first, and then drop table, which avoids longer locking of the system tables.  25. Avoid using cursors as much as possible because cursors are inefficient and should be considered for overwriting if the cursor is manipulating more than 10,000 rows of data.  26. Before using a cursor-based method or temporal table method, you should first look for a set-based solution to solve the problem, and the set-based approach is generally more efficient.  27. As with temporary tables, cursors are not unusable. Using Fast_forward cursors on small datasets is often preferable to other progressive processing methods, especially if you must reference several tables to obtain the required data. Routines that include "totals" in the result set are typically faster than using cursors. If development time permits, a cursor-based approach and a set-based approach can all be tried to see which method works better.  28. Set NOCOUNT on at the beginning of all stored procedures and triggers, set NOCOUNT OFF at the end. You do not need to send a DONE_IN_PROC message to the client after each statement that executes the stored procedure and trigger.  29. Try to avoid large transaction operations and improve the system concurrency capability.  30. Try to avoid the return of large data to the client, if the amount of data is too large, should consider whether the corresponding demand is reasonable.

How to improve the efficiency of MySQL query???

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.