SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


where fldValue=123 vs join to table with one row


where fldValue=123 vs join to table with one row

Author
Message
Sergiy
Sergiy
One Orange Chip
One Orange Chip (25K reputation)One Orange Chip (25K reputation)One Orange Chip (25K reputation)One Orange Chip (25K reputation)One Orange Chip (25K reputation)One Orange Chip (25K reputation)One Orange Chip (25K reputation)One Orange Chip (25K reputation)

Group: General Forum Members
Points: 25724 Visits: 12467
souLTower (1/24/2013)
That helps but still doesn't explain why the
where IN (sub select)

has such a different execution plan than

WHERE IN (constants)

What I've done to improve the performance (and I'm not really proud of) is to loop over the list of values, query the main view into a temp table using WHERE fldValue = syntax, then query the temp table for the final output. I tried changing indexes, join hints, lots of things but this worked best and was extensible whether querying against 1 value or 100.


When you operate with constants SQL Server builds an execution plan considering the actual values assigned to the constants.
When it selects it from the table it jhas no idea what kind of values are in there (because the table is a heap) and it has to build more "pessimistic", but more universal plan, which might be not that good in this particular case but will perform better in average in other cases.

Add an index to your lookup table. It should help the optimizer in building more effective plan considering the current data in the table.
Sean Pearce
Sean Pearce
SSCarpal Tunnel
SSCarpal Tunnel (4.1K reputation)SSCarpal Tunnel (4.1K reputation)SSCarpal Tunnel (4.1K reputation)SSCarpal Tunnel (4.1K reputation)SSCarpal Tunnel (4.1K reputation)SSCarpal Tunnel (4.1K reputation)SSCarpal Tunnel (4.1K reputation)SSCarpal Tunnel (4.1K reputation)

Group: General Forum Members
Points: 4078 Visits: 3436
I have downloaded the plans and they are exactly the same. I downloaded them twice in case of PEBKAC issues.



The SQL Guy @ blogspot

@SeanPearceSQL

About Me
souLTower
souLTower
Ten Centuries
Ten Centuries (1K reputation)Ten Centuries (1K reputation)Ten Centuries (1K reputation)Ten Centuries (1K reputation)Ten Centuries (1K reputation)Ten Centuries (1K reputation)Ten Centuries (1K reputation)Ten Centuries (1K reputation)

Group: General Forum Members
Points: 1029 Visits: 563
Sorry, I just realized that the fast and slow executions are included in each file. Compare the top one to the bottom one. You can see in the fast one that the execution plan starts by using "Where tblConsul.fldPlanKey=30472"

In the slower plan, the index on the temp table (IDX_TMP) isn't introduced until midway through the execution. This is after 7+ million rows have been queried to the right of this execution point.

So my question, which I understand may not be able to be answered, is why is a different table used to determine the filter in the two different cases AND, is there a way to prevent it or to let the optimizer know that there is a more efficient strategy?

ST
Sean Pearce
Sean Pearce
SSCarpal Tunnel
SSCarpal Tunnel (4.1K reputation)SSCarpal Tunnel (4.1K reputation)SSCarpal Tunnel (4.1K reputation)SSCarpal Tunnel (4.1K reputation)SSCarpal Tunnel (4.1K reputation)SSCarpal Tunnel (4.1K reputation)SSCarpal Tunnel (4.1K reputation)SSCarpal Tunnel (4.1K reputation)

Group: General Forum Members
Points: 4078 Visits: 3436
My impression is that your query is too complicated and the query optimizer has no chance of compiling the most efficient plan. You can see this in the execution plan you posted.

StatementOptmEarlyAbortReason="TimeOut"



This means that the optimizer timed out while evaluating plans and occurs in both of your queries.

You mention that you want to know the difference between
Select from myView where fldValue IN (123,456,789)

and
Select from myView V inner join myTemp T ON V.fldValue = T.fldValue

However your queries are so complicated it is preventing you from comparing an apple with an apple.

If I were you I wouldn't be at all interested in the above comparison, but rather in breaking up the query to give the optimizer a better chance.

Most alarming to me is MemoryGrant="25184" for query 1 and MemoryGrant="529712" for query 2.

http://blogs.msdn.com/b/sqlqueryprocessing/archive/2010/02/16/understanding-sql-server-memory-grant.aspx

Query 1 has 9 hash matches while query 2 has 14 hash matches.



The SQL Guy @ blogspot

@SeanPearceSQL

About Me
Sean Pearce
Sean Pearce
SSCarpal Tunnel
SSCarpal Tunnel (4.1K reputation)SSCarpal Tunnel (4.1K reputation)SSCarpal Tunnel (4.1K reputation)SSCarpal Tunnel (4.1K reputation)SSCarpal Tunnel (4.1K reputation)SSCarpal Tunnel (4.1K reputation)SSCarpal Tunnel (4.1K reputation)SSCarpal Tunnel (4.1K reputation)

Group: General Forum Members
Points: 4078 Visits: 3436
Can you tell me how the following two queries compare please.

PLEASE NOTE: You have not aliased all your columns so I have no idea which tables they come from. You must edit the following queries to remove any non-aliased columns that are not from vwREPQICASSCostsByCC.

SELECT
fldConsulNo,
fldConsulName,
fldCCGroupCd,
fldCCGroupDesc,
fldCCSortOrder,
fldCCDesc,
fldSubCCNo,
fldSubCCName,
v.fldCostCenterKey,
fldAmount
v.fldPlanKey
v.fldCostCenterKey
v.fldConsulKey
v.fldSubCostCenterKey
FROM
vwREPQICASSCostsByCC v
WHERE
v.fldStageKey = 4
AND v.fldPLanKey IN (30472);

