盘点开发中那些常用的MySQL优化

北京中科白癜风医院坑不坑 https://m-mip.39.net/czk/mipso_5154116.html

做家:jian

  

MySQL能够行使一个索引来满意ORDERBY子句的排序,而不需求额外的排序,不过需求满意如下几个前提:

(1)WHERE前提和OREDRBY行使雷同的索引:即key_part1与key_part2是复合索引,where中行使复合索引中的key_part1

SELECT*FROMuserWHEREkey_part1=1ORDERBYkey_part1DESC,key_part2DESC;

(2)并且ORDERBY递次和索引递次雷同:

SELECT*FROMuserORDERBYkey_part1,key_part2;

(3)并且要末都是升序要末都是降序:

SELECT*FROMuserORDERBYkey_part1DESC,key_part2DESC;

但如下几种环境则不行使索引:

(1)ORDERBY中搀和ASC和DESC:

SELECT*FROMuserORDERBYkey_part1DESC,key_part2ASC;

(2)盘诘行的关键字与ORDERBY所行使的不雷同,即WHERE后的字段与ORDERBY后的字段是不同样的

SELECT*FROMuserWHEREkey2=‘xxx’ORDERBYkey1;

(3)ORDERBY对不同的关键字行使,即ORDERBY后的关键字不雷同

SELECT*FROMuserORDERBYkey1,key2;5、OR的优化

当MySQL行使OR盘诘时,假若要操纵索引的话,一定每个前提列都使自力索引,而不是复合索引(多列索引),才略保证行使到盘诘的时分行使到索引。

譬如咱们新建一张用户讯息表user_info

mysqlselect*fromuser_info;+---------+--------+----------+-----------+

user_id

idcard

name

address

+---------+--------+----------+-----------+

1

Zhangsan

Kunming

2

Lisi

Beijing

3

Wangwu

Shanghai

4

Lijian

Guangzhou

+---------+--------+----------+-----------+4rowsinset

以后创立ind_name_id(user_id,name)复合索引、id_index(id_index)自力索引,idcard主键索引三个索引。

mysqlshowindexfromuser_info;+-----------+------------+-------------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+

Table

Non_unique

Key_name

Seq_in_index

Column_name

Collation

Cardinality

Sub_part

Packed

Null

Index_type

Comment

Index_


转载请注明:http://www.aierlanlan.com/rzdk/1038.html