all groups > sql server programming > july 2005 >
You're in the

sql server programming

group:

Index Question


Index Question Mike Labosh
7/5/2005 7:32:18 PM
sql server programming: An hour and 15 minutes after I hit send, this still has not even appeared in
the newsgroup, so I am trying again, in case it went into a black hole:

I have these three columns that I want to JOIN from TableA in this order:

Vendor NVARCHAR(255) NULL,
MachineType NVARCHAR(255) NULL,
Model NVARCHAR(255) NULL

To this column in TableB:

Identifier NVARCHAR(500) NOT NULL -- has a unique constraint

TableA has 52 million records in it, and it is still busy calculating the
selectivity of values in each of these columns. I want to build a
nonclustered composite index across the three columns in TableA.

Once I determine the selectivity, I will build the composite index so that
the most selective column is the first index key, then the next, then the
last.

Now my question is, does the order of the columns within the composite index
affect the phrasing of JOIN & WHERE operations?

For example, if it turns out that Model is the most selective, followed by
MachineType, followed by Vendor, I would phrase the index this way:

CREATE NONCLUSTERED INDEX SSAHardwareMatchingIX
ON SSA (Model, MachineType, Vendor)

Q1: Am I required to phrase the joins using the columns in the same order?
:

FROM Something s
INNER JOIN SSA
ON SSA.Model = s.Model
AND SSA.MachineType = s.MachineType
AND SSA.Vendor = s.Vendor

Q2: Would the above index be useful for two stored procedures that have to
join like this:

FROM Something s
INNER JOIN SSA
ON (SSA.Vendor + SSA.MachineType + SSA.Model) = s.Identifier

--
Peace & happy computing,

Mike Labosh, MCSD

"Mr. McKittrick, after very careful consideration, I have
come to the conclusion that this new system SUCKS."
-- General Barringer, "War Games"

RE: Index Question SlowLearner
7/5/2005 8:51:04 PM
Hi
Answer for Q1:
You don't need to have columns in where condition in the same order as it
exists in index. SQLServer is not order specific.

Answer for Q2:
SQLServer will certainly use the index SSAHardwareMatchingIX in this case
also. But in this case i would recommend you to have a view (with
schemabinding/index on it) and then use the rather than a base table.

Have a good day.

[quoted text, click to view]
Re: Index Question Steve Kass
7/6/2005 1:05:57 AM
Mike,

The order of terms in the join condition is unlikely to matter here.
When a query involves many tables or tables with many potentially
helpful indexes, the optimizer cannot consider all potential query plans,
and the order in which the join is phrased can affect the specific plans
it will consider. Otherwise, it shouldn't matter.

For a join condition
SSA.Vendor + SSA.MachineType + SSA.Model) = s.Identifier

the index will not be used for its ordering of keys, regardless of
the column order of the index. The index may be useful if the entire
query references only these three columns of SSA and the clustered
index key of SSA, and the index rows are narrower than the table
rows.

It's possible that an index on these three columns would be useful
if it were declared in (Vendor,MachineType,Model) order and the
condition were written with a condition that referred to Vendor
alone, such as

SSA.Vendor < s.Identifier
and
SSA.Vendor + SSA.MachineType + SSA.Model) = s.Identifier

or something similar.

Steve Kass
Drew University

[quoted text, click to view]
AddThis Social Bookmark Button