?
Solved

Order of Select

Posted on 2014-01-27
7
Medium Priority
?
267 Views
Last Modified: 2014-05-15
Curious. If you select one column but not from the table in your from but the table in your join, does that matter? How does the SQL engine handle this? See below

Select
b.Name Id
  From appointments a
 join names b on a.SomeID=b.SomeID
Where A.SomeID = 5000000
0
Comment
Question by:cheryl9063
7 Comments
 
LVL 143

Assisted Solution

by:Guy Hengel [angelIII / a3]
Guy Hengel [angelIII / a3] earned 668 total points
ID: 39813327
I don't understand what exactly you are looking for, but this is "plain" sql and how it is shall work.

as I presume that A.SomeID = xxxx is not the same field as the JOIN .. ON condition fields, it will fetch alll the values for a.SomeID for the join based on the where clause, and then look up all the values in table B (b.SomeID) for that.

if it would really be a.SomeID on the same field, the query shall be simplified to:SELECT b.Name ID from names b where b.someid = 5000000

other than that, you shall check the "explain plan" for your queries to see "how it is running" your queries
0
 
LVL 1

Author Comment

by:cheryl9063
ID: 39813339
Not what I'm asking really. A vendor has a query in which the column in the select statement does not pull from the first table(forget the where).  In looking at BOTH queries below. Which would perform better?

Select b.Name Id
  From appointments a
 join names b on a.SomeID=b.SomeID


Select b.Name Id
  From names b
 join appointments a on a.SomeID=b.SomeID
0
 
LVL 143

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 39813349
such "simple" query both shall run in the same explain plan, hence in the same performance
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 1

Author Comment

by:cheryl9063
ID: 39813358
Are you sure? If we have 34,000,000 rows and multiple indexes both would perform the same?
0
 
LVL 70

Assisted Solution

by:Scott Pletcher
Scott Pletcher earned 664 total points
ID: 39813363
>>  If you select one column but not from the table in your from but the table in your join, does that matter? <<

No.  SQL will internally determine all the columns you need to process on every table referenced.


>> How does the SQL engine handle this? <<

SQL does process certain parts of the SQL before others.  But all you really have to do is index the WHERE conditions when appropriate.  In particular, if you can specify the clustering key, do so, as that will help SQL generate the most efficient plan.
0
 
LVL 143

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 39813398
yes, both would perform the same, normally. as the indexes are "static" (in terms of their existance), the sql engine will first see what indexes are there, which would be the most approriate for the query (checking what you give as input, what it will return as ouput, statistics by the table data and indexes etc, without going into details here), and find for both queries the same explain plan.

please just use the "estimate explain plan" for the 2 queries, it shall show you a graphical display, which should be the same for both. the number of "source records" does not matter.
however, based on server load, if the statistics based on input/ouput data might be very close to some "internal thresholds", your queries might indeed be run differently. but this is very unlikely.
0
 
LVL 34

Accepted Solution

by:
Brian Crowe earned 668 total points
ID: 39813515
Both of the queries:

Select b.Name Id
  From appointments a
 join names b on a.SomeID=b.SomeID


Select b.Name Id
  From names b
 join appointments a on a.SomeID=b.SomeID

Would be treated exactly the same by the query compiler.  You can verify this by turning on the "Include Actual Execution Plan" in the query options.  Both would be served best by the index:

CREATE NONCLUSTERED INDEX IX_Names_SomeID
   ON Names (SomeID)
   INCLUDE (Name)

Including the Name column in the index prevents KEY LOOKUP on the original table to retrieve that value.
0

Featured Post

Vote for the Most Valuable Expert

It’s time to recognize experts that go above and beyond with helpful solutions and engagement on site. Choose from the top experts in the Hall of Fame or on the right rail of your favorite topic page. Look for the blue “Nominate” button on their profile to vote.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

It is possible to export the data of a SQL Table in SSMS and generate INSERT statements. It's neatly tucked away in the generate scripts option of a database.
Microsoft Access has a limit of 255 columns in a single table; SQL Server allows tables with over 255 columns, but reading that data is not necessarily simple.  The final solution for this task involved creating a custom text parser and then reading…
Via a live example, show how to extract information from SQL Server on Database, Connection and Server properties
Viewers will learn how to use the UPDATE and DELETE statements to change or remove existing data from their tables. Make a table: Update a specific column given a specific row using the UPDATE statement: Remove a set of values using the DELETE s…
Suggested Courses

807 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question