How to make your SQL run faster

zhaozj2021-02-17  47

How to make your SQL run faster

---- People tend to fall into a misunderstanding when using SQL, that is, it is too concerned that the result is correct, and ignore the possible performance differences between different implementations, this performance difference is large or complex In the database environment (such as online transaction processing OLTP or decision support system DSS) is particularly obvious. The author found in work practice, poor SQL often comes from inappropriate index design, unsolicient connection conditions and inertable WHERE clauses. After proper optimization of them, its running speed is significantly improved! Below I will summarize from these three aspects:

---- For more intuitive explanation, the SQL running time in all instances is tested, no more than 1 second is expressed as (<1 second).

---- Test Environment ------ Host: HP LH II ---- The frequency: 330MHz ---- memory: 128 mega ---- Operating system: OperServer5.0.4 ---- Database: Sybase11 .0.3

First, unreasonable index design ---- Example: Table Record has 620000 lines, try to see the operation of several SQL below in different index: ---- 1. Construction of a non-communical index on Date

Select Count (*) from Record Where Date> '199991201' And Date <'19991214' And Date <' 19991214'and Amount> 2000 (25 second) Select Date, Sum (Amount) from Record Group by Date (55 seconds) Select Count (*) from Record WHERE DATE> '1990901' And Place in ('bj', 'sh') (27 seconds)

---- Analysis: ---- Date has a lot of repetition values, under the non-clustered index, data is physically randomly stored on the data page, and you must perform a table scan to find this range. All rows within.

---- 2. A cluster index on Date

Select Count (*) from Record Where Date> 'and Date <' 19991214 'And Amount> 2000 (14 second) Select, SUM (Amount) from Record Group by Date (28 seconds) Select Count (*) from Record WHERE DATE> '1990901' And Place in ('bj', 'sh') (14 seconds)

---- Analysis: ---- Under the cluster index, the data is physically in order on the data page, and the repetition value is also arranged together, and it can be found in the range, and you can first find the starting point of this range, and Scan the data pages only within this range, avoiding a wide range of scans, increase the query speed.

---- 3. Combine index on Place, Date, Amount

Select Count (*) from Record Where Date> 'and Date <' 19991214 'And Amount> 2000 (26 second) Select, SUM (Amount) from Record Group by Date (27 second) Select Count (*) from Record WHERE DATE> '19990901' AND discount in ('bj,' sh ') ---- Analysis: ---- This is an unseasonful combination index, because its leading column is Place, The first and second SQL did not reference the Place, so there is no use of the index; the third SQL uses the Place, and all columns referenced are included in the combined index, and the index coverage is formed, so it's speed is very fast. of.

---- 4. Combine index on Date, Place, Amount

Select Count (*) from Record Where Date> 'and Date <' 19991214 'and Amount> 2000 (<1 second) Select Date, Sum (Amount) from Record Group by Date (11 second) Select Count (*) from Record where Date> '19990901' and Place in ('bj', 'sh') (<1 second)

---- Analysis: - This is a reasonable combination index. It uses DATE as the leader, allowing each SQL to utilize indexes, and forms an index coverage in the first and third SQLs, and thus performance has achieved optimal.

---- 5. Summary:

---- The index established by default is a non-clustered index, but sometimes it is not the best; reasonable index design is built on various queries analysis and prediction. General:

---- 1. There are a lot of repetition values, and often have a range inquiry.

(Between,>, <,> =, <=) and the columns that occur, the group BY, can consider establish a cluster index;

---- 2. Always access multiple columns simultaneously, and each column contains a repetition value to consider establish a combined index;

---- 3. Combined indexes should try to make a key query form an index coverage, and the leading list must be the most frequent column.

Second, the connection condition: ---- Example: Table Card has 7896 lines, there is a non-aggregated index on Card_no, table Account has 191122 lines, there is a non-aggregated index on Account_no, trying to look at different tables Under connection conditions, the execution of two SQL:

SELECT SUM (A.Amount) from Account A, Card B Where A.CARD_NO = B.Card_no (20 seconds)

---- Change SQL to: SELECT SUM (A.Amount) from Account A, Card B Where A.Card_no = B.Card_no and a.account_no = B.account_NO (<1 second)

---- Analysis: ---- In the first connection condition, the best query scheme is to make an access to an external table, a CARD as a table, and use the index on the Card. The number of I / O can be made from the following formula. Estimated as: ---- Outer Table Account 22541 page (Outer Table Account 191122 Row * Inland Table Card) 3 pages of the first line of the first line on the outer table) = 595907 times I / O

