How to make your SQL run faster zz

xiaoxiao2021-03-06  65

Subject: I ask what way this unit queries quicker speed of about 4 seconds statement of:? Luosidao (screwdriver) Grade: credit value: 100 Quantity Forum: MS-SQL Server database development issues Points: 100 Replies: 25 Posted : 2003-12-12 16: 34: 24z

Still analyzing yourself.

How to make your SQL run faster - people tend to fall into a misunderstanding when using SQL, that is, if it is too concerned that the result is correct, and ignore the possible performance differences between different implementation methods, this Performance Differences in large or complex database environments (such as online transaction 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 runtime 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 1. Unreasonable index design ---- Example: Table Record has 620000 lines, try under different indexes, the following SQL operation: ---- 1. Built on Date with a non-commission set Index Select Count (*) from Record Where Date> '19991201' And Date <'19991214'and Amount> 2000 (25 second) Select Date, SUM (Amount) from Record Group by Date (55 second) Select Count (*) from Record where date> '19990901' and place in ('bj', 'sh') (27 seconds) ---- Analysis: ---- Date has a large number of repetition values, under non-communical index, data is physically Randomly stored on the data page, when searching, you must perform a table scan to find all the rows within this range. ---- 2. A cluster index in Date Select Count (*) from Record Where Date> '19991201' And Date <'19991214' and Amount> 2000 (14 seconds) Select Date, Sum (Amount) from Record Group BY DATE (28 seconds) Select count (*) from record where date> '19990901' and place in ('bj', 'sh') (14 seconds) ---- Analysis: ---- Under the cluster index, Data is physically in order on the data page, the repetition value is also arranged together, and thus in the range lookup, you can first find the starting point of this range, and only scan the data pages within this range, avoid a wide range of scans, Improve the query speed.

---- 3. In Place, Date, Amount SELECT Count (*) from Record Where Date> '19991201' And Date <'19991214' And Amount> 2000 (26 second) Select Date, SUM (Amount) From Record Group by Date (27 second) Select count (*) from record where date> '19990901' and place in ('bj', 'sh') (<1 second) ---- Analysis: ---- this It is an unseasonful combination index because it is the leader, the first and second SQL do not reference the Place, so there is no use of the index; the third SQL uses the Place, and all columns included are included In the combined index, an index coverage is formed, so its speed is very fast. ---- 4. In Date, Place, Amount SELECT Count (*) from Record Where Date> '19991201' and And> 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 based on analysis and prediction of various queries. Generally, ---- 1. There are a large number of repetitive values, and often have a range of queries (Between,>, <,> =, <=) and the columns that occur, the columns that occur, can consider establish a cluster index; --- 2. Always access multiple columns simultaneously, and each column contains repetition values ​​to consider establish a combined index; ---- 3. Combined index should try to make critical queries to form index coverage, the front lead list must be the most frequent use Columns. 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: ---- Under the first connection condition, the best query is to make an access to an additional table, CARD The inner layer table, using the index on the Card, the number of I / O can be estimated by the following formula: ---- Outer Table Account 22541 page (Outer Table Account 19112 line * inner layer table Card) The 3 pages of the first line to find the first line) = 595907 times I / O ---- Under the second connection conditions, the best query scheme is to make the CARD out of the table, Account as the inner table, and use account The index, the number of I / O can be estimated by the following formula: 1944 page on the outer table Card (7896 line of the outer table Card), the inner layer table Account, the outer table is looking for every line. 4 pages) = 33528 times I / O --- visible, only a fully connected condition, the real best solution will be executed. ---- Summary: ---- 1. Before being executed before being actually executed, the query optimizer lists several groups of possible connection schemes and finds the best solution 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 - Use SET Showplanon to open the showplan option, you can see the connection order, use the information of the index; want to see more detailed information, you need to perform DBCC with SA roles (3604 310, 302).

Third, unmoderable WHERE clause ---- 1. Example: The columns in the following SQL condition statements have an appropriate index, but the execution speed is very slow: select * from record whereesubstring (CARD_NO, 1, 4) = '5378' (13 seconds) Select * from record whereamount / 30 <1000 (11 second) Select * from record whereconvert (char (10), DATE, 112) = '19991201' (10 seconds) ---- Analysis: - --- Any operating result of the column in the WHERE clause is calculated one by-quarter by SQL runtime, so it has to perform a table search without using the index above the column; if these results are compiled in the query Can be optimized by SQL optimizer, use indexes, avoid table search, so rewritten SQL into the following: Select * from record where card_no 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 SQL obviously faster! ---- 2. Example: Table stuff has 200,000 lines, 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 ---- directly calculated results, execution time Same as above! ---- Summary: ---- Visible, so-called Optimization, the WHERE clause utilizes an index, and the 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.

Reply to: zjcxc (Zou Jian) ​​() Reputation: 310 2003-12-13 16: 07: 15Z score: 0

Query optimization

Remember such a principle. 1. Calculation of fields will cause full mete scans. So, you can use: SELECT * FROM table where field = 1 Do not use: SELECT * FROM table Where field -1 = 0

2. The necessary indexes is important to increase the data processing speed. Therefore, for fields that are often sorted, conditional comparisons are required to establish an index. (Note distinguish between composite indexes and separate indexes)

3. Good at using stored procedures / views,

4. For query analysis statements that cannot be determined, copy it to the query analyzer. Press Ctrl L. Take a look at it. Each step is required to take advantage of time percentage. In the step of watching the table scan, do you utilize the index you created. If there are other query methods, analyze other query methods, by comparison the optimal query method.

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

New Post(0)