SELECT
fldConsulNo,
fldConsulName,
fldCCGroupCd,
fldCCGroupDesc,
fldCCSortOrder,
fldCCDesc,
fldSubCCNo,
fldSubCCName,
v.fldCostCenterKey,
fldAmount
v.fldPlanKey
v.fldCostCenterKey
v.fldConsulKey
v.fldSubCostCenterKey
FROM
vwREPQICASSCostsByCC v
WHERE
v.fldStageKey = 4
AND v.fldPLanKey IN (SELECT X.fldIMPPlanKey FROM #tmpPlan X);





The SQL Guy @ blogspot

@SeanPearceSQL

About Me
souLTower
souLTower
Ten Centuries
Ten Centuries (1K reputation)Ten Centuries (1K reputation)Ten Centuries (1K reputation)Ten Centuries (1K reputation)Ten Centuries (1K reputation)Ten Centuries (1K reputation)Ten Centuries (1K reputation)Ten Centuries (1K reputation)

Group: General Forum Members
Points: 1029 Visits: 563
I've run this test previously with similar results. I've attached the execution plan. I removed some columns so that we're just dealing with the view in question.

Thanks! I appreciate the help.
ST
Attachments
Execution2.sqlplan (7 views, 283.00 KB)
ScottPletcher
ScottPletcher
SSCoach
SSCoach (19K reputation)SSCoach (19K reputation)SSCoach (19K reputation)SSCoach (19K reputation)SSCoach (19K reputation)SSCoach (19K reputation)SSCoach (19K reputation)SSCoach (19K reputation)

Group: General Forum Members
Points: 19745 Visits: 7410
Sean Pearce (1/25/2013)
PLEASE NOTE: You have not aliased all your columns so I have no idea which tables they come from. You must edit the following queries to remove any non-aliased columns that are not from vwREPQICASSCostsByCC.



Exactly! As I noted above, that's why I didn't try a re-write yet. I'm certain the query needs at least some re-write, but w/o knowing which table/view every column comes from, it's like coding in the dark.

So, good general rules to follow are:

1) Always alias tables when using multiple tables/views in one query;
2) Always prefix every column with the appropriate alias when doing joins.

This not only helps anyone else looking at the query, it can help you if/when you have to look at it 6 mths later and may not remember all the table details yourself.

SQL DBA,SQL Server MVP(07, 08, 09) Prosecutor James Blackburn, in closing argument in the Fatal Vision murders trial:
If in the future, you should cry a tear, cry one for them [the murder victims]. If in the future, you should say a prayer, say one for them. And if in the future, you should light a candle, light one for them.
Sean Pearce
Sean Pearce
SSCarpal Tunnel
SSCarpal Tunnel (4.1K reputation)SSCarpal Tunnel (4.1K reputation)SSCarpal Tunnel (4.1K reputation)SSCarpal Tunnel (4.1K reputation)SSCarpal Tunnel (4.1K reputation)SSCarpal Tunnel (4.1K reputation)SSCarpal Tunnel (4.1K reputation)SSCarpal Tunnel (4.1K reputation)

Group: General Forum Members
Points: 4078 Visits: 3436
souLTower (1/25/2013)
I've run this test previously with similar results. I've attached the execution plan. I removed some columns so that we're just dealing with the view in question.

Thanks! I appreciate the help.
ST

My original response still holds true. The view definition is not giving the optimizer a chance to find the optimum plan.

StatementOptmEarlyAbortReason="TimeOut"



What does the execution plan for the following look like?

SELECT
v.fldConsulKey,
v.fldPlanKey
FROM
tblConsul v
WHERE
v.fldPLanKey IN (SELECT X.fldIMPPlanKey FROM #tmpPlan X);

SELECT
v.fldConsulKey,
v.fldPlanKey
FROM
tblConsul v
WHERE
v.fldPLanKey IN (30472);





The SQL Guy @ blogspot

@SeanPearceSQL

About Me
souLTower
souLTower
Ten Centuries
Ten Centuries (1K reputation)Ten Centuries (1K reputation)Ten Centuries (1K reputation)Ten Centuries (1K reputation)Ten Centuries (1K reputation)Ten Centuries (1K reputation)Ten Centuries (1K reputation)Ten Centuries (1K reputation)

Group: General Forum Members
Points: 1029 Visits: 563
Thanks for the response. I've attached the execution plans.

I find it weird that the optimizer chose in both cases to use the non-clustered index for the seek. The table has a clustered index on fldPlanKey precisely to avoid this.

IDX_tblConsul_PlanKey clustered located on PRIMARY fldPlanKey
Attachments
Execution3.sqlplan (7 views, 10.00 KB)
Sean Pearce
Sean Pearce
SSCarpal Tunnel
SSCarpal Tunnel (4.1K reputation)SSCarpal Tunnel (4.1K reputation)SSCarpal Tunnel (4.1K reputation)SSCarpal Tunnel (4.1K reputation)SSCarpal Tunnel (4.1K reputation)SSCarpal Tunnel (4.1K reputation)SSCarpal Tunnel (4.1K reputation)SSCarpal Tunnel (4.1K reputation)

Group: General Forum Members
Points: 4078 Visits: 3436
Can you please supply DDL for tblConsul and #tmpPlan, including all indexes.

While you are there, can you supply the view definition.



The SQL Guy @ blogspot

@SeanPearceSQL

About Me
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search