|
因此按照详细营业环境成立多列的连系索引是须要的,那么我们来试试吧。
- alter table SC drop index sc_c_id_index;alter table SC drop index sc_score_index;create index sc_c_id_score_index on SC(c_id,score);
执行上述查询语句,耗损时刻为:0.007s,这个速率照旧可以吸取的
执行打算:

该语句的优化暂且告一段落
总结:
- mysql嵌套子查询服从确实较量低
- 可以将其优化成毗连查询
- 毗连表时,可以先用where前提对表举办过滤,然后做表毗连(固然mysql会对连表语句做优化)
- 成立吻合的索引,须要时成立多列连系索引
- 学会说明sql执行打算,mysql会对sql举办优化,以是说明执行打算很重要
索引优化
上面讲到子查询的优化,以及怎样成立索引,并且在多个字段索引时,别离对字段成立了单个索引
后头发明着实成立连系索引服从会更高,尤其是在数据量较大,单个列区分度不高的环境下。
单列索引
查询语句如下:
- select * from user_test_copy where sex = 2 and type = 2 and age = 10
索引:
- CREATE index user_test_index_sex on user_test_copy(sex);
- CREATE index user_test_index_type on user_test_copy(type);
- CREATE index user_test_index_age on user_test_copy(age);
别离对sex,type,age字段做了索引,数据量为300w,查询时刻:0.415s
执行打算:

发明type=index_merge
这是mysql对多个单列索引的优化,对功效集回收intersect并集操纵
多列索引
我们可以在这3个列上成立多列索引,将表copy一份以便做测试
- select * from user_test where sex = 2 and type = 2 and age = 10
查询语句:
select * from user_test where sex = 2 and type = 2 and age = 10执行时刻:0.032s,快了10多倍,且多列索引的区分度越高,进步的速率也越多
执行打算:

最左前缀
多列索引尚有最左前缀的特征,执行一下语句:
- select * from user_test where sex = 2select * from user_test where sex = 2 and type = 2select * from user_test where sex = 2 and age = 10
城市行使到索引,即索引的第一个字段sex要呈此刻where前提中
索引包围
(编辑:湖南网)
【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!
|