[Solved] SQL Server Deadlock by IX pagelock

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 Subqueries (query line: 22): 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.
  2. 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.
Optimal indexes for this query:
CREATE INDEX chargeitems_idx_workitem_id ON dbo.ChargeItems (WorkItem_Id);
CREATE INDEX workitems_idx_dmc ON WorkItems (DMC);
The optimized query:
SELECT
        [Limit1].[Id] AS [Id],
        [Limit1].[DMC] AS [DMC],
        [Limit1].[FirstSeen] AS [FirstSeen],
        [Limit1].[DrawingNo] AS [DrawingNo],
        [Limit1].[MachineId] AS [MachineId],
        [Limit1].[WorkItemState_Id] AS [WorkItemState_Id],
        [Limit1].[ItemType_Id] AS [ItemType_Id],
        [Limit1].[Repaired] AS [Repaired],
        [Limit1].[MachineCycle] AS [MachineCycle],
        [Limit1].[FirstSeenCheck] AS [FirstSeenCheck],
        [Limit1].[LastSeen] AS [LastSeen],
        [Limit1].[Archive] AS [Archive],
        [Limit1].[CastingDateString] AS [CastingDateString],
        [Limit1].[Deleted] AS [Deleted],
        [Limit1].[DMC2] AS [DMC2],
        [Limit1].[Id1] AS [Id1],
        [Limit1].[WorkPlace_Id] AS [WorkPlace_Id],
        [Limit1].[CastingFormIdent_Id] AS [CastingFormIdent_Id],
        [Limit1].[FormIdentItemType_Id] AS [FormIdentItemType_Id] 
    FROM
        (SELECT
            TOP (1) [Extent1].[Id] AS [Id],
            [Extent1].[DMC] AS [DMC],
            [Extent1].[FirstSeen] AS [FirstSeen],
            [Extent1].[DrawingNo] AS [DrawingNo],
            [Extent1].[MachineId] AS [MachineId],
            [Extent1].[WorkItemState_Id] AS [WorkItemState_Id],
            [Extent1].[ItemType_Id] AS [ItemType_Id],
            [Extent1].[Repaired] AS [Repaired],
            [Extent1].[MachineCycle] AS [MachineCycle],
            [Extent1].[FirstSeenCheck] AS [FirstSeenCheck],
            [Extent1].[LastSeen] AS [LastSeen],
            [Extent1].[Archive] AS [Archive],
            [Extent1].[CastingDateString] AS [CastingDateString],
            [Extent1].[Deleted] AS [Deleted],
            [Extent1].[DMC2] AS [DMC2],
            [Extent1].[WorkPlace_Id] AS [WorkPlace_Id],
            [Extent1].[CastingFormIdent_Id] AS [CastingFormIdent_Id],
            [Extent1].[FormIdentItemType_Id] AS [FormIdentItemType_Id],
            [Extent2].[Id] AS [Id1] 
        FROM
            WorkItems AS [Extent1] 
        LEFT OUTER JOIN
            [dbo].[ChargeItems] AS [Extent2] 
                ON [Extent1].[Id] = [Extent2].[WorkItem_Id] 
        WHERE
            (
                [Extent1].[DMC] = ''
            ) 
            OR (
                (
                    [Extent1].[DMC] IS NULL
                )
            )) AS [Limit1]

Related Articles



* original question posted on StackOverflow here.