sql optimization tips

Embedded in the program line by line of SQL statements, if you use some optimization tips, will be able to achieve a multiplier effect.

TipsComparison operators can use "=" would not "<>"

"=" Increases the chance of using the index.

Tipsknowing that only one query results, then please use the "LIMIT 1"

"LIMIT 1" to avoid full table scan to find the corresponding results of the scan will not continue.

Tipscolumn select the appropriate data type

Can TINYINT would not SMALLINT , can SMALLINT would not INT , you know the truth, disk and memory consumption as small as possible thing.

TipsThe large DELETE , UPDATE or INSERT query becomes more small queries

You can write a few lines, hundreds of lines of SQL statements are not forced to appear very high grid? However, in order to achieve better performance and better data control, you can turn them into multiple small queries.

Tipsusing UNION ALL instead of the UNION , allowing repeat if the result set it

Because UNION ALL not heavy, more efficient than the UNION .

Tipsmultiple times to get the same result set, please keep the SQL statement is consistent

The aim is to make full use of the query cache. For example, according to the geographical and product id Product Pricing, for the first time used: the SELECT. Price from the Order and the WHERE id = 123456 Region = 'beijing';

Then the same query a second time, please keep the consistency of the above statements, such as not to where the statement inside the id and the region position reverse the order.

TipsTry to avoid using "SELECT *"

If you do not query all the columns in the table, try to avoid using the SELECT * , because it will be a full table scan, can not effectively use the index, increasing the burden on the database server, and network between it and the application client IO overhead.

Technical Qiao 8 WHERE clause is possible inside the column index

Just "try" Oh, not to say that all of the columns. Local conditions, adjusted according to actual situation, because sometimes too many indexes will decrease performance.

Tips 9 JOIN clause inside the column is indexed as much as possible

Also just "try" Oh, not to say that all of the columns.

Tips 10 ORDER BY column is indexed as much as possible

If the ORDER BY column is indexed, the performance will be better.

Tips 11  using LIMIT implement paging logic

Not only improve performance, while reducing unnecessary network traffic between the database and application.

Tips 12  using EXPLAIN keyword to view an execution plan

EXPLAIN can check the index usage and scanning lines.

other

There are many excellent tune SQL method, the same results can have a variety of different ways to search. In fact, the best way is most close to the real data sets and hardware environment by testing in a development environment, and then posted to the production environment.

 

Guess you like

Origin www.cnblogs.com/nnnnmmmm/p/11769950.html