?
Solved

Order of Select

Posted on 2014-01-27
7
Medium Priority
?
265 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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
Get MySQL database support online, now!

At Percona’s web store you can order your MySQL database support needs in minutes. No hassles, no fuss, just pick and click. Pay online with a credit 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 69

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

Get proactive database performance tuning online

At Percona’s web store you can order full Percona Database Performance Audit in minutes. Find out the health of your database, and how to improve it. Pay online with a credit card. Improve your database performance now!

Question has a verified solution.

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

Why is this different from all of the other step by step guides?  Because I make a living as a DBA and not as a writer and I lived through this experience. Defining the name: When I talk to people they say different names on this subject stuff l…
Recently we ran in to an issue while running some SQL jobs where we were trying to process the cubes.  We got an error saying failure stating 'NT SERVICE\SQLSERVERAGENT does not have access to Analysis Services. So this is a way to automate that wit…
Via a live example, show how to set up a backup for SQL Server using a Maintenance Plan and how to schedule the job into SQL Server Agent.
Viewers will learn how to use the SELECT statement in SQL and will be exposed to the many uses the SELECT statement has.
Suggested Courses

771 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