Indexes' maintenance in Microsoft Fabric's SQL databases is supposed to happen automatically in the background via automatic tuning options feature, though the whole functionality is still in its early phases, and therefore many questions regarding the whole process may arise. Probably the most important question is whether indexes can still be created, respectively maintained manually. That's useful for temporary or even periodic workloads, where maybe organizations might still want to maintain indexes manually.
The tests made below are based on the SalesLT.Product from AdventureWorkds database available in Microsoft Fabric. The target was to create several indexes that could be used for the various testing purposes. Each set of the below scripts was run 5-10 times until records appeared in the sys.dm_db_missing_index_details table for each test case (see further below):
-- batch 1: filter on single column (to be run 5-10 times) SELECT * FROM SalesLT.Product WHERE Color = 'Red' SELECT * FROM SalesLT.Product WHERE Color = 'Black' SELECT * FROM SalesLT.Product WHERE Color = 'White' -- batch 2: filter on two columns (to be run 5-10 times) SELECT * FROM SalesLT.Product WHERE Color = 'Red' AND Size = '58' SELECT * FROM SalesLT.Product WHERE Color = 'Black' AND Size = '58' SELECT * FROM SalesLT.Product WHERE Color = 'White' AND Size = '58' -- batch 3: filter with column selection (to be run 5-10 times) SELECT ProductNumber, Name, Color, ListPrice FROM SalesLT.Product WHERE ListPrice BETWEEN 50 AND 55 SELECT ProductNumber, Name, Color, ListPrice FROM SalesLT.Product WHERE ListPrice BETWEEN 100 and 105
Once the scripts run, one can look at the records created in the above considered dynamic management view:
-- sys metadata - missing indexes SELECT MID.statement AS table_name , MID.equality_columns , MID.inequality_columns , MID.included_columns --, MIG.index_group_handle --, MIG.index_handle FROM sys.dm_db_missing_index_details MID JOIN sys.dm_db_missing_index_groups MIG ON MID.index_handle = MIG.index_handle ORDER BY MIG.index_group_handle , MIG.index_handle
table_name | equality_columns | inequality_columns | included_columns |
[AdventureWorks01-...].[SalesLT].[Product] | [Color] | ||
[AdventureWorks01-...].[SalesLT].[Product] | [Color], [Size] | ||
[AdventureWorks01-...].[SalesLT].[Product] | [ListPrice] | [Name], [ProductNumber], [Color] |
The next step is to create one of the indexes (please note that database's name must be replaced accordingly or used only the 2-part naming convention - schema & table name ):
-- create index on Color CREATE INDEX IX_SalesLT_Product_Color ON [AdventureWorks01-...].[SalesLT].[Product] (Color);
Once the script was run, all the records related to the SalesLT.Product disappeared from the dynamic management view. Therefore, it might be a good idea to take a snapshot with view's data before creating any indexes manually. Probably the same behavior should be expected when the indexes are created by the system.
-- create index on Color & Size CREATE INDEX IX_SalesLT_Product_Color_Size ON [SalesLT].[Product] (Color, Size); -- create index on ListPrice with included columns CREATE INDEX IX_SalesLT_Product_ListPrice_IC ON [SalesLT].[Product] (ListPrice) INCLUDE(ProductNumber, Name, Color);
One can use the following query based on the meta.vIndexes (created in a previous post) to look at the indexes created:
-- sys metadata - index columns SELECt IND.db_name , IND.schema_name , IND.table_name , IND.index_name , IND.index_type , IND.principal_type , IND.auto_created FROM meta.vIndexes IND WHERE IND.schema_name = 'SalesLT' AND IND.table_name = 'Product' AND IND.index_name IN ('IX_SalesLT_Product_Color ','IX_SalesLT_Product_Color_Size' ,'IX_SalesLT_Product_ListPrice_IC') ORDER BY IND.table_name , IND.index_name
db_name | schema_name | table_name | index_name | index_type | principal_type | auto_created |
AdventureWorks01-... | SalesLT | Product | IX_SalesLT_Product_Color | NONCLUSTERED | S | False |
AdventureWorks01-... | SalesLT | Product | IX_SalesLT_Product_Color_Size | NONCLUSTERED | S | False |
AdventureWorks01-... | SalesLT | Product | IX_SalesLT_Product_ListPrice_IC | NONCLUSTERED | S | False |
After this model can be created further indexes as needed. It's always a good idea to take a "copy" of the indexes created (or keep a history of the scripts run for indexes' maintenance). This best practice is now more important, when the system can drop indexes as it considers fit.
Don't forget to clean up the changes made if the indexes aren't needed anymore:
-- cleaning after DROP INDEX IF EXISTS SalesLT.IX_SalesLT_Product_Color; DROP INDEX IF EXISTS SalesLT.IX_SalesLT_Product_Color_Size; DROP INDEX IF EXISTS SalesLT.IX_SalesLT_Product_ListPrice_IC;
So, after these tests, the standard syntax for index's maintenance seems to work also on SQL databases, with all the implications deriving from this (e.g. porting of scripts, database objects, etc.)
Happy coding!
Previous Post <<||>> Next Post
No comments:
Post a Comment