【发布时间】:2020-10-02 04:45:32
【问题描述】:
我今天在执行查询时发现了一些奇怪的东西,我想知道这是怎么发生的。
以下是我的查询:
select sum(price) as total from table_a where testing_date = '2020-06-10'
此查询在搜索数据时需要 2-5 秒。现在我在查询中做了如下的小改动:
select sum(price) as total from table_a where date(testing_date) = '2020-06-10'
在这种情况下,查询需要 2-3 分钟。这里 testing_date 列数据采用 dateTime 格式,例如:2020-06-01 00:00:00
这里的总记录大小超过 700 万。
【问题讨论】:
-
您不再按索引数据过滤?
-
@ÁlvaroGonzález,我在列上添加了索引
-
您尚未共享您的数据库结构,但您似乎没有function based index 用于
DATE(testing_date)。如果您不想添加一个,则需要重写查询以使用现有的。 -
testing_date的数据类型是什么? -
@RickJames 数据类型的 testing_date 是
datetime
标签: mysql sql select query-optimization where-clause