This stored procedure is the result of a linq query execution: I got it from the Sql Server Profiler
exec sp_executesql N'SELECT
[Limit1].[C2] AS [C1],
[Limit1].[object] AS [object],
[Limit1].[C1] AS [C2]
FROM ( SELECT TOP (50)
[GroupBy1].[A1] AS [C1],
[GroupBy1].[K1] AS [object],
1 AS [C2]
FROM ( SELECT
[Extent1].[object] AS [K1],
COUNT(1) AS [A1]
FROM [dbo].[BEM_EVT_FULL] AS [Extent1]
WHERE [Extent1].[date] > @p__linq__0
GROUP BY [Extent1].[object]
) AS [GroupBy1]
) AS [Limit1]',N'@p__linq__0 datetime2(7)',@p__linq__0='2015-01-21 00:00:00'
I have created indexes on all the columns (object and date). The problem is that this query is totaly ignoring indexes and takes more than 3 minutes to run.
On the other hand I have this query that I've run manually.
SELECT
[Limit1].[C2] AS [C1],
[Limit1].[object] AS [object],
[Limit1].[C1] AS [C2]
FROM ( SELECT TOP (50)
[GroupBy1].[A1] AS [C1],
[GroupBy1].[K1] AS [object],
1 AS [C2]
FROM ( SELECT
[Extent1].[object] AS [K1],
COUNT(1) AS [A1]
FROM [dbo].[BEM_EVT_FULL] AS [Extent1]
WHERE [Extent1].[date] > convert(datetime2, '2015-01-21 00:00:00.0000000', 121)
GROUP BY [Extent1].[object]
) AS [GroupBy1]
) AS [Limit1]
This query benefits from indexes and run under 1s. Does anyone have an idea why this is?