我有 2 个表(SQL Server 2008):文档和字段。
文件:
Id (PK)
Some-Others-Columns
领域:
Id (PK)
DocumentId (FK to Documents)
Name
Value
每个文档都分配了 80 多个字段。我必须选择一个表,其中每一行都是一个文档 + 一些特定字段(不是 80+,只有一些字段)。
我的 T-SQL 代码对大表运行速度极慢,我该如何优化它?
SELECT f1.Value AS 'f1', f2.Value AS 'f2', f3.Value AS 'f3', f4.Value AS 'f4', f5.Value AS 'f5', f6.Value AS 'f6', d.PartyId, d.CreationDate
FROM dbo.Fields AS f WITH (NOLOCK)
INNER JOIN dbo.Fields AS f1 ON f.Id = f1.Id
INNER JOIN dbo.Documents AS d ON f1.DocumentId = d.Id
INNER JOIN dbo.Fields AS f2 ON d.Id = f2.DocumentId
INNER JOIN dbo.Fields AS f3 ON d.Id = f3.DocumentId
INNER JOIN dbo.Fields AS f4 ON d.Id = f4.DocumentId
INNER JOIN dbo.Fields AS f5 ON d.Id = f5.DocumentId
INNER JOIN dbo.Fields AS f6 ON d.Id = f6.DocumentId
WHERE
(f1.Name = 'Some-Name-1')
AND (f2.Name = 'Some-Name-2')
AND (f3.Name = 'Some-Name-3')
AND (f4.Name = 'Some-Name-4')
AND (f5.Name = 'Some-Name-5')
AND (f6.Name = 'Some-Name-6')
请帮我优化这个查询