Skip to content

Conversation

@FirstCall42
Copy link
Contributor

@FirstCall42 FirstCall42 commented May 10, 2025

Adding support for indexed views. As discussed we will use table_name for results column and the filter (if used).

Tested using the following.

EXEC sp_IndexCleanup @database_name = 'MyDatabase'
EXEC sp_IndexCleanup @database_name = 'MyDatabase', @table_name = 'MyView'

resolves #569

@erikdarlingdata
Copy link
Owner

@FirstCall42 thanks for this. There are quite a few notable changes in here, specifically to a lot of the joins. How much testing have you done with the changes? My primary concerns are probably stuff like:

  • Does specifying a table still work?
  • Does not specifying either a table or a view still work?
  • Do any of the inserts hit duplicate key violations?

These change is of particular interest:
First: WHERE (t.object_id IS NULL OR t.is_ms_shipped = 0)
Second: WHERE (t.object_id IS NULL OR t.type <> N''TF'')

I'm not in love with the ISNULLs in a couple of the JOINs, but it might not be that much of a problem in practice.

Thanks!

@erikdarlingdata erikdarlingdata added enhancement New feature or request sp_IndexCleanup Review unused and duplicative indexes labels May 13, 2025
@FirstCall42
Copy link
Contributor Author

FirstCall42 commented May 14, 2025

Hi Erik,

Here is the testing I have done. Against some test and production databases. SQL 2019 Enterprise.
Tested:
No parameters
@table_name = View name
@table_name = Table name

However, I expected to get some comments about the style and the joins.

Would you prefer an alternative like this? If so, I can make a new commit with the changes.

SELECT DISTINCT
-- @database_id,
-- database_name = DB_NAME(@database_id),
    schema_id = s.schema_id,
    schema_name = s.name,
    object_id = i.object_id,
    table_name = x.name,
    index_id = i.index_id,
    index_name = ISNULL(i.name, x.name + N'.Heap'),
    can_compress =
        CASE
            WHEN p.index_id > 0
            AND  p.data_compression = 0
            THEN 1
            ELSE 0
        END
FROM sys.indexes AS i
LEFT JOIN sys.tables AS t
    ON i.object_id = t.object_id
	AND t.is_ms_shipped = 0 AND t.type <> N'TF'
LEFT JOIN sys.views AS v
    ON i.object_id = v.object_id
CROSS APPLY (
	SELECT t.schema_id, t.name WHERE t.schema_id IS NOT NULL
		UNION ALL
	SELECT v.schema_id, v.name WHERE v.schema_id IS NOT NULL
) AS x
JOIN sys.schemas AS s
    ON x.schema_id = s.schema_id 
JOIN sys.partitions AS p
    ON  i.object_id = p.object_id
    AND i.index_id = p.index_id
LEFT JOIN sys.dm_db_index_usage_stats AS us
    ON  i.object_id = us.object_id
WHERE i.is_disabled = 0
AND i.is_hypothetical = 0

@erikdarlingdata
Copy link
Owner

@FirstCall42 Eh, I'll tweak things if they start causing issues, but I appreciate the offer.

Thanks for taking care of this. It'll show in the next release at the beginning of June.

@erikdarlingdata erikdarlingdata merged commit eaba7f5 into erikdarlingdata:dev May 14, 2025
3 checks passed
ReeceGoding pushed a commit to ReeceGoding/DarlingData that referenced this pull request May 15, 2025
Adding support for indexed views in addition to indexes on tables.
ReeceGoding pushed a commit to ReeceGoding/DarlingData that referenced this pull request May 15, 2025
Adding support for indexed views in addition to indexes on tables.
ReeceGoding pushed a commit to ReeceGoding/DarlingData that referenced this pull request May 15, 2025
Adding support for indexed views in addition to indexes on tables.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement New feature or request sp_IndexCleanup Review unused and duplicative indexes

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants