为非常大的 SQL Server 表添加索引

为非常大的 SQL Server 表添加索引

我有一张包含 900 万行的表(刚从我们的承保提供商处收到)。4 列 - leadid(guid)、RawLeadXML(nvarchar(max),每行包含一个 2-3kb 的 xml 文档)、LeadStatusID(int)和 id(autonumber int,我刚刚添加了这个)。

我甚至无法运行一个简单的删除语句。

DELETE FROM Leads WHERE LeadID = '100a7927-5311-4f12-8fe3-95c079d32dd4' 

我曾尝试向 LeadID 添加索引,但是超时了。

CREATE NONCLUSTERED INDEX IX_Leads ON dbo.Leads(LeadID) WITH( STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY] 

我的机器有 4 个处理器和 12 GB 的 RAM。运行 Windows 7。SQL Server 2008(我相信是开发人员版)。如何向此表添加索引?

答案1

9M 行其实不算多。我猜想在这两种情况下,都有某些东西锁定了表。

检查表锁,并关闭锁定表的任何进程,或手动终止锁(如果您确定没有其他人正在使用数据库)。有一个相当不错的脚本可以在此显示所有锁

SELECT  L.request_session_id AS SPID, 
        DB_NAME(L.resource_database_id) AS DatabaseName,
        O.Name AS LockedObjectName, 
        P.object_id AS LockedObjectId, 
        L.resource_type AS LockedResource, 
        L.request_mode AS LockType,
        ST.text AS SqlStatementText,        
        ES.login_name AS LoginName,
        ES.host_name AS HostName,
        TST.is_user_transaction as IsUserTransaction,
        AT.name as TransactionName,
        CN.auth_scheme as AuthenticationMethod
FROM    sys.dm_tran_locks L
        JOIN sys.partitions P ON P.hobt_id = L.resource_associated_entity_id
        JOIN sys.objects O ON O.object_id = P.object_id
        JOIN sys.dm_exec_sessions ES ON ES.session_id = L.request_session_id
        JOIN sys.dm_tran_session_transactions TST ON ES.session_id = TST.session_id
        JOIN sys.dm_tran_active_transactions AT ON TST.transaction_id = AT.transaction_id
        JOIN sys.dm_exec_connections CN ON CN.session_id = ES.session_id
        CROSS APPLY sys.dm_exec_sql_text(CN.most_recent_sql_handle) AS ST
WHERE   resource_database_id = db_id()
ORDER BY L.request_session_id

相关内容