zoukankan      html  css  js  c++  java
  • MYSQL Sorting result 把队列堆满了,该怎么办?

    show processlist;
    发现有200多个队列,
    select cardnum from table where xxxid = 31 order by abc_time desc

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

     
     
     
    评论 (2) •  • 链接 • 2011-11-28 
     
    • 0
      xxxid有索引吗? – 何远伦 2011-11-28
    • 0
      有索引啊~~~ – 尚兴跃 2011-11-30
    3个答案

     

    mysql对于排序,使用了两个变量来控制sort_buffer_size和 max_length_for_sort_data。可以通过增大这两个属性值加快ORDER BY或GROUP BY操作。

    其中,max_length_for_sort_data更用于优化filesort算法:
    mysql的filesort有两个方法,mysql 4.1之前是使用方法A,之后版本会使用改进的算法B,但使用方法B的前提是列长度的值小于max_length_for_sort_data

    filesort的两个方法介绍以及优化方式:http://forge.mysql.com/wiki/MySQL_Internals_Algorithms

    How MySQL Does Sorting (filesort)
    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.)

    胡加杰
    编辑于 2011-11-28
     
    评论 (1) • 链接 • 2011-11-28
    • 0
      我增加了read_buffer_size = X M
      read_rnd_buffer_size = X M
      排序的内容太多,也影响到了速度。
      这两个参数,也起到了不错的效果。
       – 尚兴跃 2011-11-30
     

    同意@胡加杰的从MySQL配置上去优化,我还想说一点:
    建立索引在匹配条件的字段建立会更好。比如你的这个SQL,应该在“xxxid”建立索引,而不应该在“abc_time”建立索引。因为索引(B+树、Hash、位图)都是为了加快查找速度,“desc”是排序用的,对它建立索引没有用处。所以去掉abc_time字段索引,建立xxxid字段索引。

    评论 (0) • 链接 • 2011-11-29
     

    首先你是用了xxxid作为条件去查询数据,xxxid字段最好要有索引,
    其次你可以用explain select cardnum from table where xxxid = 31 order by abc_time desc 看看这条语句遍历的数据行数有多少,优化一下语句,
    另外最好加一个limit,限制一下每次取数据的行数,
    最后要看看这个队列是不是有大量的写入更新操作,可以做一下读写分离,因为写入更新操作太多也是会堵塞查询的。

    何远伦
    编辑于 2011-11-29
     
    评论 (0) • 链接 • 2011-11-29
     

     

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

  • 相关阅读:
    LeetCode对撞指针汇总
    167. Two Sum II
    215. Kth Largest Element in an Array
    2018Action Recognition from Skeleton Data via Analogical Generalization over Qualitative Representations
    题解 Educational Codeforces Round 84 (Rated for Div. 2) (CF1327)
    题解 JZPKIL
    题解 八省联考2018 / 九省联考2018
    题解 六省联考2017
    题解 Codeforces Round #621 (Div. 1 + Div. 2) (CF1307)
    题解Codeforces Round #620 (Div. 2)
  • 原文地址:https://www.cnblogs.com/seasonzone/p/3469413.html
Copyright © 2011-2022 走看看