You’ve probably heard: “indexes are not free”. They are right!
Indexes can greatly improve queries performance in the SQL server, but they may hamper the performance too.
There are costs in storage (disk space) as well as resources to the maintenance required on DML operations.
So, it’s very important to understand your overall workload and find a good balance between, making your queries efficient and not paying too much for that.
There are many approaches, concepts, and technics about the index, below you will see some of them.
SQL Server will sometimes suggest an index that it believes will help that query run faster.
These are known as missing indexes suggestions.
SQL Server is very good at determining when an index is needed but sometimes it fails.
The script below identifies missing indexes with more than 70000 unique compiles or user seek or user scan and average use impact higher than 80%.
You should not create all the missing indexes this script suggests. They are just for your guidance.
- If the server was rebooted earlier than two weeks ago, the stats are useless.
- You probably shouldn’t have then 5 indexes per table (Over-Indexing).
select sd.name, substring(d.statement, charindex('.', d.statement) + 1, len(d.statement)) as tablename, s.unique_compiles, s.user_seeks, s.user_scans, s.avg_user_impact, 'use [' + sd.name + ']; create index IX_' + replace(reverse(substring(reverse(d.statement), 0, charindex('[', reverse(d.statement)))), ']', '') + '_nn ON ' + substring(d.statement, charindex('.', d.statement) + 1, len(d.statement)) + ' (' + case when d.equality_columns is not null and d.inequality_columns is not null then d.equality_columns + ',' + d.inequality_columns when d.equality_columns is not null and d.inequality_columns is null then d.equality_columns when d.equality_columns is null and d.inequality_columns is not null then d.inequality_columns end + ')' + case when d.included_columns is not null then ' include (' + d.included_columns + ')' else '' end as create_cmd from master.sys.dm_db_missing_index_details d inner join master.sys.dm_db_missing_index_groups g on d.index_handle = g.index_handle inner join master.sys.dm_db_missing_index_group_stats s on g.index_group_handle = s.group_handle inner join master.dbo.sysdatabases sd on d.database_id = sd.dbid where sd.dbid > 4 and sd.name not in ('distribution', 'LogShipping') and (s.unique_compiles > 70000 or s.user_seeks > 70000 or s.user_scans > 70000) and s.avg_user_impact > 80 order by sd.name, s.user_seeks desc, s.user_scans desc
Fill factor option
When you create (or rebuild) an index, you can set “fillfactor”, what means the percentage of each 8K data page used in the “leaf” level of the index it should fill up.
Lowering this setting may improve performance by reducing page splits, but do not set the system-wide value for it, probably that will hurt more than help.
It would be best if you had a proper maintenance plan and only change the fill factor for heavily fragmented indexes.
If needed, start reducing the fillfactor gradually, going to 95%, and rebuilding the indexes in a planned change.
Sometimes an index is never used to improve performance.
Whatever, SQL Server has to maintain it in every INSERT, UPDATE and DELETE operations.
So, why keep this index?
You can use the script below to find indexes that were updated more than 5000 times, never used or with many updates and few lookups ( < 10 ):
- Save the output “create_cmd” for re-create index script in case of rollback.
- Execute the output “drop_cmd ” to implement the change.
- Make sure there is not any query or store procedure forcing hint to the indexes that will be dropped.
set nocount on; declare @DBName nvarchar(128), @cmd nvarchar(max) = N'', @collation varchar(100) select @collation = convert(varchar, serverproperty('collation')) if object_id('tempdb..#indexes') is not null drop table #indexes create table #indexes (databasename sysname not null, tablename varchar(200) not null, indexname varchar(200) not null, drop_cmd varchar(max) not null, create_cmd varchar(max) not null) declare DBList cursor local fast_forward for select cast(name as nvarchar(128)) from master.dbo.sysdatabases where dbid > 4 and name not in ('distribution', 'LogShipping') and databasepropertyex(name, 'Status') = 'ONLINE' and databasepropertyex(name, 'Updateability') = 'READ_WRITE' open DBList fetch next from DBList into @dbname while @@fetch_status <> -1 begin set @cmd = N' ;with notusedindexes as (select db_name(db_id()) AS DBName, ss.name + ''.'' + o.name AS [table_name], i.name AS [index_name], i.type_desc, ''drop index '' + i.name + '' on '' + ss.name + ''.'' + o.name AS drop_cmd from [' + @DBName + '].sys.dm_db_index_usage_stats iu inner join [' + @DBName + '].sys.indexes i ON iu.index_id = i.index_id and iu.object_id = i.object_id inner join [' + @DBName + '].sys.objects o ON i.object_id = o.object_id left join [' + @DBName + '].sys.objects n ON n.name = i.name inner join [' + @DBName + '].sys.schemas ss ON o.schema_id = ss.schema_id where iu.database_id = db_id() and i.type <> 0 and i.type_desc = ''nonclustered'' and (n.TYPE IS NULL or n.type NOT IN (''PK'', ''UQ'')) and ((iu.user_seeks = 0 and iu.user_scans = 0 and iu.user_lookups = 0 and iu.user_updates <> 0) -- only ever updated or (iu.user_updates > 100000 and iu.user_seeks < 10 and iu.user_scans < 10 and iu.user_lookups < 10) -- loads of updates, few lookups or (iu.user_seeks < 10 and iu.user_scans < 10 and iu.user_lookups < 10 and iu.user_updates < 10)) -- hardly used at all and iu.user_updates > 5000), -- updated more than 5000 times indexlist as (select distinct n.table_name tablename,n.index_name indexname, n.drop_cmd, i.is_unique, (select distinct stuff((select '', '' + c.name from sys.index_columns ic1 inner join sys.columns c on ic1.object_id=c.object_id and ic1.column_id=c.column_id where ic1.index_id = ic.index_id and ic1.object_id=i.object_id and ic1.index_id=i.index_id and ic1.is_included_column = 0 order by ic1.key_ordinal For XML PATH('''')),1,2,'''') from sys.index_columns ic where object_id=i.object_id and index_id=i.index_id) as keycolumnlist, (select distinct stuff((select '', '' + c.name from sys.index_columns ic1 inner join sys.columns c on ic1.object_id=c.object_id and ic1.column_id=c.column_id where ic1.index_id = ic.index_id and ic1.object_id=i.object_id and ic1.index_id=i.index_id and ic1.is_included_column = 1 order by index_column_id For XML PATH('''')),1,2,'''') from sys.index_columns ic where object_id=i.object_id and index_id=i.index_id) as includedcolumnlist from sys.indexes i inner join sys.index_columns ic on i.object_id=ic.object_id and i.index_id=ic.index_id inner join sys.objects o on i.object_id=o.object_id inner join notusedindexes n on i.object_id = object_id(n.table_name) and i.name collate ' + @collation + ' = n.index_name collate ' + @collation + ' where o.is_ms_shipped=0 and i.is_primary_key = 0 and i.is_disabled = 0 and i.is_unique_constraint = 0 and i.has_filter = 0) insert into #indexes select db_name() as databasename, tablename, indexname, drop_cmd, ''create '' + case when is_unique = 1 then ''unique '' else '''' end + ''index '' + indexname + '' on '' + tablename + '' ('' + keycolumnlist + '')'' + case when includedcolumnlist is not null then + '' include ('' + includedcolumnlist + '')'' else '''' end + '' with (data_compression = page)'' create_cmd from indexlist' --print @cmd exec sys.sp_executesql @cmd fetch next from DBList into @DBName end close DBList deallocate DBList select * from #indexes drop table #indexes