Click here to monitor SSC
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Logical Query Processing


Logical Query Processing

Author
Message
Lozza
Lozza
SSC Rookie
SSC Rookie (35 reputation)SSC Rookie (35 reputation)SSC Rookie (35 reputation)SSC Rookie (35 reputation)SSC Rookie (35 reputation)SSC Rookie (35 reputation)SSC Rookie (35 reputation)SSC Rookie (35 reputation)

Group: General Forum Members
Points: 35 Visits: 318
Dear all,

It was my understanding that when processing a query the order in which it is processed is FROM, JOIN, ON, WHERE, GROUP BY etc. This is also shown in Inside Microsoft SQL Server 2008: T-SQL Querying by Itzik Ben-Gan. However, the query below only runs successfully with the WHERE clause -

SELECT
SC.i5_n_oor
, EC.CountryName
FROM
extract.i5_5_synd_clm AS SC
INNER JOIN
extract.i1_o_event AS E
ON
SC.i5_c_evt = E.i1_c_event
INNER JOIN
load.t_Dim_Country AS EC
ON
SUBSTRING(E.i1_s_event,CHARINDEX(',',E.i1_s_event) + 2, (CHARINDEX(' - ',E.i1_s_event) - CHARINDEX(',',E.i1_s_event)) - 2) = EC.CountryName
WHERE
CHARINDEX(', ',E.i1_s_event) > 0
AND CHARINDEX(' - ',E.i1_s_event) > 0
AND CHARINDEX(', ',E.i1_s_event) < CHARINDEX(' - ',E.i1_s_event)

If I remove the WHERE clause I get the following error -
Invalid length parameter passed to the LEFT or SUBSTRING function.

So it seems it is processing the WHERE clause before the JOIN.

Could someone please tell me what is happening? What is the order in which this query is being processed?

Thanks in advance
Koen Verbeeck
Koen Verbeeck
SSCoach
SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)

Group: General Forum Members
Points: 16505 Visits: 13207
The logical processing order specifies a way how the results should be achieved.
However, the query processor can choose any ordering it wants, as long as the end result is the same as the one of the logical processing order.

In some cases the order is different and SQL Server does something that leads to an error, which shouldn't be there if the logical order was followed. In this case it will execute the LEFT and SUBSTRING functions first, before the JOINS and the WHERE clauses, instead of at the end in the SELECT.

I had a similar issue like you once, and I believe I added an index to force some other order. Or you can rewrite your query a bit.



How to post forum questions.
Need an answer? No, you need a question.
What’s the deal with Excel & SSIS?

Member of LinkedIn. My blog at SQLKover.

MCSA SQL Server 2012 - MCSE Business Intelligence
Lozza
Lozza
SSC Rookie
SSC Rookie (35 reputation)SSC Rookie (35 reputation)SSC Rookie (35 reputation)SSC Rookie (35 reputation)SSC Rookie (35 reputation)SSC Rookie (35 reputation)SSC Rookie (35 reputation)SSC Rookie (35 reputation)

Group: General Forum Members
Points: 35 Visits: 318
Thank you very much, that explains it!
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