Solved

Order of Select

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

Control application downtime with dependency maps

Visualize the interdependencies between application components better with Applications Manager's automated application discovery and dependency mapping feature. Resolve performance issues faster by quickly isolating problematic components.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Scheduling Jobs for Execution: 4 16
Sql query to Stored Procedure 6 20
Test a query 23 19
select over clause 1 10
In this article I will describe the Detach & Attach method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
In this article I will describe the Copy Database Wizard method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
Via a live example, show how to extract information from SQL Server on Database, Connection and Server properties
Via a live example, show how to shrink a transaction log file down to a reasonable size.

747 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

12 Experts available now in Live!

Get 1:1 Help Now