SQL optimization lecture (5)-those things about indexes

SQL column

Summary of SQL basics

SQL advanced knowledge summary

When it comes to indexing, everyone knows that it was born to improve query efficiency. But in the query process, how can we make our query statement use the index? Everyone will encounter this problem more or less. Today we will answer this question.

1
clustered index and non-clustered index

Indexes are generally divided into clustered indexes and non-clustered indexes.

The clustered index is very fast, but only one can be built, so try to build a clustered index for frequently used columns.

Although a non-clustered index is not as fast as a clustered index, it can be built multiple times, which is faster than a full table scan.

2How
to build an efficient index

A. Establish an index on the associated conditions

E.g:

SELECT * FROM T1

JOIN T2 ON T1.ORDER_ID=T2.ORDER_ID;

The two columns after the association condition ON can be indexed separately, so that the data that meets the association condition will be queried quickly.

B. Create an index on the conditional query

E.g:

SELECT * FROM T1

WHERE T1.PRICE>20;

Indexes can be created on the WHERE condition PRICE column.

Note: Indexes will not be used in the following situations

  • Operators are used on the index column,

For example: T1.PRICE*0.5>20, this kind of index will not be used

  • Use the function on the index column,

For example: UPPER(T1.ADDRESS)='NEWYORK', no index will be used

  • When using the index, there is a null value NULL,

For example: T1.ADDRESS IS NULL, then the index will not be used when querying

  • Character data is not quoted or indexed

For example: ORDER_ID was originally a character type, T1.ORDER_ID='112' will use the index, but if you remove the quotation marks, it becomes T1.ORDER_ID=112, the query statement will not report an error, but the index will not be used.

  • Or (OR), unequal (<>,!=) and NOT IN, etc. will not use indexes

  • Often used LIKE, except for post matching, other matches are not indexed

For example: T1.ADDRESS LIKE'NEW%', this takes the index, but like

T1.ADDRESS LIKE'%NEW%' and T1.ADDRESS LIKE'%NEW' are not indexed

Finally, if the query optimizer judges that the full table scan is faster than the index, it will not use the index.

C. Principles of indexing

  • Columns that are not frequently written and updated are suitable for indexing

  • Frequently queried columns are suitable for indexing

  • Indexes can be established for those with less repetitive data

D. The magical effect of the joint index

A joint index is a combination of several columns to form an index, which will have unexpected effects compared to a single-column index in the WHERE condition.

E.g:

SELECT * FROM T1 WHERE T1.CITY='Beijing' AND T1.DISTR='Haidian District';

At this time, the columns CITY and DISR are established as a joint index, the effect will be better.

Note: The joint index needs to go in order. If an index in the middle cannot be used, the columns after it will not use the index.

E.g:

SELECT * FROM T1

WHERE T1.CITY='Beijing'

AND LEFT(T1.DISTR,3)='Haidian District'

AND T1.ROAD='#10'

If we create CITY, DISR, and ROAD as a joint index, due to the pre-index rules, only CITY will be indexed. The following DISR uses a function, and the index becomes invalid. The final ROAD column will also become invalid due to the invalidation of DISR. Just remember it here.

3
What is not suitable for indexing

Since it takes time to create and maintain an index, the time increases in proportion to the increase in data; it needs to occupy physical space; when maintaining the data in the table, the index should also be maintained, which reduces the maintenance of the data speed. Based on these shortcomings, the following situations are not suitable for indexing

  • For columns that are rarely used or referenced in the query process, indexes should not be created.

  • For columns with very few data values, indexes should not be created, such as gender.

  • For columns defined as image, text, and bit data types, indexes should not be created.

  • When the modification performance is much greater than the retrieval performance, indexes should not be built.

  • Many duplicate values ​​are not suitable for indexing.

Well, today’s index will stop here. Friends who are interested in optimization can join our QQ group or WeChat group, and we can exchange and learn together.

Guess you like

Origin blog.51cto.com/15057820/2656471