【问题标题】:Avoid "filesort" on UNION RESULT在 UNION RESULT 上避免“文件排序”
【发布时间】:2016-04-03 18:20:46
【问题描述】:

子查询1:

SELECT * from big_table
where category = 'fruits' and name = 'apple'
order by yyyymmdd desc

解释:

table       |   key           |   extra
big_table   |   name_yyyymmdd |   using where

看起来不错!

子查询 2:

SELECT * from big_table
where category = 'fruits' and (taste = 'sweet' or wildcard = '*')
order by yyyymmdd desc

解释:

table       |   key               |   extra
big_table   |   category_yyyymmdd |   using where

看起来不错!

现在如果我将它们与 UNION 结合起来:

SELECT * from big_table
where category = 'fruits' and name = 'apple'

UNION

SELECT * from big_table
where category = 'fruits' and (taste = 'sweet' or wildcard = '*')

Order by yyyymmdd desc

解释:

table       |   key      |   extra
big_table   |   name     |   using index condition, using where
big_table   |   category |   using index condition
UNION RESULT|   NULL     |   using temporary; using filesort

不太好,它使用文件排序。

这是一个更复杂查询的精简版,以下是关于 big_table 的一些事实:

  • big_table 有 10M + 行
  • 有 5 个独特的“类别”
  • 有 5 种独特的“味道”
  • 大约有 10,000 个唯一的“名称”
  • 大约有 10,000 个独特的“yyyymmdd”
  • 我已经在每个字段上创建了单个索引,加上复合 idx,例如 yyyymmdd_category_taste_name,但 Mysql 没有使用它。

【问题讨论】:

    标签: mysql indexing query-optimization filesort


    【解决方案1】:
    SELECT * FROM big_table
        WHERE category = 'fruits'
          AND (  name = 'apple'
              OR taste = 'sweet'
              OR wildcard = '*' )
        ORDER BY yyyymmdd DESC
    

    并且有INDEX(catgory) 或一些以category 开头的索引开始。但是,如果超过大约 20% 的表是 category = 'fruits' 可能会决定忽略索引并简单地进行表扫描。 (既然你说只有 5 个类别,我怀疑优化器会正确地避开索引。)

    或者这个可能是有益的:INDEX(category, yyyymmdd),按这个的顺序。

    UNION 必须进行排序(在磁盘上的内存中,不清楚),因为它无法按所需顺序获取行。

    复合索引INDEX(yyyymmdd, ...) 可用于避免“文件排序”,但不会使用yyyymmdd 之后的任何列。

    在构造复合索引时,开始将任何WHERE 列比较'='。之后,您可以添加一个范围或group byorder byMore details.

    UNION 通常是避免慢速 OR 的好选择,但在这种情况下,它需要三个索引

    INDEX(category, name)
    INDEX(category, taste)
    INDEX(category, wildcard)
    

    除非您添加 LIMIT,否则添加 yyyymmdd 将无济于事。

    查询将是:

    ( SELECT * FROM big_table WHERE category = 'fruits' AND name = 'apple' )
    UNION DISTINCT
    ( SELECT * FROM big_table WHERE category = 'fruits' AND taste = 'sweet' )
    UNION DISTINCT
    ( SELECT * FROM big_table WHERE category = 'fruits' AND wildcard = '*' )
    ORDER BY yyyymmdd DESC
    

    添加限制会更麻烦。先把yyyymmdd加在三个复合索引的end上,然后

    ( SELECT ... ORDER BY yyyymmdd DESC LIMIT 10 )
    UNION DISTINCT
    ( SELECT ... ORDER BY yyyymmdd DESC LIMIT 10 )
    UNION DISTINCT
    ( SELECT ... ORDER BY yyyymmdd DESC LIMIT 10 )
    ORDER BY yyyymmdd DESC  LIMIT 10
    

    添加 OFFSET 会更糟。

    另外两种技术——“覆盖”索引和“惰性查找”可能会有所帮助,但我对此表示怀疑。

    另一种技术是将所有单词放在同一列中并使用FULLTEXT 索引。但这可能是有问题的,原因有很多。

    【讨论】:

    • 还有一件事......“文件排序”不是邪恶的;查询的复杂度是。
    【解决方案2】:

    这也必须在没有 UNION 的情况下工作

    SELECT * from big_table
    where 
        ( category = 'fruits' and name = 'apple' )
        OR
        ( category = 'fruits' and (taste = 'sweet' or wildcard = '*')
    ORDER BY yyyymmdd desc;
    

    【讨论】:

    • 我的查询首先没有 UNION,但是由于某种原因,在第二个条件(通配符)中找到匹配行的时间太长。
    • 请向我们发布您的查询的完整说明。可能没有带有字段味道或通配符的 INDEX (COMPOSITE)
    猜你喜欢
    • 2019-11-20
    • 1970-01-01
    • 2013-07-07
    • 1970-01-01
    • 2021-10-10
    • 2012-03-17
    • 2015-07-04
    • 2017-08-07
    • 1970-01-01
    相关资源
    最近更新 更多