[Solved] Query slow on SQL Server 2008 R2

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 LIKE Searches With Leading Wildcard (query line: 8): The database will not use an index when using like searches with a leading wildcard (e.g. '%param1%'). Although it's not always a satisfactory solution, please consider using prefix-match LIKE patterns (e.g. 'TERM%').
  2. Avoid LIKE Searches With Leading Wildcard (query line: 10): The database will not use an index when using like searches with a leading wildcard (e.g. '%param2%'). Although it's not always a satisfactory solution, please consider using prefix-match LIKE patterns (e.g. 'TERM%').
  3. Avoid LIKE Searches With Leading Wildcard (query line: 12): The database will not use an index when using like searches with a leading wildcard (e.g. '%param3%'). Although it's not always a satisfactory solution, please consider using prefix-match LIKE patterns (e.g. 'TERM%').
  4. Avoid LIKE Searches With Leading Wildcard (query line: 13): The database will not use an index when using like searches with a leading wildcard (e.g. '%%param4%%'). Although it's not always a satisfactory solution, please consider using prefix-match LIKE patterns (e.g. 'TERM%').
  5. Avoid LIKE Searches With Leading Wildcard (query line: 14): The database will not use an index when using like searches with a leading wildcard (e.g. '%%param5%%'). Although it's not always a satisfactory solution, please consider using prefix-match LIKE patterns (e.g. 'TERM%').
  6. Avoid Selecting Unnecessary Columns (query line: 2): Avoid selecting all columns with the '*' wildcard, unless you intend to use them all. Selecting redundant columns may result in unnecessary performance degradation.
  7. 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 v_alarmconsolidate_idx_critico_approvatoq_alarmid ON WWALMDB.dbo.v_AlarmConsolidated (Critico,ApprovatoQA,AlarmID);
The optimized query:
SELECT
        * 
    FROM
        [WWALMDB].[dbo].[v_AlarmConsolidated] 
    WHERE
        [WWALMDB].[dbo].[v_AlarmConsolidated].Critico = 1 
        AND [WWALMDB].[dbo].[v_AlarmConsolidated].ApprovatoQA = 0 
        AND [WWALMDB].[dbo].[v_AlarmConsolidated].InAttesaDiRiconoscimento LIKE '%param1%' 
        AND (
            [WWALMDB].[dbo].[v_AlarmConsolidated].Tipo LIKE '%param2%'
        ) 
        AND [WWALMDB].[dbo].[v_AlarmConsolidated].Area LIKE '%param3%' 
        AND [WWALMDB].[dbo].[v_AlarmConsolidated].Nome LIKE '%%param4%%' 
        AND [WWALMDB].[dbo].[v_AlarmConsolidated].Descrizione LIKE '%%param5%%' 
        AND (
            (
                [WWALMDB].[dbo].[v_AlarmConsolidated].[Dataora Scatto] >= CONVERT(DATETIME, 'param6', 105)
            ) 
            AND (
                [WWALMDB].[dbo].[v_AlarmConsolidated].[Dataora Scatto] <= CONVERT(DATETIME, 'param7', 105)
            ) 
            OR (
                (
                    [WWALMDB].[dbo].[v_AlarmConsolidated].[Dataora Rientro] >= CONVERT(DATETIME, 'param6', 105)
                ) 
                AND (
                    [WWALMDB].[dbo].[v_AlarmConsolidated].[Dataora Rientro] <= CONVERT(DATETIME, 'param7', 105)
                )
            ) 
            OR (
                (
                    [WWALMDB].[dbo].[v_AlarmConsolidated].[Dataora PresoInCarico] >= CONVERT(DATETIME, 'param6', 105)
                ) 
                AND (
                    [WWALMDB].[dbo].[v_AlarmConsolidated].[Dataora PresoInCarico] <= CONVERT(DATETIME, 'param7', 105)
                )
            )
        ) 
    ORDER BY
        [WWALMDB].[dbo].[v_AlarmConsolidated].AlarmID DESC

Related Articles



* original question posted on StackOverflow here.