SQL statement performance optimization operation

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

 

2, should be avoided to a null value is determined in fields where clause, NULL when creating the table is the default, but most of the time use should NOT NULL, or the use of a special value, such as 0, -1 as a default value.

 

3, should be avoided in the where clause = or <> operator, MySQL only use of the following indexes operators:! <, <=, =,>,> =, BETWEEN, IN, and some time LIKE.

 

4, should avoid or use to connect to the conditions in the where clause, will cause the engine to give up using the index and full table scan, you can use UNION combined 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, for continuous values, can not use in between the: Select id from t where num between 1 and 3.

 

6, the following query will result in a full table scan: select id from t where name like '% abc%' or select id from t where name like '% abc' To improve efficiency, can be considered full-text search. The select id from t where name like'abc% 'just use the index.

 

7, if the parameter in the where clause would lead to a full table scan.

 

8, should be avoided in the operation of the fields where clause expression, should be avoided for a function operation in the fields where clause.

 

select num from a where num in (select num from b): 9, is in many cases a good selection exists instead. Replaced with the following statement: select num from a where exists (select 1 from b where num = a.num).

 

10, although the index can improve the efficiency of the corresponding select, but also reduces the efficiency of insert and update, because it is possible when the insert or update will rebuild the index, so the need to carefully consider how to build the index, 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.

 

11, 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.

 

12, make use of numeric fields, numeric fields containing information if only so as not to design for the character, which will reduce the performance of queries and connections, and will increase the storage overhead.

 

13, as much as possible the use of 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.

 

14, it is best not to use "" Return all: select from t, with a specific list of fields instead of "*", do not return any of the fields with less than.

 

15, returned to the client to avoid large amounts of data, if the data is too large, you should consider the corresponding demand is reasonable.

 

16, alias table (Alias): When connecting a plurality of tables in the SQL statement, the table using the alias and the alias prefix on each Column. As a result, it can reduce the time to resolve and reduce those grammatical errors caused by Column ambiguity.

 

17, using the "temporary table" Scratch intermediate results:

 

An important method to simplify the SQL statement is to use a temporary table temporary storage of intermediate results, but the benefits far more than a temporary table, the result will be temporarily stored in the temporary temporary table, query on the back in tempdb, this procedure avoids multiple scans the main table, but also greatly reduces program execution "shared lock" blocking "update locks", reducing congestion, and improving concurrency.

 

18, some SQL queries should be added nolock, reading and writing will be blocked each other, in order to improve concurrency for some queries, you can add nolock, so read when you can allow to write, but the downside is likely to read uncommitted dirty data.

 

Use nolock there are three principles:

 

  • The results for the query "insert, delete, change" can not add NOLOCK;

  • Table query page belong to split frequent, caution nolock;

  • Using temporary tables can be saved as "pre-data shadow", play a similar function undo Oracle table space, temporary table can be adopted to improve the performance of concurrency, do not use nolock.

 

19, simplify common rules are as follows:

 

Do not have more than five or more connections tables (JOIN), consider the use of temporary variables table or table storing intermediate results. Less subquery, not too deep nested view, a general view of nested preferably not more than 2.

 

20, will need to check the results of the pre-calculated on the table, the query time and then Select. This is the most important tool in SQL7.0 ago, for example, inpatient hospital fee calculation.

 

21, with the OR of words can be decomposed into a plurality of queries, queries and is connected by a plurality of UNION. Their speed is only whether to use the same index, and if queries need to use the joint index, with a more efficient implementation of the UNION all. OR plurality of words does not use the index, and then rewritten into the form UNION trying to match the index. A key problem is the index used.

 

22, after the IN list denomination, the most frequent values ​​will appear on the front, the least appear on the final surface, reduce the number of judgments.

 

23, as far as possible the processing of data on the server, to reduce the overhead of the network, such as the use of stored procedures.

 

A stored procedure is compiled, optimized, and are organized into an execution plan in, and SQL statements in the database storage, is a collection of control flow language, fast speed, of course. Dynamic SQL is repeatedly performed, use the temporary storage process, which (temporary table) is placed in Tempdb.

 

