[Solved] How to detect any changes to a database (DDL and DML)

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: 25): The database will not use an index when using like searches with a leading wildcard (e.g. '%ACQUIRE_LOCK_SCH_M OBJECT%'). Although it's not always a satisfactory solution, please consider using prefix-match LIKE patterns (e.g. 'TERM%').
The optimized query:
SELECT
        NextLSN = MAX(fn.[Current LSN]),
        Databasename = DB_NAME() 
    FROM
        fn_dblog(NULL,
        NULL) fn 
    LEFT JOIN
        sys.allocation_units au 
            ON fn.AllocUnitId = au.allocation_unit_id 
    LEFT JOIN
        sys.partitions p 
            ON p.partition_id = au.container_id 
    LEFT JOIN
        sys.objects so 
            ON so.object_id = p.object_id 
    WHERE
        (
            (
                Operation IN (
                    'LOP_INSERT_ROWS', 'LOP_MODIFY_ROW', 'LOP_DELETE_ROWS', 'LOP_BEGIN_XACT', 'LOP_COMMIT_XACT'
                ) 
                AND so.is_ms_shipped = 0
            ) 
            OR (
                [Lock Information] LIKE '%ACQUIRE_LOCK_SCH_M OBJECT%'
            )
        )

Related Articles



* original question posted on StackOverflow here.