SQL Server Index Creation

时间:2017-07-12 08:05:35

标签: sql sql-server indexing

I need to create non clustered index on a table, I will be using 5 columns in the where clause. I will have to use include(Covering) to avoid lookup. Shall I create 5 non clustered index with include columns for each index or Shall I create one index with 5 columns with include columns. I will be having around 50 million records in it. What would be best for above given scenarios. Please suggest me the best way to increase the retrieval performance.

2 个答案:

答案 0 :(得分:0)

@Baskar, I would suggest you to run this query and find the need to create Index, and you will understand in your table the need for 5 NC indices. This query is for Info only, do not need to create all Indices suggested by this query. Change you table name in where clause

SELECT TOP 10
dm_mid.database_id AS DatabaseID,
dm_migs.avg_user_impact*(dm_migs.user_seeks+dm_migs.user_scans) Avg_Estimated_Impact,
dm_migs.last_user_seek AS Last_User_Seek,
OBJECT_NAME(dm_mid.OBJECT_ID,dm_mid.database_id) AS [TableName],
'CREATE INDEX [IX_' + OBJECT_NAME(dm_mid.OBJECT_ID,dm_mid.database_id) + '_'
+ REPLACE(REPLACE(REPLACE(ISNULL(dm_mid.equality_columns,''),', ','_'),'[',''),']','') 
+ CASE
WHEN dm_mid.equality_columns IS NOT NULL
AND dm_mid.inequality_columns IS NOT NULL THEN '_'
ELSE ''
END
+ REPLACE(REPLACE(REPLACE(ISNULL(dm_mid.inequality_columns,''),', ','_'),'[',''),']','')
+ ']'
+ ' ON ' + dm_mid.statement
+ ' (' + ISNULL (dm_mid.equality_columns,'')
+ CASE WHEN dm_mid.equality_columns IS NOT NULL AND dm_mid.inequality_columns 
IS NOT NULL THEN ',' ELSE
'' END
+ ISNULL (dm_mid.inequality_columns, '')
+ ')'
+ ISNULL (' INCLUDE (' + dm_mid.included_columns + ')', '') AS Create_Statement
FROM sys.dm_db_missing_index_groups dm_mig
INNER JOIN sys.dm_db_missing_index_group_stats dm_migs
ON dm_migs.group_handle = dm_mig.index_group_handle
INNER JOIN sys.dm_db_missing_index_details dm_mid
ON dm_mig.index_handle = dm_mid.index_handle
WHERE dm_mid.database_ID = DB_ID() and OBJECT_NAME(dm_mid.OBJECT_ID,dm_mid.database_id) = 'MyTable'
ORDER BY Avg_Estimated_Impact DESC
GO

答案 1 :(得分:0)

使用执行计划模式(CTRL + SHIFT + ALT + L或CTRL + L)运行查询。此模式建议索引。并创造这个。