当前位置:Gxlcms > mysql > MysqlSQL性能优化之order_MySQL

MysqlSQL性能优化之order_MySQL

时间:2021-07-01 10:21:17 帮助过:3人阅读

bitsCN.com

Mysql SQL性能优化之order

mysql的order在排序时,会被select出来的数据多少所影响,

数据列越多,排序越慢,为了说明这个问题,请看几个例子。

高效的查询

低性能:

select id,job_name from test_results where id <96836230 order by id desc limit 0,1;

1 rows fetched (390 ms)

高性能:

select id,job_name from test_results where id =(select id from test_results where id<110836230 order by id desc limit 0,1 ) limit 1;

1 rows fetched (281 ms)

高效的分页查询 www.bitsCN.com

低性能:

select id,job_name from test_results order by id desc limit 1200000,100;

100 rows fetched (34.616 sec)

高性能:

select id,job_name from test_results join (select id from test_results order by id desc limit 1300000,100) as t2 using(id);

100 rows fetched (5.460 sec)

接下来我们分析其原因,首先从mysql的执行顺序开始讲起,

执行顺序从上到下:

FROM < left_table >

ON < join_condition >

< join_type> JOIN < right_table>

WHERE < where_condition>

GROUP BY < group_by_list >

HAVING < having_condition>

SELECT

DISTINCT

ORDER BY < order_by_list>

所以得出结论,select出来的数据越多,排序越慢。

减少了order by中的排序数据,再去join或者=查询即可加快执行效率。

bitsCN.com

人气教程排行