24, when the server's memory is long enough, the preparation of the number of threads +5 = maximum number of connections, this can maximize efficiency; otherwise use the preparation of number of threads <maximum number of connections that enable SQL SERVER the thread pool to solve, or if the maximum number of connections = number +5, serious damage to the performance of the server.

 

25, associated with the query with the written order:

 

select a.personMemberID, * from chineseresume a,personmember b where personMemberID = b.referenceid and a.personMemberID = ‘JCNPRH39681’ (A = B ,B = ‘号码’) 

 

select a.personMemberID, * from chineseresume a,personmember b where a.personMemberID = b.referenceid and a.personMemberID = ‘JCNPRH39681’ and b.referenceid = ‘JCNPRH39681’ (A = B ,B = ‘号码’, A = ‘号码’) 

 

select a.personMemberID, * from chineseresume a,personmember b where b.referenceid = ‘JCNPRH39681’ and a.personMemberID = ‘JCNPRH39681’ (B = ‘号码’, A = ‘号码’)

 

26, make use exists instead of select count (1) to determine whether there is a record, count the number of functions used only when all the rows in tables, and the count (1) (*) more efficient than count.

 

27, to make use of "> =" Do not use ">."

 

28, use of the index specification:

 

  • To create the index considered in conjunction with the application, the proposed large OLTP table not more than six indexes;

  • Use index field as much as possible as a query, especially in a clustered index, you can be forced through the specified index index index_name when necessary;

  • Avoid table scan, when necessary, to consider the new index queries on large tables;

  • In the field as a condition of using the index, if the index is a joint 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;

  • Pay attention to the maintenance of the index, periodic rebuild the index, recompile the stored procedure.  

 

29, the following conditional statement in SQL columns are built right index, but the execution speed is very slow: 

 

SELECT * FROM record WHERE substrINg(card_no,1,4)=’5378’ (13秒) 

 

SELECT * FROM record WHERE amount/30< 1000 (11秒) 

 

SELECT * FROM record WHERE convert(char(10),date,112)=’19991201’ (10秒) 

 

analysis: 

 

WHERE clause any operation results are listed in the SQL runtime calculated column by column, it has to search a table, without using the column index above.

 

If these results can be obtained in a query compile time, then it can be optimized SQL optimizer use the index to avoid table search, so the SQL re-written as follows: 

 

SELECT * FROM record WHERE card_no like ‘5378%’ (< 1秒) 

 

SELECT * FROM record WHERE amount< 1000*30 (< 1秒) 

 

SELECT * FROM record WHERE date= ‘1999/12/01’ (< 1秒)

 

30, when there is a batch insert or update, and insert a batch or batch updates, will not be a bar to update the record.

 

31, all of the stored procedure, it is possible, I would never use the cycle to achieve a SQL statement.

For example: Lists each day last month, I will connect by recursive queries to look, never go with the cycle from the first day to the last day of the last month.

 

32, select the most efficient sequence table name (only valid in the rule-based optimizer): 

 

Oracle parser right to left in the order of the processing table in the FROM clause, written in the FROM clause final table (base table driving table) will be processed first, comprising a plurality of tables in the FROM clause under the circumstances, you must select the least number of records as a basis for the table table.

 

If there are three or more connection table queries, it would need to select the cross-table (intersection table) as a base table, cross table refers to the table that is referenced by other tables.

 

33, the GROUP BY statement efficiency may not be required by a recording filtered before GROUP BY. The following two queries return the same result, but the second obviously on a lot faster. 

 

Inefficient:

 

SELECT JOB , AVG(SAL) 

FROM EMP 

GROUP BY JOB 

HAVING JOB =’PRESIDENT’ 

OR JOB =’MANAGER’ 

 

Efficient: 

 

SELECT JOB , AVG(SAL) 

FROM EMP 

WHERE JOB =’PRESIDENT’ 

OR JOB =’MANAGER’ 

GROUP BY JOB

 

34, SQL statements in uppercase, because Oracle is always to parse SQL statements, the lowercase letters converted to uppercase and then executed.

 

35, using an alias, the alias technique is the application of large databases, is the table name, column alias names to a letter in the query, the query faster than build the connection table 1.5 times faster.

 

