SQL Relationships and indexes

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.

Answers


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.


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.


Need Your Help

Is it possible to add multiple locations in bulk to a Google Map under My Places?

xml google-maps kml

I have a KML file with 52 locations, but when I import it into Google Maps, I only get the last point.

How to call function with identical params?

php oop

I'm trying to call a grandparent's function.

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.