【发布时间】:2020-12-27 18:59:29
【问题描述】:
到目前为止,我有一个大约有 200 万行的表。该表将继续增长,因为每个月将添加大约 0.5-06 百万行。
例如,我有如下查询:
select `importer_name`, ROUND(SUM(total_value_usd_exchange), 2) AS top15_usd
from `importer_bills`
WHERE year(bill_of_entry_date)=2020
AND
Month(bill_of_entry_date)=3
group by `importer_name`
order by ROUND(SUM(total_value_usd_exchange), 2) desc limit 15 offset 0;
此查询当前需要9.98 秒才能执行。
跟随explain的输出:
1 SIMPLE importer_bills p0 ref idx_importer_bills_upwork_09,idx_importer_bills_year_month idx_importer_bills_year_month 5 const,const 1106762 100.00 Using index condition; Using temporary; Using filesort
-
idx_importer_bills_upwork_09 是
importer_name列上的索引。 -
idx_importer_bills_year_month 是
bill_of_entry_year和bill_of_entry_month的索引
我还添加了bill_of_entry_year的分区。
我尝试将上述查询替换为:
select `importer_name`, ROUND(SUM(total_value_usd_exchange), 2) AS top15_usd
from `importer_bills`
WHERE
bill_of_entry_year=2020
AND
bill_of_entry_month = 3
group by `importer_name`
order by ROUND(SUM(total_value_usd_exchange), 2) desc limit 15 offset 0;
这花费了9.01 秒。
explain的输出:
1 SIMPLE importer_bills p0 ref idx_importer_bills_upwork_09,idx_importer_bills_year_month idx_importer_bills_year_month 5 const,const 1106762 100.00 Using index condition; Using temporary; Using filesort
如何有一堆这样的查询基于比仅年份和月份更多的过滤器?有时只使用一年过滤器。仅 2M 行 10 秒是不可接受的。我该如何优化呢?
where 子句中使用的列总是会根据用户对过滤器的选择而改变,但可以考虑强制使用 YEAR 过滤器。也可能是一个月(但最好不要这样做)
【问题讨论】:
标签: mysql indexing relational-database partitioning database-partitioning