【发布时间】: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