mysql实战45讲 笔记order by的工作

order by: 使用explain后再extra 中有

using index 表示使用了覆盖索引

using filesort表示的就是需要排序,

Using temporary 需要使用临时表

分配一个内存sort_buffer用于排序,并不是所有的order by都需要排序的,其原因是原来的数据都是无序的。order by id 或者order by的字段与where中的字段做成一个联合索引(company_id,product_id)可以避免如下,有时候还可以将要返回的字段均作为联合索引,避免回表。

1.全字段排序:内存够

2.rowId排序

如果order by后面加上limit num,num是小值,在5.6以上会使用优先队列进行排序。

获取随机数据:

发布了85 篇原创文章 · 获赞 6 · 访问量 1万+

猜你喜欢

转载自blog.csdn.net/New_CJ/article/details/90665917