Почему Entity Framework не использует индекс даты в запросе диапазона дат? - PullRequest
0 голосов
/ 27 января 2019

У меня есть таблица с именем BPIrequests с 28M строками, которую я использую Entity Framework для запроса по диапазону дат.Это запрос, который он выдает, что время ожидания:

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)))

Если я отредактирую запрос в 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'

вот планы запроса для сравнения:

Estimated Execution Plans

Как мне заставить Entity Framework использовать индекс даты?

1 Ответ

0 голосов
/ 27 января 2019

Похоже, что дата в EF-запросе взята из поданной строки, а SQL-запрос преобразует строку в дату, это сбивает с толку оптимизатор запросов, и индекс никогда не используется.

Запрос, который вы написали в Management Studio, уже обрабатывает значение как дату, поэтому используется индекс.

Вы должны изменить свой класс модели EF, чтобы использовать DateTime в качестве типа для поля «Дата»

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...