1: index type
1.1B-tree index
Note: It is called btree index. From a large perspective, it uses a balanced tree, but in terms of specific implementation, each engine is slightly different.
For example, strictly speaking, the NDB engine uses T-tree
In Myisam, innodb, B-tree index is used by default
But abstract ---B-tree system, it can be understood as "sorted fast search structure".
1.2 hash index
In the memory table, the default is the hash index, and the theoretical query time complexity of hash is O(1)
Question: Since hash lookup is so efficient, why not use hash indexes?
answer:
1: The result of the hash function calculation is random, if the data is placed on the disk,
If the primary key is id as an example, then as the id grows, the row corresponding to the id is randomly placed on the disk.
2: The range query cannot be optimized.
3: The prefix index cannot be used. For example, in the btree, the value of the field column is "hellopworld", and the index is added
Query xx=helloword, you can naturally use the index, xx=hello, you can also use the index. (left prefix index)
Because hash('helloword'), and hash('hello'), the relationship between the two is still random
4: Sorting cannot be optimized either.
5: The row must be returned. That is to say, to get the data position through the index, you must go back to the table to get the data
2: Common misunderstandings of btree indexes
2.1 Add indexes to the columns commonly used in where conditions
Example: where cat_id=3 and price>100 ;//Query the 3rd column, products above 100 yuan
Error: index is added to cat_id, and price.
Wrong: Only cat_id or Price index can be used, because it is an independent index, and only one can be used at the same time.
2.2 After creating an index on multiple columns, which column is queried, the index will play a role
Error: On a multi-column index, the index plays a role and needs to meet the left prefix requirement.
Take index(a,b,c) as an example,
statement |
Does the index work |
Where a=3 |
Yes, only column a is used |
Where a=3 and b=5 |
Yes, columns a and b are used |
Where a=3 and b=5 and c=4 |
Yes, using abc |
Where b=3 / where c=4 |
no |
Where a=3 and c=4 |
A column can play an index, c can not |
Where a=3 and b>10 and c=7 |
A can use, b can use, C can not use |
同上,where a=3 and b like ‘xxxx%’ and c=7 |
A works, B works, C doesn't work |
For ease of understanding, assume that ABC is 10 meters long and the river is 30 meters wide.
The full value index is that the board is 10 meters long,
Like, left prefix and range query, the board is 6 meters long,
Splice it yourself, if you can cross the other side of the river, you will know whether the index can be used.
In the above example, wherea=3 and b>10, and c=7,
A board is 10 meters long, and the index of column A plays a role
The A board is normally connected to the B board, and the B board index plays a role
The B board is short, the C board cannot be connected, and the index of the C column does not work.
Multi-column index classic topic:
http://www.zixue.it/thread-9218-1-4.html
Suppose a table has a joint index (c1, c2, c3, c4) - only the c1, c2, c3 part of the joint index can be used
A where c1=x and c2=x and c4>x and c3=x
B where c1=x and c2=x and c4=x order by c3
C where c1=x and c4= x group by c3,c2
D where c1=x and c5=x order by c2,c3
E where c1=x and c2=x and c5=? order byc2,c3
create table t4 (
c1 tinyint(1) not null default 0,
c2 tinyint(1) not null default 0,
c3 tinyint(1) not null default 0,
c4 tinyint(1) not null default 0,
c5 tinyint(1) not null default 0,
index c1234(c1,c2,c3,c4)
);
insert into t4 values (1,3,5,6,7),(2,3,9,8,3),(4,3,2,7,5);
For A:
c1=x and c2=x and c4>x and c3=x <==等价==> c1=x and c2=x and c3=x and c4>x
So c1,c2,c3,c4 can be used. As follows:
mysql> explain select * from t4 where c1=1 andc2=2 and c4>3 and c3=3 \G
*************************** 1. row***************************
id: 1
select_type: SIMPLE
table: t4
type: range
possible_keys: c1234
key: c1234
key_len: 4 #It can be seen that c1,c2,c3,c4 indexes are used
ref: NULL
rows: 1
Extra: Using where
对于B: select *from t4 where c1=1 and c2=2 and c4=3 order by c3
c1 , c2 index is used, and c3 is sorted based on the index used by c2, so no additional sorting is required.
And c4 didn't work.
mysql> explain select * from t4 where c1=1 andc2=2 and c4=3 order by c3 \G
*************************** 1. row***************************
id: 1
select_type: SIMPLE
table: t4
type: ref
possible_keys: c1234
key: c1234
key_len: 2
ref: const,const
rows: 1
Extra: Using where
1 row in set (0.00 sec)
mysql> explain select * from t4 where c1=1 andc2=2 and c4=3 order by c5 \G
***************************1. row ***************************
id: 1
select_type: SIMPLE
table: t4
type: ref
possible_keys: c1234
key: c1234
key_len: 2
ref: const,const
rows: 1
Extra: Using where; Using filesort
1 row in set (0.00 sec)
For C: Only the c1 index is used, because the order of group byc3, c2 cannot use the c2, c3 index
mysql> explain select * from t4 where c1=1 andc4=2 group by c3,c2 \G
*************************** 1. row***************************
id:1
select_type: SIMPLE
table: t4
type: ref
possible_keys: c1234
key: c1234
key_len: 1 #Only c1 is used, because c3 is used first and then c2 is used for grouping, resulting in c2, c3 indexes do not work
ref: const
rows: 1
Extra: Using where; Using temporary; Using filesort
1 row in set (0.00 sec)
mysql> explain select * from t4 where c1=1 andc4=2 group by c2,c3 \G
***************************1. row ***************************
id: 1
select_type: SIMPLE
table: t4
type: ref
possible_keys: c1234
key: c1234
key_len: 1
ref: const
rows: 1
Extra: Using where
1 row in set (0.00 sec)
D statement: Based on the determination of C1, c2 is ordered, and C3 is ordered under C2, so c2, c3 play the role of sorting.
Therefore, filesort is not used
mysql> explain select * from t4 where c1=1 andc5=2 order by c2,c3 \G
*************************** 1. row***************************
id: 1
select_type: SIMPLE
table: t4
type: ref
possible_keys: c1234
key: c1234
key_len: 1
ref: const
rows: 1
Extra: Using where
1 row in set (0.00 sec)
E: This sentence is equivalent to elect * from t4 where c1=1 and c2=3 and c5=2 order by c3;
Because the value of c2 is fixed, it is not considered when participating in sorting
mysql> explain select * from t4 where c1=1 andc2=3 and c5=2 order by c2,c3 \G
***************************1. row ***************************
id: 1
select_type: SIMPLE
table: t4
type: ref
possible_keys: c1234
key: c1234
key_len: 2
ref: const,const
rows: 1
Extra: Using where
1 row in set (0.00 sec)
An interview question:
There is a product table, there is a primary key, goods_id, column column cat_id, price price
Said: The price column has been indexed, but the query by price is still very slow,
What could be the reason and how to solve it?
Answer: In the actual scenario, there are many categories of goods on an e-commerce website. It is very rare to check the goods by price directly among all the goods. Generally, customers come to the category and then check them again.
Correction: Remove the index of the separate Price column and add a (cat_id, price) composite index
Inquire again.