大虾们谈谈索引
今天和人谈索引的时候碰到几个问题,情景是这样的:
有表A,表结构为(ID,A,B),其中ID是主键,然后又在表A建了一个复合索引[ID asc,A asc,B asc]
问题是
1.一次查询中,一个表可以同时走两个索引吗?
2.我分别建[A asc]和[B asc]的索引,与[A asc,B asc]的复合索引等价吗?不等价的话有什么区别?
虽然不知道你们说些什么,但是感觉你们很牛B
[解决办法]
有索引,查询肯定是快了
如果不影响插入、修改的效率,多点索引也没错(数据库文件会大)
[解决办法]
SET TRANSACTION ISOLATION LEVEL READ UNCOMMITTED
SELECT TOP 30
DB_NAME() AS DatabaseName ,
'[' + SCHEMA_NAME(o.Schema_ID) + ']' + '.' + '['
+ OBJECT_NAME(s.[object_id]) + ']' AS TableName ,
i.name AS IndexName ,
i.type AS IndexType ,
s.user_updates ,
s.system_seeks + s.system_scans + s.system_lookups AS [System_usage]
FROM sys.dm_db_index_usage_stats s
INNER JOIN sys.indexes i ON s.[object_id] = i.[object_id]
AND s.index_id = i.index_id
INNER JOIN sys.objects o ON i.object_id = O.object_id
WHERE s.database_id = DB_ID()
AND OBJECTPROPERTY(s.[object_id], 'IsMsShipped') = 0
AND s.user_seeks = 0
AND s.user_scans = 0
AND s.user_lookups = 0
AND i.name IS NOT NULL
ORDER BY s.user_updates DESC