[Solved] EF6 generating unneeded SQL query

How to optimize this SQL query?

In case you have your own slow SQL query, you can optimize it automatically here.

For the query above, the following recommendations will be helpful as part of the SQL tuning process.
You'll find 3 sections below:

  1. Description of the steps you can take to speed up the query.
  2. The optimal indexes for this query, which you can copy and create in your database.
  3. An automatically re-written query you can copy and execute in your database.
The optimization process and recommendations:
  1. Avoid Correlated Subqueries (query line: 10): A correlated subquery is a subquery that contains a reference (column: EventId) to a table that also appears in the outer query. Usually correlated queries can be rewritten with a join clause, which is the best practice. The database optimizer handles joins much better than correlated subqueries. Therefore, rephrasing the query with a join will allow the optimizer to use the most efficient execution plan for the query.
  2. Avoid Subqueries (query line: 24): We advise against using subqueries as they are not optimized well by the optimizer. Therefore, it's recommended to join a newly created temporary table that holds the data, which also includes the relevant search index.
  3. Create Optimal Indexes (modified query below): The recommended indexes are an integral part of this optimization effort and should be created before testing the execution duration of the optimized query.
  4. Mixed Order By Directions Prevents Index Use (query line: 41): The database will not use a sorting index (if exists) in cases where the query mixes ASC (the default if not specified) and DESC order. To avoid filesort, you may consider using the same order type for all columns. Another option that will allow you to switch one direction to another is to create a new reversed "sort" column (max_sort - sort) and index it instead.
  5. Prefer Direct Join Over Joined Subquery (query line: 29): We advise against using subqueries as they are not optimized well by the optimizer. Therefore, we recommend to replace subqueries with JOIN clauses.
Optimal indexes for this query:
CREATE INDEX events_idx_deviceid_timeend ON dbo.Events (DeviceId,TimeEnd);
The optimized query:
        [Project2].[EventId] AS [EventId],
        [Project2].[DeviceId] AS [DeviceId],
        [Project2].[TimeEnd] AS [TimeEnd],
        [Project2].[C2] AS [C1],
        [Project2].[EventId1] AS [EventId1],
        [Project2].[Name] AS [Name],
        [Project2].[C1] AS [C2] 
            [Limit1].[EventId] AS [EventId],
            [Limit1].[TimeEnd] AS [TimeEnd],
            [Limit1].[DeviceId] AS [DeviceId],
            [Extent2].[Name] AS [Name],
            [Extent2].[EventId] AS [EventId1],
                WHEN ([Extent2].[Value] IS NULL) THEN CAST(NULL AS float) 
                ELSE ROUND([Extent2].[Value],
                1) END AS [C1],
    WHEN ([Extent2].[Value] IS NULL) THEN CAST(NULL AS int) 
    ELSE 1 END AS [C2] 
    [Project1].[EventId] AS [EventId],
    [Project1].[TimeEnd] AS [TimeEnd],
    [Project1].[DeviceId] AS [DeviceId] 
    [dbo].[Events] AS [Project1] 
        [Project1].[DeviceId] = 1
    [Project1].[TimeEnd] DESC) AS [Limit1] 
[dbo].[Octaves] AS [Extent2] 
    ON [Limit1].[EventId] = [Extent2].[EventId]
) AS [Project2] 
[Project2].[TimeEnd] DESC,
[Project2].[EventId] ASC,
[Project2].[C2] ASC

Related Articles

* original question posted on StackOverflow here.