我有以下疑问:
SELECT cp.ID_Case
FROM dbo.CaseParty cp (NOLOCK)
JOIN dbo.Client cli (NOLOCK) ON CASE
WHEN cli.ClientType = 'atty' AND cp.ID_ClientAttorney = cli.ID_Client THEN 1
WHEN cli.ClientType = 'adj' AND cp.ID_ClientAdjuster = cli.ID_Client THEN 1
ELSE 0
END = 1
WHERE cli.ID_Client = @ID_Client
当我运行它时,执行计划会对表 CaseParty 的主键索引执行聚集索引扫描。 该表中有大约 700k 行,因此查询需要大量时间。 我承认需要
CASE
声明的连接不太理想,但这就是我必须处理的。
我尝试过创建各种索引,但它只是不会使用它们:
CREATE INDEX IX_CaseParty_ClientAttorneyAdjuster ON dbo.CaseParty (ID_ClientAttorney, ID_ClientAdjuster);
CREATE INDEX IX_CaseParty_ClientAttorney ON dbo.CaseParty (ID_ClientAttorney);
CREATE INDEX IX_CaseParty_ClientAdjuster ON dbo.CaseParty (ID_ClientAdjuster);
我还尝试将
CASE
连接更改为 OR
(下面的示例),但它没有任何改变。
SELECT cp.ID_Case
FROM dbo.CaseParty cp (NOLOCK)
JOIN dbo.Client cli (NOLOCK)
ON (cli.ClientType = 'atty' AND cp.ID_ClientAttorney = cli.ID_Client)
OR (cli.ClientType = 'adj' AND cp.ID_ClientAdjuster = cli.ID_Client)
WHERE cli.ID_Client = @ID_Client
我做错了什么?
选择 cp.ID_Case 来自 dbo.CaseParty cp (NOLOCK) JOIN dbo.Client cli_atty (NOLOCK) ON cp.ID_ClientAttorney = cli_atty.ID_Client AND cli_atty.ClientType = 'atty' 其中 cli_atty.ID_Client = @ID_Client
联盟
选择 cp.ID_Case 来自 dbo.CaseParty cp (NOLOCK) JOIN dbo.Client cli_adj (NOLOCK) ON cp.ID_ClientAdjuster = cli_adj.ID_Client AND cli_adj.ClientType = 'adj' 其中 cli_adj.ID_Client = @ID_Client