First, the joint index test
Note: Mysql version 5.7.20
Create a test table t_mobilesms_11 (table number recorded as 60000):
We userId
, mobile
, billMonth
three fields to add a joint index!
Then explain
view an execution plan to observe the utilization index:
1. query conditions userid
EXPLAIN SELECT * FROM `t_mobilesms_11` WHERE userid='2222'
We can key
see that effective joint index
2. Query conditions mobile
EXPLAIN SELECT * FROM `t_mobilesms_11` WHERE mobile='13281899972'
We can see the joint index is invalid
3. Query conditions billMonth
EXPLAIN SELECT * FROM `t_mobilesms_11` WHERE billMonth='2018-04'
Joint index is invalid
4. to query userid and mobile
EXPLAIN SELECT * FROM `t_mobilesms_11` WHERE userid='2222' AND mobile='13281899972'
Effective joint index
5. Query conditions mobile and userid
EXPLAIN SELECT * FROM `t_mobilesms_11` WHERE mobile='13281899972' AND userid='2222'
Swap the order of the query conditions on the basis of 4 found joint index is still valid
6. The query was userid or mobile
EXPLAIN SELECT * FROM `t_mobilesms_11` WHERE userid='2222' OR mobile='13281899972'
To and
replace or
, find the joint index is invalid!
7. Query conditions userid and billMonth
EXPLAIN SELECT * FROM `t_mobilesms_11` WHERE userid='2222' AND billMonth='2018-04'
These two conditions are located joint index position of the first and third test joint index is still valid!
8. query conditions mobile and billMonth
EXPLAIN SELECT * FROM `t_mobilesms_11` WHERE mobile='13281899972' AND billMonth='2018-04'
The second and third are located in these two conditions index joint position, joint index found invalid!
9. query conditions userid and mobile and billMonth
EXPLAIN SELECT * FROM `t_mobilesms_11` WHERE userid='2222' AND mobile='13281899972' AND billMonth='2018-04'
Query with all the conditions, joint index effective! (Of course, this is the most orthodox usage ah!)
Second, a separate index test
Create three separate indexes:
1. query conditions userid and mobile and billMonth
EXPLAIN SELECT * FROM `t_mobilesms_11` WHERE userid='2222' AND mobile='13281899972' AND billMonth='2018-04'
We found three separate index only userid
valid (the first position for the query), the other two have no access to
2. Query conditions mobile and billMonth
EXPLAIN SELECT * FROM `t_mobilesms_11` WHERE mobile='13281899972' AND billMonth='2018-04'
Here we find two query only mobile
effective (also the location for the first query), the latter is invalid
3. Query conditions userid or mobile
EXPLAIN SELECT * FROM `t_mobilesms_11` WHERE userid='2222' OR mobile='13281899972'
This time put and
into or
, and found two query are indexed to spend!
Popular understanding:
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.
所以说创建复合索引时,应该仔细考虑列的顺序。对索引中的所有列执行搜索或仅对前几列执行搜索时,复合索引非常有用;仅对后面的任意列执行搜索时,复合索引则没有用处。
多个单列索引在多条件查询时只会生效第一个索引!所以多条件联合查询时最好建联合索引!
最左前缀原则:
顾名思义是最左优先,以最左边的为起点任何连续的索引都能匹配上,
注:如果第一个字段是范围查询需要单独建一个索引
注:在创建联合索引时,要根据业务需求,where子句中使用最频繁的一列放在最左边。这样的话扩展性较好,比如 userid
经常需要作为查询条件,而 mobile
不常常用,则需要把 userid
放在联合索引的第一位置,即最左边
同时存在联合索引和单列索引(字段有重复的),这个时候查询mysql会怎么用索引呢?
这个涉及到mysql本身的查询优化器策略了,当一个表有多条索引可走时, Mysql 根据查询语句的成本来选择走哪条索引;
有人说where查询是按照从左到右的顺序,所以筛选力度大的条件尽量放前面。网上百度过,很多都是这种说法,但是据我研究,mysql执行优化器会对其进行优化,当不考虑索引时,where条件顺序对效率没有影响,真正有影响的是是否用到了索引!
联合索引本质:
当创建(a,b,c)联合索引时,相当于创建了(a)单列索引,(a,b)联合索引以及(a,b,c)联合索引
想要索引生效的话,只能使用 a和a,b和a,b,c三种组合;当然,b,a也是好使的,因为sql会对它优化
注:这个可以结合上边的 通俗理解 来思考!
其他知识点:
1 Required index fields, where you want to condition
2, add a small amount of data fields do not need to index; because there is a certain cost to build the index, if the small amount of data it is not necessary to build the index (but slower speed)
3, where if conditions are OR relationship, plus the index does not work
4, joint index than the index were built more advantages for each column, because the more the more accounted for indexing disk space, update the data in the time more slowly. In addition the establishment of a multi-column index, the order is to be noted, should be strictly index on the front, such screening efforts will be greater and more efficient.