MySQL 查询需要很长时间才能返回一个巨大的表

标签 mysql database performance indexing

我有一个非常大的表,下面的代码需要 990 秒。去完成。 bdateitype 已建立索引。我还需要优化/更改哪些内容?

SELECT s, count(*) as total
FROM  `mt_ex_15` 
WHERE bdate > '2014-10-01' and bdate < '2014-11-01'
and itype = '3'
group by s
order by total desc

编辑:这是解释

id  select_type table   type    possible_keys   key key_len ref rows    Extra   
1   SIMPLE  mt_ex_15    ref itype,bdate,s   itype   2   const   44157686    Using where; Using temporary; Using filesort    

编辑:我认为我需要优化我的数据库或 my.cnf,因为即使下面的查询也花费了 40 秒。

SELECT count(*) as total
FROM  `mt_ex_15` 
WHERE bdate > '2015-02-01' and bdate < '2015-03-01'

解释如下:

 id     select_type     table   type    possible_keys   key     key_len     ref     rows    Extra   
1   SIMPLE  mt_ex_15    range   bdate   bdate   3   NULL    4494019     Using where; Using index

最佳答案

对于此查询:

SELECT s, count(*) as total
FROM  `mt_ex_15` 
WHERE bdate > '2014-10-01' and bdate < '2014-11-01' and itype = '3'
group by s
order by total desc

最佳索引是mt_ex_15(itype, bdate, s)。引擎应该能够充分利用 where 子句的索引。此外,这是一个覆盖索引,因此该查询不需要触及原始数据。

如果您有所有可用“s”值的列表,您可以将其作为相关子查询来执行:

select s.*,
       (select count(*)
        from mt_ex_15 m
        where m.s = s.s and m.itype = 3 and m.bdate > '2014-10-01' and m.bdate < '2014-11-01'
       ) total
from s
having total > 0 -- using a convenient MySQL extension
order by total desc;

此查询的最佳索引是 mt_ex_15(s, itype, bdate)

注意:如果itype确实是一个整数,则应该删除常量周围的引号。它们具有误导性。

关于MySQL 查询需要很长时间才能返回一个巨大的表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29806847/

相关文章:

mysql - 光标未打开错误

mysql - SQL查询两张表(1-M)

mysql - where 子句中的数据库名称

mysql - 组合两个 MySQL 查询以显示每个列的一个唯一值和一个 AVG 值

c - openmp 在指针数组和指向数组的指针之间的性能差异有什么问题?

windows - 实现虚拟文件系统时可以安全忽略的各种系统文件列表

php - 我应该使用更受约束的 SQL 查询还是在代码中处理结果集?

mysql - 关于 WHERE 子句条件顺序的性能优势

c# - 数据库事务中的并发线程导致 .NET Core EF Core 中的重大延迟

javascript - 获取错误 Uncaught ReferenceError : firebase is not defined