Solved

Order of Select

Posted on 2014-01-27
7
253 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 142

Assisted Solution

by:Guy Hengel [angelIII / a3]
Guy Hengel [angelIII / a3] earned 167 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 142

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
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

 
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:ScottPletcher
ScottPletcher earned 166 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 142

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 167 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

Backup Your Microsoft Windows Server®

Backup all your Microsoft Windows Server – on-premises, in remote locations, in private and hybrid clouds. Your entire Windows Server will be backed up in one easy step with patented, block-level disk imaging. We achieve RTOs (recovery time objectives) as low as 15 seconds.

Question has a verified solution.

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

This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
The Delta outage: 650 cancelled flights, more than 1200 delayed flights, thousands of frustrated customers, tens of millions of dollars in damages – plus untold reputational damage to one of the world’s most trusted airlines. All due to a catastroph…
This video shows, step by step, how to configure Oracle Heterogeneous Services via the Generic Gateway Agent in order to make a connection from an Oracle session and access a remote SQL Server database table.
Via a live example, show how to backup a database, simulate a failure backup the tail of the database transaction log and perform the restore.

939 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now