Always in the same order 36 to avoid deadlocks, access the same table in your stored procedures and triggers; the transaction was likely to be shortened, in a transaction should reduce the amount of data involved to the extent possible; Never waiting for user input in a transaction.

 

37, avoid the use of temporary tables, unless there is need, or should try to avoid using a temporary table, on the contrary, you can use a table variable instead; most of the time (99%), table variables stationed in memory, so the speed is faster than the temporary table temporarily stationed in TempDb database tables, so the operation on the temporary table requires communication across databases, naturally slow speed.

 

38, it is best not to use triggers:

 

  • Trigger a trigger, perform a trigger event itself is a resource-consuming process;

  • If you can use constraints to achieve, try not to use a trigger;

  • Do not use the same trigger for different triggering event (Insert, Update and Delete);

  • Do not use the transaction type the code in the trigger.

 

39, index creation rules: 

 

  • Primary key, foreign key must have an index; 

  • 300 more than the amount of data table should have an index; 

  • Table often connected with other tables, in connection field should be indexed; 

  • Often it appears in the Where clause field, especially in the field of large tables should be indexed; 

  • Index should be in the field of high selectivity; 

  • Index should be built on a small field, for large text fields and even fields long, not indexed; 

  • The establishment of a composite index requires careful analysis, try to consider instead of using a single-field index; 

  • Correct choice of the main column in the composite index field, a field is generally better selectivity; 

  • If several fields of the composite index often while AND to appear on the Where clause? Query whether a single field with little or no? If so, you can establish a composite index; otherwise consider single-field index; 

  • If the field contains the composite index often appear separately in the Where clause is decomposed into a plurality of single field index; 

  • If the field contains the composite index more than three, then carefully considered if necessary, consider reducing the composite field; 

  • If both single-field index, another composite index on these fields, you can generally delete a composite index; 

  • Frequent table data manipulation, do not create too much of an index; 

  • Delete unused indexes, to avoid negative impact on the implementation plan; 

  • Each index built on the table will increase the storage overhead, the index for insert, delete, update operations will increase processing overhead. In addition, excessive composite index, index fields in a single case, there is generally no value; the contrary, will reduce the increase in performance when deleting data, especially for frequent updates of the table, the more negative impact Big. 

  • Try not to index the field in the database contains a large number of duplicate values.

 

40, MySQL query optimization Summary:

 

The slow query log to find the slow query, the query execution plan to judge whether the normal operation, always test your query to see if they are running in the best condition.

 

Over time the performance will always change, avoid using count on the entire table (*), it may lock the entire table, the query is consistent for subsequent similar queries can use the query cache, use the GROUP BY, in appropriate circumstances rather than DISTINCT is used in WHERE, GROUP BY, and ORDER BY clause indexed columns, the index remains simple, not included in the index with a plurality of columns.

 

MySQL sometimes use the wrong index, in this case using USE INDEX, examined using SQL_MODE = STRICT problem index field for the number of records is less than 5, when using a UNION LIMIT is not OR. 

 

In order to avoid updating before SELECT, INSERT ON DUPLICATE KEY or use INSERT IGNORE, do not use UPDATE to achieve, do not use MAX, using the index field and ORDER BY clauses, LIMIT M, N can actually slow down the query in some cases, used sparingly, using UNION instead of a subquery in the WHERE clause in MySQL restart, remember to warm your database to ensure data in memory and query speed, consider a persistent connection, rather than multiple connections to reduce overhead.

 

Reference inquiries, including the load on the use of the server, sometimes a simple query can affect other queries, when the load increases on the server, use SHOW PROCESSLIST to see slow and problematic query image data generated in the development environment in test All suspicious queries.

 

41, MySQL backup process:

 

  • Backup copy from the secondary server;

  • Stopped during the execution of the backup copy, in order to avoid inconsistencies in the data dependency and foreign key constraints;

  • Complete cessation of MySQL, the database files from a backup;

  • If you are using MySQL dump to back up, back up while the binary log files - make sure to copy without interruption;

  • Do not trust LVM snapshot, which is likely to produce data inconsistencies, the future will give you trouble;

  • To make it easier to restore a single table to table to export the data as a unit - If the data is isolated from the other tables. 

  • When using mysqldump use -opt;

  • Check and optimize the table before backing up;

  • For faster import when imported temporarily disable foreign key constraints. ;

  • For faster import, temporarily disable the detection of unique import;

  • Calculating a database, and the size of the index table after each backup, in order to be more growth monitoring data size;

  • By automatically scheduling a script to monitor the replication errors and delays instance;

  • Regularly perform backups.

 

