作者:温安适
来源:https://my.oschina.net/floor/blog/4961581
引言
两个核心参数
全字段排序
Rowid排序
优化手段覆盖索引
例子
explain
SELECT order_id,pay_date FROM orders_detail WHERE order_id='1001' ORDER BY pay_date asc
ALTER TABLE `orders_detail` DROP INDEX `order_id`,ADD INDEX `order_id` (`order_id`, `pay_date`);
explain
SELECT order_id,pay_date FROM orders_detail WHERE order_id='1001' ORDER BY pay_date asc
特殊情况 Order by+ Limit
开启优化追踪
SET OPTIMIZER_TRACE="enabled=on",END_MARKERS_IN_JSON=off;
SET optimizer_trace_offset=-30, optimizer_trace_limit=30;
查看字段索引
SHOW INDEX FROM oc_order_online WHERE COLUMN_NAME='order_name';
执行order by+limit 查询语句
select * from `oc_order_online` order by `order_name` limit 20
SELECT * FROM INFORMATION_SCHEMA.OPTIMIZER_TRACE limit 30
总结
sort_buffer_size 决定内排,外排 max_length_for_sort_data 决定 全字段排序还是,rowid排序 覆盖索引是一种优化手段 Limit可能涉及优先队列排序