Conditions joint index entry into force and failure to see here

This question is to examine the index entry into force, failure conditions. Like this kind of problem only makes sense in fact, I suggest that after the interview, when other partners, and more focused questions such problems than the general conceptual study of the problem much better.

 

Can probably examine candidates to write a program to do is focus on optimization, improve code quality and application performance it or just simple CV trouble.

Joint failure conditions index

Joint index called composite index. Index on two or more columns is called a composite index.

For the composite index: Mysql left to right using the index field, a query can use only a portion of the index, but only the leftmost portion. For example, the index is a key index (a, b, c ). Can support a | a, b | a, b, c 3 combinations to find, but not b, c to find out. When the left-most field is a constant reference, the index will be very effective.

With an additional column in the index, you can narrow the search, but using a two-column index as having two separate indices.

Similar structures and phonebook composite index, made up the names first and last name, phone book sort by last name first, and then by name for people with the same last name sort. If you know the name, the phone book is useful; if you know the first and last name, phone book is more useful, but if you only know the name does not name, the phone book will be of no use.

So when you create a composite index, you should carefully consider the order of the columns . When a search is performed only for the first few columns or perform a search for all the columns in the index, the composite index is useful; when any of the back row only perform a search, the composite index is not useful.

Such as: the establishment name, age, sex composite index.

    create table myTest(

         a int,

         b int,

         c int,

         KEY a(a,b,c)

    );

1

select * from myTest  where a=3 and b=5 and c=4; ----  abc顺序

abc three indexes are used in conditions where there, and play a role

2

select * from myTest  where  c=4 and b=6 and a=3;

Conditions of the order where there will be automatically optimized before mysql query, the same effect is to keep a

3

select * from myTest  where a=3 and c=7;

use the index a, b not used, it is not used in the index c effect

4

select * from myTest  where a=3 and b>7 and c=3; ---- b范围值,断点,阻塞了c的索引

It uses a, b also used, c is not used, this place b is the range of values, are also considered breakpoints, but uses its own index

5

select * from myTest  where b=3 and c=4;   --- 联合索引必须按照顺序使用,并且需要全部使用

Because a index is not used, so there are no access index effect bc

6

select * from myTest  where a>4 and b=7 and c=9;

a b uses is not used, c is not used

7

select * from myTest  where a=3 order by b;

use the index a, b results in the sorting index is also used in effect, a is below any section is sorted b

8

select * from myTest  where a=3 order by c;

a use of the index, but this place did not play the sort c effect, because the middle breakpoints, you can see using explain filesort

9

select * from mytable where b=3 order by a;

index b is not used, a sort not exert effect index

Finally talk about failure conditions index

  • Not do anything column index (calculated, function (automatic or manual) type conversion), the index will lead to failure of the steering full table scan

  • Storage engine can not use an index range conditions on the right column

  • Try to use a covering index (an index only access query (indexed columns and query columns consistent)) to reduce the select *

  • mysql is not equal in use (! = or <>) can not be used when the index will lead to a full table scan

  • is null, is not null can not use the index

  • Wildcard like to begin ( '% abc ...') mysql failure index table scan operation becomes perfected.

Problem: solved like method is not used when the index '% string%'?

[33] respectively to talk about the conditions and failure of the joint index into force

String failure without single quotation marks index

SELECT * from staffs where name='2000';  
-- 因为mysql会在底层对其进行隐式的类型转换

SELECT * from staffs where name=2000;  
--- 未使用索引

General recommendations

      High-quality video programming shangyepingtai.xin

  • For the key index, try to choose for the current query filter of a better index

  • In selecting a combination of the time index, the best current Query field filterability index field in order, a position far forward as possible.

  • In selecting a combination of the index when the index can contain as much as possible can choose where clause more fields in the current query

  • As far as possible by analyzing the wording of statistics and adjust query to achieve the purpose of selecting the appropriate index

 

Published 122 original articles · won praise 47 · views 30000 +

Guess you like

Origin blog.csdn.net/fengzongfu/article/details/105324215