I have a query that takes 20 minutes to run, even though I have an index for every column in the where clauses, and every column being joined:
SELECT DISTINCT skt.VCDRAWING_REG_NO, skb.NDRAWING_ORG_NO, skb.NDRAWING_ORG_REV_NO, skb.CAPPLY_START_DATE, skb.CAPPLY_END_DATE, skto.*
FROM SPM_ABS_TRANBASE skt
JOIN SPM_ABS_BASE skb
ON skt.NDRAWING_ORG_REV_NO = skb.NDRAWING_ORG_REV_NO
AND skt.NDRAWING_ORG_NO = skb.NDRAWING_ORG_NO
JOIN SPM_ABS_MODEL skm
ON skb.NDRAWING_ORG_REV_NO = skm.NDRAWING_ORG_REV_NO
AND skb.NDRAWING_ORG_NO = skm.NDRAWING_ORG_NO
JOIN SPM_ABS_TRANOPT skto
ON skt.NDRAWING_SYSTEM_NO = skto.NDRAWING_SYSTEM_NO
JOIN ModelImport mi
ON skm.CMODEL = mi.ModelCode
WHERE (skb.CAPPLY_START_DATE <= DATEADD(day, 2, GETDATE()) OR skb.CAPPLY_START_DATE IS NULL)
AND (skb.CAPPLY_END_DATE >= DATEADD(day, -2, GETDATE()) OR skb.CAPPLY_END_DATE IS NULL)
One thing that puzzles me is this: If I add the following WHERE clause, the query returns in about 0.5 seconds:
AND mi.ModelCode = '3FBK5'
Now you're saying, well, duh, of course it gets much faster with that
- the thing is, the ModelImport table contains only 351 records. Which means if I were to split up the query above into 351 queries, each with its own where clause for a distinct ModelCode - then I can get 100% of my query results in about 175 seconds, or 2.9 minutes. This is dramatically faster. Which tells me that something in the wide-open query is grossly inefficient, and the query plan is bad.
Here is my query plan with AND mi.ModelCode = '3FBK5'
added.
After viewing my query plan, any ideas how I can speed this up?
The following recommendations will help you in your SQL tuning process.
You'll find 3 sections below:
SELECT
DISTINCT skt.VCDRAWING_REG_NO,
skb.NDRAWING_ORG_NO,
skb.NDRAWING_ORG_REV_NO,
skb.CAPPLY_START_DATE,
skb.CAPPLY_END_DATE,
skto.*
FROM
SPM_ABS_TRANBASE skt
JOIN
SPM_ABS_BASE skb
ON skt.NDRAWING_ORG_REV_NO = skb.NDRAWING_ORG_REV_NO
AND skt.NDRAWING_ORG_NO = skb.NDRAWING_ORG_NO
JOIN
SPM_ABS_MODEL skm
ON skb.NDRAWING_ORG_REV_NO = skm.NDRAWING_ORG_REV_NO
AND skb.NDRAWING_ORG_NO = skm.NDRAWING_ORG_NO
JOIN
SPM_ABS_TRANOPT skto
ON skt.NDRAWING_SYSTEM_NO = skto.NDRAWING_SYSTEM_NO
JOIN
ModelImport mi
ON skm.CMODEL = mi.ModelCode
WHERE
(
skb.CAPPLY_START_DATE <= DATEADD(day, 2, GETDATE())
OR skb.CAPPLY_START_DATE IS NULL
)
AND (
skb.CAPPLY_END_DATE >= DATEADD(day, -2, GETDATE())
OR skb.CAPPLY_END_DATE IS NULL
)