WITH tst
AS (
SELECT nz.sPostcode op
,zp.nZoneCountryID
,zp.sPostcode
,zp.nZoneID
,zp.nInjectionID
FROM dbo.tblZonePostcode zp
INNER JOIN tblNewZone nz ON nz.nPostcodeID = zp.NewZoneId
WHERE CHARINDEX('%', zp.sPostcode) = 1
AND zp.sPostcode IS NOT NULL
AND isnumeric(zp.sPostcodeRange) = 0
)
SELECT *
INTO #tmptst
FROM tst;
CREATE INDEX idx_sPostcode12 ON #tmptst (sPostcode);
CREATE INDEX idx_x1 ON #tmptst (nZoneCountryID);
CREATE INDEX idx_x2 ON #tmptst (nInjectionID);
CREATE INDEX idx_x3 ON #tmptst (nZoneID);
UPDATE #tmprec
SET calculatedPostCodeZone = (
SELECT TOP 1 zp.op
FROM #tmptst zp
WHERE zp.nZoneCountryID = CalculatedCountryId
AND zp.nInjectionID = CalculatedinjectionPointId
AND zp.nZoneID = CalculatedZoneId
AND CHARINDEX(zp.sPostcode, sConsigneePostcodeFirst) = 1
)
WHERE CalculatedRateChartGroupID > 0
AND calculatedRatechartValues IS NULL
AND calculatedPostCodeZone IS NULL
AND CalculationStatus IS NULL
AND 0 < (
SELECT count(1)
FROM #tmptst
);
总共有tst,总共包含85k条记录,而#tmprec包含255k条记录。上述查询的目的是从tblNewZone获取matchig邮政编码,以获得以匹配sConsigneePostcodeFirst开头的记录邮政编码。
问题是上面的查询需要7分钟才能执行。我已经为查询添加了索引,尝试查询,尝试了文本搜索索引。
如果我能做任何改进查询的事情,请告诉我。