42, the buffer does not automatically query processing space, thus, when writing SQL statements, should minimize the use of space, especially in the space and the first end of SQL (buffer because the query is not automatically intercept and trailing spaces).

 

43, member as the standard by mid divide table easier to find it? General business requirements are basically username for the query based on the normal username should be made to the modulo hash sub-table.

 

The points table, then the MySQL partition function is doing that, the code is transparent; the code level to achieve the seemingly unreasonable.

 

44, we should all set an ID as its primary key for each table in the database, and best of all a INT type (recommended UNSIGNED), and set the automatic increase in the AUTO_INCREMENT flag.

 

45, is provided at the beginning of SET NOCOUNT ON 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.

 

46, MySQL query can enable high-speed query cache. This is one of the effective ways to improve MySQL database performance optimization. When the same query is executed multiple times, extract data from the cache and returned much faster data directly from the database.

 

47, EXPLAIN SELECT query used to track the viewing results:

 

Use EXPLAIN keyword can let you know how MySQL is handling your SQL statement. This can help you analyze performance bottlenecks in your query or table structure. EXPLAIN query results will also tell you your index primary key is how to use, how your table is searched and sorted.

 

48, when using LIMIT 1 line of data as long as:

 

Sometimes when you query the table, you already know the result will only be a result, but because you might need to go fetch cursor, or you might go check the number of records returned.

 

In this case, the performance can be increased together LIMIT 1. As a result, MySQL database engine will stop the search after finding a piece of data, rather than continue to check back next line with less data record.

 

49, a suitable selection table storage engine: 

 

  • myisam: application to read and insert operations-based, only a small number of updates and deletes, and the integrity of the transaction, concurrency requirements are not very high. 

  • InnoDB: data consistency requirements under the transaction, and concurrent conditions. In addition to the inserts and queries, including many updates and deletes. (InnoDB effectively reduce the delete and update causes the lock).

    For InnoDB table types that support transactions, the main reason is the impact speed AUTOCOMMIT default setting is open, and the program does not explicitly call BEGIN begin a transaction, each leading to insert an automatically committed a serious impact on speed. You can call begin before the implementation of SQL, SQL form a number of things (even if autocommit can also be opened), will greatly improve performance.

 

50, to optimize the data type of the table, select the appropriate type of data: 

 

Principles: smaller is usually better, simple enough, all fields have to have default values, to avoid null. 

 

For example: When the database table design accounted for less disk space to use smaller integer types as possible. (Mediumint would be more appropriate than int) 

 

Such as the time fields: datetime and timestamp, datetime 8 bytes, 4 bytes and timestamp, only half, the timestamp indicates the range suitable 1970-2037 Updated 

 

MySQL can be a good support large amounts of data access, but generally speaking, the smaller tables in the database, perform queries on it also will be faster. 

 

Therefore, when creating the table, in order to obtain better performance, we can set the width of columns in the table as small as possible.

 

For example: when defining the code of this field, if it is set to CHAR (255), to the database clearly adds unnecessary space. Even the use of this type of VARCHAR is superfluous, because the CHAR (6) can be very good to complete the task.

 

Likewise, if possible, we should use MEDIUMINT instead BIGIN to define integer field, you should try to field is set to NOT NULL, so when executing the query, the database do not have to compare the NULL values ​​in the future. 

 

For some text fields, such as "provinces" or "gender", we can define them as ENUM type. Because in MySQL, the ENUM type is treated as numeric data, numeric data and much faster rate than it is processed text type. In this way, we can improve database performance.

 

51, string data type: char, varchar, text selection difference.

 

52, any operation on the columns will cause a table scan, which includes database functions, evaluate expressions, etc., to move the operation to the right of the equal sign as the query.

Guess you like

Origin www.cnblogs.com/wzk-0000/p/11079422.html
Recommended