 |
|
 |
|
Next: multiply string with a comma
|
| Author |
Message |
External

Since: Nov 05, 2004 Posts: 75
|
(Msg. 1) Posted: Tue Aug 10, 2010 9:55 pm
Post subject: Question on anatomy of a query Archived from groups: microsoft>public>sqlserver>programming (more info?)
|
|
|
I was looking at an article by Plamen Ratchev (which I had printed a while
ago but can't find it online anymore) where he states that the first thing a
query does if there are 2 tables involved is create a Cartesian Product
(cross Join) between each table. Then the ON filter is applied.
If Table A has 20,000,000 rows and Table B has 30,000,000 rows - the system
would read all the rows and put all the combinations together to get
600,000,000,000,000 rows? Then apply the ON filter???
Where would it put the data it just read?
Does it read all this into a temporary table in memory?
Then when it applies the ON predicate, does it them move the rows that match
to another temporary table?
And if an outer join, move the other rows to the 2nd temporary table?
Thanks,
Tom >> Stay informed about: Question on anatomy of a query |
|
| Back to top |
|
 |  |
External

Since: May 30, 2004 Posts: 1649
|
(Msg. 2) Posted: Wed Aug 11, 2010 4:25 am
Post subject: Re: Question on anatomy of a query [Login to view extended thread Info.] Archived from groups: per prev. post (more info?)
|
|
|
tshad (tfs@dslextreme.com) writes:
> I was looking at an article by Plamen Ratchev (which I had printed a
> while ago but can't find it online anymore) where he states that the
> first thing a query does if there are 2 tables involved is create a
> Cartesian Product (cross Join) between each table. Then the ON filter is
> applied.
>
> If Table A has 20,000,000 rows and Table B has 30,000,000 rows - the
> system would read all the rows and put all the combinations together to
> get 600,000,000,000,000 rows? Then apply the ON filter???
That is what would happen if there wasn't an optimizer.
Keep in mind that SQL is a *declarative* language. You tell what want. The
DBMS figures out how to perform the operation in the most efficient manner.
But in order to get you what you want, you need to understand the rules
the computer works according to, and those are the rules than Plamen
describes.
The optimizer is free to recast the computation order as long as the result
is guaranteed to be the same.
--
Erland Sommarskog, SQL Server MVP, esquel.TakeThisOut@sommarskog.se
Books Online for SQL Server 2005 at
http://www.microsoft.com/technet/prodtechnol/sql/2005/downloads/books.mspx
Books Online for SQL Server 2000 at
http://www.microsoft.com/sql/prodinfo/previousversions/books.mspx >> Stay informed about: Question on anatomy of a query |
|
| Back to top |
|
 |  |
External

Since: Nov 05, 2004 Posts: 75
|
(Msg. 3) Posted: Wed Aug 11, 2010 9:18 am
Post subject: Re: Question on anatomy of a query [Login to view extended thread Info.] Archived from groups: per prev. post (more info?)
|
|
|
"Erland Sommarskog" wrote in message
> tshad (tfs@dslextreme.com) writes:
>> I was looking at an article by Plamen Ratchev (which I had printed a
>> while ago but can't find it online anymore) where he states that the
>> first thing a query does if there are 2 tables involved is create a
>> Cartesian Product (cross Join) between each table. Then the ON filter is
>> applied.
>>
>> If Table A has 20,000,000 rows and Table B has 30,000,000 rows - the
>> system would read all the rows and put all the combinations together to
>> get 600,000,000,000,000 rows? Then apply the ON filter???
>
> That is what would happen if there wasn't an optimizer.
>
Why?
Why wouldn't it just read both tables and take the rows that match the ON
filter?
> Keep in mind that SQL is a *declarative* language. You tell what want. The
> DBMS figures out how to perform the operation in the most efficient
> manner.
>
> But in order to get you what you want, you need to understand the rules
> the computer works according to, and those are the rules than Plamen
> describes.
>
> The optimizer is free to recast the computation order as long as the
> result
> is guaranteed to be the same.
>
I agree that that is what happens which is why it is confusing when thinking
about how joins work. I have always been told that you do the joins left to
right and that each join becomes the left condition of the next join.
For example,
Select *
From tableA a
join tableB b on a.id = b.id
join tablec c on b.id = c.id
join tabled d on c.id = d.id
where something.
In this case, a is joined with b, b is joined with the result of a and b, d
is joined with the results of a and b and c.
If the optimizer changed the order this is done then couldn't you possibly
have a different result, especially if dealing with left joins or there is
multiple criteria in one of the joins.
Thanks,
Tom
>
> --
> Erland Sommarskog, SQL Server MVP, esquel DeleteThis @sommarskog.se
>
> Books Online for SQL Server 2005 at
> http://www.microsoft.com/technet/prodtechnol/sql/2005/downloads/books.mspx
> Books Online for SQL Server 2000 at
> http://www.microsoft.com/sql/prodinfo/previousversions/books.mspx >> Stay informed about: Question on anatomy of a query |
|
| Back to top |
|
 |  |
External

Since: Mar 21, 2009 Posts: 4
|
(Msg. 4) Posted: Wed Aug 11, 2010 11:10 am
Post subject: Re: Question on anatomy of a query [Login to view extended thread Info.] Archived from groups: per prev. post (more info?)
|
|
|
You're probably talking about the following article:
http://pratchev.blogspot.com/2007/05/anatomy-of-query.html .
In this article, Platmen make first the explicit note that this is the
logical processing of a query and that it doesn't necessarily reflect it's
physical processing.
--
Sylvain Lafontaine, ing.
MVP - Access
Blog/web site: http://coding-paparazzi.sylvainlafontaine.com
Independent consultant and remote programming for Access and SQL-Server
(French)
"tshad" wrote in message
>I was looking at an article by Plamen Ratchev (which I had printed a while
>ago but can't find it online anymore) where he states that the first thing
>a query does if there are 2 tables involved is create a Cartesian Product
>(cross Join) between each table. Then the ON filter is applied.
>
> If Table A has 20,000,000 rows and Table B has 30,000,000 rows - the
> system would read all the rows and put all the combinations together to
> get 600,000,000,000,000 rows? Then apply the ON filter???
>
> Where would it put the data it just read?
>
> Does it read all this into a temporary table in memory?
>
> Then when it applies the ON predicate, does it them move the rows that
> match to another temporary table?
>
> And if an outer join, move the other rows to the 2nd temporary table?
>
> Thanks,
>
> Tom
>
> >> Stay informed about: Question on anatomy of a query |
|
| Back to top |
|
 |  |
External

Since: Nov 05, 2004 Posts: 75
|
(Msg. 5) Posted: Wed Aug 11, 2010 11:10 am
Post subject: Re: Question on anatomy of a query [Login to view extended thread Info.] Archived from groups: per prev. post (more info?)
|
|
|
"Sylvain Lafontaine" wrote in message
> You're probably talking about the following article:
> http://pratchev.blogspot.com/2007/05/anatomy-of-query.html .
>
Actually it is only the first part of the article.
The one I had copied was much longer with examples.
One of the examples had to do placing a filter as part of the ON clause or
as part of the WHERE clause and that it makes a difference in an outer join,
which I assume would not matter in an inner join.
> In this article, Platmen make first the explicit note that this is the
> logical processing of a query and that it doesn't necessarily reflect it's
> physical processing.
But that is what is confusing. Why would you say that logically you put all
the combinations together and then take the ones that match. Logically, I
would think you would say that both tables are read and only the ones that
match are put into a temporary table.
Just curious. I like the article but that part kind of threw me.
Thanks,
Tom
>
> --
> Sylvain Lafontaine, ing.
> MVP - Access
> Blog/web site: http://coding-paparazzi.sylvainlafontaine.com
> Independent consultant and remote programming for Access and SQL-Server
> (French)
>
>
> "tshad" wrote in message
>
>>I was looking at an article by Plamen Ratchev (which I had printed a while
>>ago but can't find it online anymore) where he states that the first thing
>>a query does if there are 2 tables involved is create a Cartesian Product
>>(cross Join) between each table. Then the ON filter is applied.
>>
>> If Table A has 20,000,000 rows and Table B has 30,000,000 rows - the
>> system would read all the rows and put all the combinations together to
>> get 600,000,000,000,000 rows? Then apply the ON filter???
>>
>> Where would it put the data it just read?
>>
>> Does it read all this into a temporary table in memory?
>>
>> Then when it applies the ON predicate, does it them move the rows that
>> match to another temporary table?
>>
>> And if an outer join, move the other rows to the 2nd temporary table?
>>
>> Thanks,
>>
>> Tom
>>
>>
>
> >> Stay informed about: Question on anatomy of a query |
|
| Back to top |
|
 |  |
External

Since: Apr 17, 2007 Posts: 417
|
(Msg. 6) Posted: Wed Aug 11, 2010 11:10 am
Post subject: Re: Question on anatomy of a query [Login to view extended thread Info.] Archived from groups: per prev. post (more info?)
|
|
|
When you have more of a background, you might want to read some ANSI/
ISO Standards. They are dull, boring and complicated and in a strange
language we called "Standard-speak" when i was on the committee. One
of the technical terms is "effectively" which means that we are about
to describe a way to do something (like that CROSS JOIN thing) which
will have some effect. But any process that produces that same result
is just as good.
I work out a full SELECT statement in SQL FOR SMARTIES as per the
formal process just to show how this could be done. >> Stay informed about: Question on anatomy of a query |
|
| Back to top |
|
 |  |
External

Since: Mar 21, 2009 Posts: 4
|
(Msg. 7) Posted: Wed Aug 11, 2010 2:03 pm
Post subject: Re: Question on anatomy of a query [Login to view extended thread Info.] Archived from groups: per prev. post (more info?)
|
|
|
This is important when you want to understand the old ANSI syntax for the
Outer Join: *= and =*.
Contrary to what many people think, *= is not identical with a Left Join and
=* is not identical with a Right Join. If you have a single *= or =* in
your query then yes, remplacing it with a modern Left or Right Join will
give you the same result. However, if you have more than one then replacing
them with their corresponding Left or Right Join can give a vastly different
result.
This is because all the Cross Join are done before all the *= and =* are
applied at the same time in a single operation after that. In the case of
the modern Left and Right Join, all the joins are performed consecutively
(one after the other); so when you have more than one join, in many cases,
you'll get a different result between a combination of multiples *= and/or
=* in comparaison to a combination of the modern Left and Right join.
In the case of modern outer joins, there is no need to go into this logical
application of a Cross Join but in the case of the older Ansi outer joins,
yes, it is.
--
Sylvain Lafontaine, ing.
MVP - Access
Blog/web site: http://coding-paparazzi.sylvainlafontaine.com
Independent consultant and remote programming for Access and SQL-Server
(French)
"tshad" wrote in message
>
> "Sylvain Lafontaine" wrote in message
>
>> You're probably talking about the following article:
>> http://pratchev.blogspot.com/2007/05/anatomy-of-query.html .
>>
> Actually it is only the first part of the article.
>
> The one I had copied was much longer with examples.
>
> One of the examples had to do placing a filter as part of the ON clause or
> as part of the WHERE clause and that it makes a difference in an outer
> join, which I assume would not matter in an inner join.
>
>> In this article, Platmen make first the explicit note that this is the
>> logical processing of a query and that it doesn't necessarily reflect
>> it's physical processing.
>
> But that is what is confusing. Why would you say that logically you put
> all the combinations together and then take the ones that match.
> Logically, I would think you would say that both tables are read and only
> the ones that match are put into a temporary table.
>
> Just curious. I like the article but that part kind of threw me.
>
> Thanks,
>
> Tom
>
>>
>> --
>> Sylvain Lafontaine, ing.
>> MVP - Access
>> Blog/web site: http://coding-paparazzi.sylvainlafontaine.com
>> Independent consultant and remote programming for Access and SQL-Server
>> (French)
>>
>>
>> "tshad" wrote in message
>>
>>>I was looking at an article by Plamen Ratchev (which I had printed a
>>>while ago but can't find it online anymore) where he states that the
>>>first thing a query does if there are 2 tables involved is create a
>>>Cartesian Product (cross Join) between each table. Then the ON filter is
>>>applied.
>>>
>>> If Table A has 20,000,000 rows and Table B has 30,000,000 rows - the
>>> system would read all the rows and put all the combinations together to
>>> get 600,000,000,000,000 rows? Then apply the ON filter???
>>>
>>> Where would it put the data it just read?
>>>
>>> Does it read all this into a temporary table in memory?
>>>
>>> Then when it applies the ON predicate, does it them move the rows that
>>> match to another temporary table?
>>>
>>> And if an outer join, move the other rows to the 2nd temporary table?
>>>
>>> Thanks,
>>>
>>> Tom
>>>
>>>
>>
>>
>
> >> Stay informed about: Question on anatomy of a query |
|
| Back to top |
|
 |  |
External

Since: Nov 05, 2004 Posts: 75
|
(Msg. 8) Posted: Wed Aug 11, 2010 5:03 pm
Post subject: Re: Question on anatomy of a query [Login to view extended thread Info.] Archived from groups: per prev. post (more info?)
|
|
|
"--CELKO--" wrote in message
> When you have more of a background, you might want to read some ANSI/
> ISO Standards. They are dull, boring and complicated and in a strange
> language we called "Standard-speak" when i was on the committee. One
> of the technical terms is "effectively" which means that we are about
> to describe a way to do something (like that CROSS JOIN thing) which
> will have some effect. But any process that produces that same result
> is just as good.
>
> I work out a full SELECT statement in SQL FOR SMARTIES as per the
> formal process just to show how this could be done.
I'll have to check that out when I get home.
Thanks,
Tom >> Stay informed about: Question on anatomy of a query |
|
| Back to top |
|
 |  |
External

Since: May 30, 2004 Posts: 1649
|
(Msg. 9) Posted: Wed Aug 11, 2010 5:25 pm
Post subject: Re: Question on anatomy of a query [Login to view extended thread Info.] Archived from groups: per prev. post (more info?)
|
|
|
|
|
| Back to top |
|
 |  |
External

Since: Nov 05, 2004 Posts: 75
|
(Msg. 10) Posted: Wed Aug 11, 2010 5:25 pm
Post subject: Re: Question on anatomy of a query [Login to view extended thread Info.] Archived from groups: per prev. post (more info?)
|
|
|
|
|
| Back to top |
|
 |  |
External

Since: Nov 05, 2004 Posts: 75
|
(Msg. 11) Posted: Wed Aug 11, 2010 11:25 pm
Post subject: Re: Question on anatomy of a query [Login to view extended thread Info.] Archived from groups: per prev. post (more info?)
|
|
|
And what about the question of the JOINS:
I agree that that is what happens which is why it is confusing when thinking
about how joins work. I have always been told that you do the joins left to
right and that each join becomes the left condition of the next join.
For example,
Select *
From tableA a
join tableB b on a.id = b.id
join tablec c on b.id = c.id
join tabled d on c.id = d.id
where something.
In this case, a is joined with b, b is joined with the result of a and b, d
is joined with the results of a and b and c.
If the optimizer changed the order this is done then you could possibly
have a different result, especially if dealing with left joins or there is
multiple criteria in one of the joins.
Thanks,
To
"tshad" wrote in message
>
> "Erland Sommarskog" wrote in message
>
>> tshad (tfs@dslextreme.com) writes:
>>> Why?
>>>
>>> Why wouldn't it just read both tables and take the rows that match the
>>> ON
>>> filter?
>>
>> But how it read them? How would it know which fits together? Logically
>> the
>> only way to do this is to create all combinations and then filter.
>>
>
> Makes sense.
>
> Thanks,
>
> Tom
>
>> Oh, in practice the optimizer does it another way, but here is the matter
>> of finding a neutral description.
>>
>> But as Celko points out, the SQL standards are written in a very
>> formalistic
>> language, and they are defintely not trivial to understand.
>>
>> --
>> Erland Sommarskog, SQL Server MVP, esquel.TakeThisOut@sommarskog.se
>>
>> Links for SQL Server Books Online:
>> SQL 2008: http://msdn.microsoft.com/en-us/sqlserver/cc514207.aspx
>> SQL 2005: http://msdn.microsoft.com/en-us/sqlserver/bb895970.aspx
>> SQL 2000:
>> http://www.microsoft.com/sql/prodinfo/previousversions/books.mspx
>>
>
> >> Stay informed about: Question on anatomy of a query |
|
| Back to top |
|
 |  |
External

Since: May 30, 2004 Posts: 1649
|
(Msg. 12) Posted: Thu Aug 12, 2010 4:25 am
Post subject: Re: Question on anatomy of a query [Login to view extended thread Info.] Archived from groups: per prev. post (more info?)
|
|
|
tshad (tfs@dslextreme.com) writes:
> For example,
>
> Select *
> From tableA a
> join tableB b on a.id = b.id
> join tablec c on b.id = c.id
> join tabled d on c.id = d.id
> where something.
>
> In this case, a is joined with b, b is joined with the result of a and
> b, d is joined with the results of a and b and c.
>
> If the optimizer changed the order this is done then you could possibly
> have a different result, especially if dealing with left joins or there is
> multiple criteria in one of the joins.
No. An inner join is just like addition and multiplication a commutative
operator. Meaning that "A JOIN B" <=> "B JOIN A".
With left and right joins this is obviously not the case.
--
Erland Sommarskog, SQL Server MVP, esquel RemoveThis @sommarskog.se
Books Online for SQL Server 2005 at
http://www.microsoft.com/technet/prodtechnol/sql/2005/downloads/books.mspx
Books Online for SQL Server 2000 at
http://www.microsoft.com/sql/prodinfo/previousversions/books.mspx >> Stay informed about: Question on anatomy of a query |
|
| Back to top |
|
 |  |
External

Since: Aug 20, 2008 Posts: 210
|
(Msg. 13) Posted: Thu Aug 12, 2010 8:07 am
Post subject: Re: Question on anatomy of a query [Login to view extended thread Info.] Archived from groups: per prev. post (more info?)
|
|
|
You are probably mixing up the two articles, for logical query
processing (http://pratchev.blogspot.com/2007/05/anatomy-of-
query.html) and for outer joins (http://pratchev.blogspot.com/2009/06/
outer-joins.html). As it has been already said, logical and physical
processing differ a lot. Logical is more like the description of the
logical flow to extract data, physical is how it is really done by the
query engine. The separation of the cross join and matching on join
predicates is done to separate the different steps logically. But when
executed in reality some predicates may be applied much earlier before
the join.
--
Plamen Ratchev
http://www.SQLStudio.com >> Stay informed about: Question on anatomy of a query |
|
| Back to top |
|
 |  |
External

Since: Nov 05, 2004 Posts: 75
|
(Msg. 14) Posted: Thu Aug 12, 2010 8:17 am
Post subject: Re: Question on anatomy of a query [Login to view extended thread Info.] Archived from groups: per prev. post (more info?)
|
|
|
"Erland Sommarskog" wrote in message
> tshad (tfs@dslextreme.com) writes:
>> For example,
>>
>> Select *
>> From tableA a
>> join tableB b on a.id = b.id
>> join tablec c on b.id = c.id
>> join tabled d on c.id = d.id
>> where something.
>>
>> In this case, a is joined with b, b is joined with the result of a and
>> b, d is joined with the results of a and b and c.
>>
>> If the optimizer changed the order this is done then you could possibly
>> have a different result, especially if dealing with left joins or there
>> is
>> multiple criteria in one of the joins.
>
> No. An inner join is just like addition and multiplication a commutative
> operator. Meaning that "A JOIN B" <=> "B JOIN A".
Right
but would:
A JOIN B
B JOIN C
C JOIN D
be the same thing as
A JOIN B
C JOIN D
B JOIN C
If so, the 3rd join IS NOT joining against the previous result set.
In otherwords, would the first example be:
(A JOIN B
B JOIN C) a1
D JOIN a1
Where D is being joined with the result of the 1st 2 joins so only joining
with the rows in C that matched with the rows in B that matched with the
rows in A.
This could give less rows that the original set than if D were JOIN'd with
the whole table of C.
And if result sets were joined with each other then the 2nd example would
have the results of A JOIN B join'd with the result set of C JOIN B.
Of course, if previous result sets are not used that this is all mute and it
doesn't matter what the order is.
Thanks,
Tom
>
> With left and right joins this is obviously not the case.
>
> --
> Erland Sommarskog, SQL Server MVP, esquel.TakeThisOut@sommarskog.se
>
> Books Online for SQL Server 2005 at
> http://www.microsoft.com/technet/prodtechnol/sql/2005/downloads/books.mspx
> Books Online for SQL Server 2000 at
> http://www.microsoft.com/sql/prodinfo/previousversions/books.mspx >> Stay informed about: Question on anatomy of a query |
|
| Back to top |
|
 |  |
External

Since: Aug 20, 2008 Posts: 210
|
(Msg. 15) Posted: Thu Aug 12, 2010 8:51 am
Post subject: Re: Question on anatomy of a query [Login to view extended thread Info.] Archived from groups: per prev. post (more info?)
|
|
|
|
|
| Back to top |
|
 |  |
| Related Topics: | Query question - I have 2 tables. One table (sales) has a salesid and sales amount. The other table (salesrep) has a salesid and name. I want to write a query that will return the max sales amount and the name of the salerep who's responsible for it. The following..
SQL Query Question - I wrote a stored procedure that uses a cursor and a temp table to return values. I would like to change this to a single query if it is possible to improve performance. The source data looks like this. Sorted by ID then DateValue. ID ...
aggregate query question - I have a recurring pattern in my queries that I am solving by using CTEs and temp tables and am just wondering if there is a better way. I continually have to extract min or max values that occurred between 2 dates but in addition to the min or max I...
Date query question - Hello All, I would like to get all the records in a table with the date = 8/15/08. The date field is dpolltime which is a datetime field.Here is my query SELECT TOP (100) PERCENT AVG(dbo.StatisticalNumeric.nValue_Avg) AS avg_value,..
Query question with 3 FKs - Novice Query question: I have a table with 3 foreign keys, for example: Notes ID as int CreatedBy int ;FK User_table.ID AssignedTo int ;FK User_table.ID ClosedBy int ;FK User_table.ID Note nvarchar(max) and a talbe of users, for example .... |
|
You can post new topics in this forum You can reply to topics in this forum You can edit your posts in this forum You can delete your posts in this forum You can vote in polls in this forum
|
|
|
|
 |
|
|