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
Solved

Order of Select

Posted on 2014-01-27
7
260 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 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 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
Optimizing Cloud Backup for Low Bandwidth

With cloud storage prices going down a growing number of SMBs start to use it for backup storage. Unfortunately, business data volume rarely fits the average Internet speed. This article provides an overview of main Internet speed challenges and reveals backup best practices.

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

Resolve Critical IT Incidents Fast

If your data, services or processes become compromised, your organization can suffer damage in just minutes and how fast you communicate during a major IT incident is everything. Learn how to immediately identify incidents & best practices to resolve them quickly and effectively.

Question has a verified solution.

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

Suggested Solutions

Use this article to create a batch file to backup a Microsoft SQL Server database to a Windows folder.  The folder can be on the local hard drive or on a network share.  This batch file will query the SQL server to get the current date & time and wi…
I have a large data set and a SSIS package. How can I load this file in multi threading?
Using examples as well as descriptions, and references to Books Online, show the different Recovery Models available in SQL Server and explain, as well as show how full, differential and transaction log backups are performed
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…

766 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