show processlist;

发现有200多个队列,

select cardnum from table where xxxid = 31 order by abc_time desc

这样的一个排序,把服务器堆住了。怎么回事? abc_time已经加了索引。



 


 


 







mysql对于排序,使用了两个变量来控制​​sort_buffer_size​​和 ​
Using the modified ​​filesort​​ algorithm, the tuples are longer than the pairs used in the original method, and fewer of them fit in the sort buffer (the size of which is given by ​​sort_buffer_size​​). As a result, it is possible for the extra I/O to make the modified approach slower, not faster. To avoid a slowdown, the optimization is used only if the total size of the extra columns in the sort tuple does not exceed the value of the ​​max_length_for_sort_data​​ system variable. (A symptom of setting the value of this variable too high is that you should see high disk activity and low CPU activity.)



 


评论 (1) • 链接 • 2011-11-28


  • 化,我还想说一点:

建立索引在匹配条件的字段建立会更好。比如你的这个SQL,应该在“xxxid”建立索引,而不应该在“abc_time”建立索引。因为索引(B+树、Hash、位图)都是为了加快查找速度,“desc”是排序用的,对它建立索引没有用处。所以去掉abc_time字段索引,建立xxxid字段索引。


 






首先你是用了xxxid作为条件去查询数据,xxxid字段最好要有索引,

其次你可以用explain select cardnum from table where xxxid = 31 order by abc_time desc 看看这条语句遍历的数据行数有多少,优化一下语句,

另外最好加一个limit,限制一下每次取数据的行数,

最后要看看这个队列是不是有大量的写入更新操作,可以做一下读写分离,因为写入更新操作太多也是会堵塞查询的。



不是您所需,查看更多相关问题与答案