简体   繁体   English

为什么实体框架不在日期范围查询中使用日期索引?

[英]Why won't Entity Framework use the date index in a date range query?

I have a table called BPIrequests with 28M rows, that I am using Entity Framework to query on a date range.我有一个名为BPIrequests的表,有 28M 行,我使用实体框架来查询日期范围。 This is the query it produces that times out:这是它产生的超时查询:

SELECT 
    [Extent1].[Id] AS [Id], 
    [Extent1].[Request ID] AS [Request ID], 
    [Extent1].[Date] AS [Date], 
    [Extent1].[Lumen URL] AS [Lumen URL], 
    [Extent1].[Copyright owner ID] AS [Copyright owner ID], 
    [Extent1].[Copyright owner name] AS [Copyright owner name], 
    [Extent1].[Reporting organization ID] AS [Reporting organization ID], 
    [Extent1].[Reporting organization name] AS [Reporting organization name], 
    [Extent1].[URLs removed] AS [URLs removed], 
    [Extent1].[URLs that were not in Google's search index] AS [URLs that were not in Google's search index], 
    [Extent1].[URLs for which we took no action] AS [URLs for which we took no action], 
    [Extent1].[URLs pending review] AS [URLs pending review], 
    [Extent1].[From Abuser] AS [From Abuser]
FROM 
    [dbo].[BPIrequests] AS [Extent1]
WHERE 
    ((convert (datetime2, convert(varchar(255), [Extent1].[Date], 102) ,  102)) >= (convert (datetime2, convert(varchar(255), convert(datetime2, '2019-01-11 19:44:10.0000000', 121), 102) ,  102))) 
    AND ((convert (datetime2, convert(varchar(255), [Extent1].[Date], 102) ,  102)) <= (convert (datetime2, convert(varchar(255), convert(datetime2, '2019-01-26 19:44:10.8392197', 121), 102) ,  102)))

If I edit the query in SSMS to look like this it runs instantly:如果我将 SSMS 中的查询编辑为如下所示,它会立即运行:

SELECT 
    [Extent1].[Id] AS [Id], 
    [Extent1].[Request ID] AS [Request ID], 
    [Extent1].[Date] AS [Date], 
    [Extent1].[Lumen URL] AS [Lumen URL], 
    [Extent1].[Copyright owner ID] AS [Copyright owner ID], 
    [Extent1].[Copyright owner name] AS [Copyright owner name], 
    [Extent1].[Reporting organization ID] AS [Reporting organization ID], 
    [Extent1].[Reporting organization name] AS [Reporting organization name], 
    [Extent1].[URLs removed] AS [URLs removed], 
    [Extent1].[URLs that were not in Google's search index] AS [URLs that were not in Google's search index], 
    [Extent1].[URLs for which we took no action] AS [URLs for which we took no action], 
    [Extent1].[URLs pending review] AS [URLs pending review], 
    [Extent1].[From Abuser] AS [From Abuser]
FROM 
    [dbo].[BPIrequests] AS [Extent1]
WHERE 
    [Extent1].[Date] >= '2019-01-11'
    AND [Extent1].[Date] <= '2019-01-26'

here are the query plans for comparison:以下是用于比较的查询计划:

预计执行计划

How can I get Entity Framework to use the date index?如何让实体框架使用日期索引?

It look s like the date in the EF query comes from a string filed, and the sql query is converting the string to a date, this confuses the query optimizer, and the index never gets utilized.看起来 EF 查询中的日期来自字符串字段,而 sql 查询正在将字符串转换为日期,这使查询优化器感到困惑,并且索引永远不会得到利用。

The query you wrote in the management studio already treats the value as a date, so the index is used.您在管理工作室中编写的查询已经将值视为日期,因此使用了索引。

You should change your EF model class to use DateTime as the type for the 'Date' field您应该更改 EF 模型类以使用 DateTime 作为“日期”字段的类型

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM