您好,欢迎来到易妖游戏网。
搜索
您的当前位置:首页MysqlSQL性能优化之order_MySQL

MysqlSQL性能优化之order_MySQL

来源:易妖游戏网


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

Copyright © 2019- vipyiyao.com 版权所有 湘ICP备2023022495号-8

违法及侵权请联系:TEL:199 18 7713 E-MAIL:2724546146@qq.com

本站由北京市万商天勤律师事务所王兴未律师提供法律服务