使用 DateTime WHERE 子句查询大型 table
Query on large table using DateTime WHERE clause
我正在尝试从相当大的 table 中查询数据,大约有 9800 万行,在 WHERE 子句中使用日期时间列。大约需要 12 分钟才能完成——这显然不是 acceptable。查询很简单:
SELECT ID, DateTime1, DateTime2, Value1, Value2
FROM dataTable
WHERE DateTime1 >= '2017-05-15 09:00' AND
DateTime1 <= '2017-05-15 09:30'
table 具有以下结构:
Column Name | DataType
-------------------------
ID | float
DateTime1 | datetime
DateTime2 | datetime
Value1 | float
Value2 | varchar(20)
table有以下索引:
Nonclustered: DateTime1, DateTime2, ID, Value2
在 SQL 服务器中:
您的索引不包含 Value1
,因此如果它使用您现有的索引,它必须从 table 中为每一行检索该列。
您可以像这样创建覆盖索引(包括查询所需的所有列):
create nonclustered index ix_dataTable_DateTime1_cover
on dbo.dataTable (DateTime1)
include (Id, DateTime2, Value1, Value2);
或修改现有索引以包含 Value1
。
另外,检查执行计划。如果仍有性能问题,请使用 Paste The Plan @ brentozar.com here are the instructions: How to Use Paste the Plan.
分享您的执行计划
你的位置只有DateTime1,所以只为这一列建立索引。
一个复杂的索引就像把一个字符串的所有部分放在一起,比如你的 DateTime1+DateTime2+ID+Value2。哦,是的,那一定很慢。
我正在尝试从相当大的 table 中查询数据,大约有 9800 万行,在 WHERE 子句中使用日期时间列。大约需要 12 分钟才能完成——这显然不是 acceptable。查询很简单:
SELECT ID, DateTime1, DateTime2, Value1, Value2
FROM dataTable
WHERE DateTime1 >= '2017-05-15 09:00' AND
DateTime1 <= '2017-05-15 09:30'
table 具有以下结构:
Column Name | DataType
-------------------------
ID | float
DateTime1 | datetime
DateTime2 | datetime
Value1 | float
Value2 | varchar(20)
table有以下索引:
Nonclustered: DateTime1, DateTime2, ID, Value2
在 SQL 服务器中:
您的索引不包含 Value1
,因此如果它使用您现有的索引,它必须从 table 中为每一行检索该列。
您可以像这样创建覆盖索引(包括查询所需的所有列):
create nonclustered index ix_dataTable_DateTime1_cover
on dbo.dataTable (DateTime1)
include (Id, DateTime2, Value1, Value2);
或修改现有索引以包含 Value1
。
另外,检查执行计划。如果仍有性能问题,请使用 Paste The Plan @ brentozar.com here are the instructions: How to Use Paste the Plan.
分享您的执行计划你的位置只有DateTime1,所以只为这一列建立索引。 一个复杂的索引就像把一个字符串的所有部分放在一起,比如你的 DateTime1+DateTime2+ID+Value2。哦,是的,那一定很慢。