---- In the second connection condition, the best query scheme is to make a CARD out of the table, an Account as an inner table, and use an index on Account. The number of I / O can be estimated by the following formula:

--- 1944 page on the outer table Card (Outer Table Card 7896 Row * Inland Table Account, the 4 pages of each line to find each other) = 33528 times I / O

---- It can be seen that only the fully connected conditions, the real best solution will be executed.

---- to sum up:

---- 1. Multi-table operations before being implemented, the query optimizer lists several groups of possible connection scenarios, and identify the best solutions for system overhead based on the connection conditions. The connection condition should be considering the table with indexes, the number of rows of rows; the selection of the inner and outer tables can be determined by the formula: the number of matches in the outer table * The number of times in the inner layer table is determined, the minimum is the best Program.

---- 2. View the implementation method 310, 302).

Third, an inelaborative WHERE clause ---- 1. Example: The columns in the following SQL condition statements have a proper index, but the execution speed is very slow:

Select * from record WHERESUBSTRING (CARD_NO, 1, 4) = '5378' (13 seconds) Select * from Record Whereamount / 30 <1000 (11 seconds) Select * from record whereconvert (char (10), DATE, 112) = ' 19991201 '(10 seconds)

---- Analysis: ---- WHERE clauses The results of the columns in the list are calculated by the SQL runtime, so it has to perform table search, without using the index above the column; if These results can be obtained when querying compile, then it can be optimized by the SQL optimizer, using an index, avoiding the table search, so rewriting SQL into the following:

Select * from record where card_no like Like'5378% '(<1 second) Select * from Record Where Amount <1000 * 30 (<1 second) SELECT * from Record where Date =' 1999/12/01 '(<1 second) ---- You will find that SQL is obviously faster!

---- 2. Example: Table stuff has 200,000 rows, there is a non-clustered index on id_no, please see the following SQL:

SELECT Count (*) from stuff where id_no in ('0', '1') (23 seconds)

---- Analysis: - 'I in' in WHERE Condition is logically equivalent to 'or', so the grammatical analyzer converts in ('0', '1') into ID_NO = '0' OR ID_NO = '1' is executed. We expect it to find separately according to each OR clause, then add the result, which can take the index on ID_no; but in fact (according to Showplan), it adopts "OR Strategy", that is, take out each The line of the OR clause, in the worksheet of the temporary database, then establish a unique index to remove the repetition, and finally calculate the results from this temporary table. Therefore, the actual process does not use ID_no to index, and the completion time is also affected by the performance of the Tempdb database. ---- Practice has proved, the more the number of lines, the worse the performance of the worksheet, when STUFF has 62,00000 lines, the implementation time has reached 220 seconds! It is better to separate the OR clause:

SELECT Count (*) from stuff where id_no = '0'select count (*) from stuff where id_no =' 1 '

---- Get two results, then make an additional calculation. Because each sentence uses an index, the execution time is only 3 seconds, and the time is only 4 seconds at 620000. Or, with a better way, write a simple stored procedure: create proc count_stuff asseclare @a intDeclare @B INTDECLARE @c INTDECLARE @d char (10) beginselect @ a = count (*) from stuff where id_no = '0' SELECT @ b = count (*) from stuff where id_no = '1'ndselect @ c = @ a @ bselect @ d = conver (char (10), @ c) Print @d

---- Direct calculation, the execution time is as fast as above! ---- to sum up:

---- It can be seen that the WHERE clause uses an index, and a table scan or additional overhead occurs.

---- 1. Any of the listed operations will cause a table scan, including database functions, calculation expressions, etc., move as much as possible to the right right when query.

---- 2.in, the OR clause often uses a worksheet to make index; if you do not generate a large amount of repetition values, you can consider unpacking the clause; the index should be included in the unpublished clause.

---- 3. Be good at using the stored procedure, which makes SQL more flexible and efficient.

---- From these examples, it can be seen that the essence of SQL optimization is to use the statement that can be identified by the optimizer, reducing the I / O number of table scans, and try to avoid watch search happened. In fact, the performance optimization of SQL is a complex process. These are only an embodiment of the application level, and in-depth studies will also involve the resource allocation of the database layer, the flow control of the network layer and the overall design of the operating system layer.

转载请注明原文地址:https://www.9cbs.com/read-29884.html

New Post(0)