17.06.2013 Views

Beginning Microsoft SQL Server 2008 ... - S3 Tech Training

Beginning Microsoft SQL Server 2008 ... - S3 Tech Training

Beginning Microsoft SQL Server 2008 ... - S3 Tech Training

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

We can figure out which BusinessEntityID is which just by looking at what table we selected first and<br />

matching it with the first BusinessEntityID column that shows up, but this is tedious at best, and at<br />

worst, prone to errors. That’s one of many reasons why using the plain * operator in JOINs is ill-advised.<br />

In the case of an INNER JOIN, however, it’s not really that much of a problem because we know that<br />

both BusinessEntityID columns, even though they came from different tables, will be exact duplicates<br />

of each other. How do we know that? Think about it — since we’re doing an INNER JOIN on those two<br />

columns, they have to match or the record wouldn’t have been returned! Don’t get in the habit of counting<br />

on this, however. When we look at other JOIN types, we’ll find that we can’t depend on the JOIN<br />

values being equal.<br />

As for all columns being returned from both tables, that is as expected. We used the * operator, which as<br />

we’ve learned before is going to return all columns to us. As I mentioned earlier, the use of the * operator<br />

in joins is a bad habit. It’s quick and easy, but it’s also dirty — it is error-prone and can result in poor<br />

performance.<br />

As I indicated back in Chapter 3, one good principle to adopt early on is to select what you need and<br />

need what you select. What I’m getting at here is that every additional record or column that you return<br />

takes up additional network bandwidth and often additional query processing on your <strong>SQL</strong> <strong>Server</strong>. The<br />

upshot is that selecting unnecessary information hurts performance not only for the current user, but<br />

also for every other user of the system and for users of the network on which the <strong>SQL</strong> <strong>Server</strong> resides.<br />

Select only the columns that you are going to be using and make your WHERE clause as restrictive as<br />

possible.<br />

If you insist on using the * operator, you should use it only for the tables from which you need all the<br />

columns. That’s right — the * operator can be used on a per-table basis. For example, if we wanted all of<br />

the base information for our contact, but only needed the Employee table to figure out their JobTitle,<br />

we could have changed your query to read:<br />

SELECT Person.BusinessEntity.*, JobTitle<br />

FROM Person.BusinessEntity<br />

INNER JOIN HumanResources.Employee<br />

ON Person.BusinessEntity.BusinessEntityID =<br />

HumanResources.Employee.BusinessEntityID<br />

If you scroll over to the right in the results of this query, you’ll see that most of the Employee-related<br />

information is now gone. Indeed, we also only have one instance of the BusinessEntityID column.<br />

What we get in our result set contains all the columns from the BusinessEntity table (since we used<br />

the * qualified for just that table — your one instance of BusinessEntityID came from this part of the<br />

SELECT list) and the only column that had the name JobTitle (which happened to be from the<br />

Employee table). Now let’s try it again, with only one slight change:<br />

SELECT Person.BusinessEntity.*, BusinessEntityID<br />

FROM Person.BusinessEntity<br />

INNER JOIN HumanResources.Employee<br />

ON Person.BusinessEntity.BusinessEntityID =<br />

HumanResources.Employee.BusinessEntityID<br />

Chapter 4: JOINs<br />

85

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!