views:

375

answers:

6

I have an MS SQL server application where I have defined my relationships and primary keys.

However do I need to further define indexes on relationship fields which are sometimes not used in joins and just as part of a where clause?

I am working on the assumption that defining a relationship creates an index, which the sql engine can reuse.

A: 

I would start by making sure that every PK and FK has an index.

Further to that, I have found that using the Index Tuning Wizard in SSMS provides excellent recommendations when you feed it the right information.

Galwegian
A: 

Indexes aren't very expensive, and speed up queries more than you realize. I would recommend adding indexes to all key and non-key fields that are often used in queries. You can even use the execution plan to recommend additional indexes that would speed up your queries.

The only point where indexes aren't in your favour is when you're doing large amounts of data inserts. Each insert requires each index in a table to be updated along with the table's data.

You can opt to wait until the application is running and you have some known queries against the database that you want to improve, or you could do it now, if you have a good idea.

Kieveli
Too many indexes can confuse the dbms into building a sub-optimal access plan!Also for small tables and queries where you want more than 50% of hte data from a given table than a simple scan is more efficient than indexed access.
James Anderson
Are you sure about that, James? As I understand it, the heuristic process whereby the database engine picks a query plan should use stats to choose whether to use an index or not, so extra indexes should just be ignored.
Ian Varley
Indexes are expensive - to inserts, deletes and updates. Extra indexes can slow these down considerably.
HLGEM
+1  A: 

Like everything in the programming world, it depends. You obviously want to create indexes and relationships to preserve normalization and speed up database lookups. But you also want to balance that by not having too many indexes that it will take SQL Server more time to build every index. Also the more indexes you have the more fragmentation that can occur in your database.

So what I do is put in the obvious indexes and relationships and then optimize after the application is build on the possible slow queries.

Nick Berardi
+4  A: 

No indexes will be automatically created on foreign keys constraint. But unique and primary key constraints will create theirs.

Creating indexes on the queries you use, be it on joins or on the WHERE clause is the way to go.

Johan Buret
+1  A: 

Defining a relationship does not create the index.

Usually in places where you have a where clause against some field you want an index but be careful not to just throw indexes out all over the place because they can and do have an effect on insert/update performance.

keithwarren7
+5  A: 

Some very thick books have been written on this subject!

Here are some ruiles of thumb:-

Dont bother indexing (apart from PK) any table with < 1000 rows.

Otherwise index all your FKs.

Examine your SQL and look for the where clauses that will most reduce your result sets and index that columun.

eg. given:

    SELECT OWNER FROM CARS WHERE COLOUR = 'RED' AND MANUFACTURER = "BMW" AND ECAP = "2.0";

You may have 5000 red cars out of 20,000 so indexing this wont help much. However you may only have 100 BMWs so indexing MANUFACURER will immediatly reduce you result set to 100 and you can eliminate the the blue and white cars by simply scanning through the hundred rows.

Generally the dbms will pick one or two of the indexes available based on cardinality so it pays to second guess and define only those indexes that are likely to be used.

James Anderson
I agree. I've upvoted this answer, more detailed than mine.
Johan Buret