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.


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.


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.


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.


Database Considerations

When you design an index, consider the following database guidelines: Large numbers of indexes on a table affect the performance of INSERT, UPDATE, DELETE, and MERGE statements because all indexes must be adjusted appropriately as data in the table changes.

  1. Avoid over-indexing heavily updated tables and keep indexes narrow, that is, with as few columns as possible.

  2. Use many indexes to improve query performance on tables with low update requirements, but large volumes of data. Large numbers of indexes can help the performance of queries that do not modify data, such as SELECT statements, because the query optimizer has more indexes to choose from to determine the fastest access method.

  3. Indexing small tables may not be optimal because it can take the query optimizer longer to traverse the index searching for data than to perform a simple table scan. Therefore, indexes on small tables might never be used, but must still be maintained as data in the table changes.

  4. Indexes on views can provide significant performance gains when the view contains aggregations, table joins, or a combination of aggregations and joins. The view does not have to be explicitly referenced in the query for the query optimizer to use it.

--Stay_Safe--


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.


Need Your Help

Is there a CSS/HTML way to cut multiple shapes out of the top/bottom of a div?

html css css3 css-shapes

I'm honestly not sure where to begin on this one (I'm a graphic designer digging a bit deeper into HTML/CSS, but my current experience is rather slim, so this problem is beyond my ability at the mo...

Form items poitioning issue

html css

I have a form where there is only textfield and a button. They are on the same row and first comes the texttfield and then a button on the right. The thing is, that the button expands based on the