Skip to content

BUG in usp_AdaptiveIndexDefrag: When dbScope is set, a new scan is done every time. #320

Description

@4KiksFamily

In this piece of code, the query checks if the QUOTED dbScope is NOT in the list of UNQUOTED dbNames that are in the dbo.tbl_AdaptiveIndexDefrag_Working or dbo.tbl_AdaptiveIndexDefrag_Stats_Working tables, and of cause this will never be true. The effect is the
script does a rescan whenever you set the @dbScope parameter.

            /* Check if database scope has changed, if rescan is not being forced */
	IF @forceRescan = 0 AND @dbScope IS NOT NULL -- Specific scope was set
	BEGIN
		IF (SELECT COUNT(DISTINCT [dbID]) FROM dbo.tbl_AdaptiveIndexDefrag_Working) > 1
			OR **QUOTENAME(LOWER(@dbScope)) NOT IN (SELECT DISTINCT LOWER([dbName]) FROM dbo.tbl_AdaptiveIndexDefrag_Working UNION SELECT DISTINCT LOWER(dbName) FROM dbo.tbl_AdaptiveIndexDefrag_Stats_Working**)
		BEGIN
			SET @forceRescan = 1
			RAISERROR('Scope has changed. Forcing rescan of single database in scope...', 0, 42) WITH NOWAIT;
		END;
	END;

Suggested fix: add Quotename to the selects in the NOT IN () term:

QUOTENAME(LOWER(@dbScope)) NOT IN (SELECT DISTINCT QUOTENAME(LOWER([dbName])) FROM dbo.tbl_AdaptiveIndexDefrag_Working UNION SELECT DISTINCT QUOTENAME(LOWER(dbName)) FROM dbo.tbl_AdaptiveIndexDefrag_Stats_Working)
BEGIN

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions