我有一个正在处理的查询,它显示了我没有预料到的性能问题。这是到目前为止的查询。
INSERT INTO @Bridge (PolicyNumber, ProducerCode, BridgeDate, EffectiveDate, FirstName, LastName, LicenseNumber, BirthDate, Address, City, State, ZipCode)
SELECT tab.col.value('@PolicyNumber', 'VARCHAR(10)') AS PolicyNumber,
tab.col.value('@ProducerCode','VARCHAR(10)') as ProducerCode,
tab.col.value('@BridgeDate','DATETIME') AS BridgeDate,
tab.col.value('@EffectiveDate', 'DATETIME') as EffectiveDate,
tab.col.value('@FirstName', 'VARCHAR(200)') as FirstName,
tab.col.value('@LastName', 'VARCHAR(200)') as LastName,
CASE
WHEN tab.col.value('@LicenseNumber','VARCHAR(50)') LIKE '%0000%' THEN NULL
WHEN tab.col.value('@LicenseNumber','VARCHAR(50)') LIKE '%1111%' THEN NULL
WHEN tab.col.value('@LicenseNumber','VARCHAR(50)') LIKE '%2222%' THEN NULL
WHEN tab.col.value('@LicenseNumber','VARCHAR(50)') LIKE '%3333%' THEN NULL
WHEN tab.col.value('@LicenseNumber','VARCHAR(50)') LIKE '%4444%' THEN NULL
WHEN tab.col.value('@LicenseNumber','VARCHAR(50)') LIKE '%5555%' THEN NULL
WHEN tab.col.value('@LicenseNumber','VARCHAR(50)') LIKE '%6666%' THEN NULL
WHEN tab.col.value('@LicenseNumber','VARCHAR(50)') LIKE '%7777%' THEN NULL
WHEN tab.col.value('@LicenseNumber','VARCHAR(50)') LIKE '%8888%' THEN NULL
WHEN tab.col.value('@LicenseNumber','VARCHAR(50)') LIKE '%9999%' THEN NULL
ELSE tab.col.value('@LicenseNumber','VARCHAR(50)')
END as LicenseNumber,
tab.col.value('@BirthDate','DATETIME') as BirthDate,
REPLACE(tab.col.value('@Address1','VARCHAR(300)'), ' APT ',' #') as Address1,
tab.col.value('@City','VARCHAR(300)') as City,
tab.col.value('@State','VARCHAR(5)') as State,
tab.col.value('@ZipCode','VARCHAR(10)') as Zip
FROM @xml.nodes('//rows/datarow') as tab(col)
SELECT B.PolicyNumber,
B.ProducerCode,
B.BridgeDate,
B.EffectiveDate,
H.current_policy,
H.cancel_date,
H.first_eff_date,
H.display_address,
H.city,
H.state,
H.zip
FROM @Bridge B
LEFT JOIN (
SELECT P.policy_id,
P.current_policy,
CASE
WHEN A.pobox <> '' THEN 'PO BOX ' + REPLACE(A.pobox,'PO BOX ','')
ELSE RTRIM(A.house_num + ' ' + A.street_name + ' ' + CASE
WHEN A.apt_num = '' THEN ''
ELSE '#' + A.apt_num
END)
END as display_address,
A.pobox,
A.house_num,
A.street_name,
A.apt_num,
A.city,
MAX(A.policyimage_num) as policimage_num, --this is just to limit the results to the most recent
S.state,
A.zip,
P.first_eff_date,
P.cancel_date
FROM Diamond.dbo.Policy P WITH (NOLOCK)
LEFT JOIN Diamond.dbo.Address A WITH (NOLOCK)
ON P.policy_id = A.policy_id
AND A.nameaddresssource_id = 3
LEFT JOIN Diamond.dbo.State S WITH (NOLOCK)
ON A.state_id = S.state_id
WHERE A.state_id IS NOT NULL
AND P.current_policy NOT IN (SELECT PolicyNumber FROM @Bridge)
GROUP BY P.policy_id,
P.current_policy,
P.cancel_date,
P.first_eff_date,
A.pobox,
A.house_num,
A.street_name,
A.apt_num,
A.city,
S.state,
A.zip) AS H
ON B.Address = H.display_address
AND B.State = H.state
AND B.City = H.city
AND SUBSTRING(B.ZipCode,1,5) = SUBSTRING(H.Zip,1,5)
AND B.PolicyNumber != H.current_policy
WHERE H.current_policy IS NOT NULL
此查询自行运行,大约在 1:30 秒内完成。但是如果我在 WHERE 子句中添加以下内容
AND B.EffectiveDate != H.first_eff_date
突然,查询需要更长的时间才能返回结果。(在我写这篇文章的时候,我们已经超过 15 分钟了,而且还在继续)我认为简单地有一个子句来清除一些额外的行不会产生如此剧烈的效果,但显然它确实如此。我如何解决它,我只是好奇是否有人对它为什么会产生这种效果有任何想法?