Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

When should you consider indexing your sql tables?

Tags:

sql

indexing

How many records should there be before I consider indexing my sql tables?

like image 588
zsharp Avatar asked Feb 07 '09 02:02

zsharp


People also ask

When should you index a SQL database?

Indexes are used to retrieve data from the database more quickly than otherwise. The users cannot see the indexes, they are just used to speed up searches/queries. Note: Updating a table with indexes takes more time than updating a table without (because the indexes also need an update).

When should you index data?

Indexes are used to quickly locate data without having to search every row in a database table every time a database table is accessed. Indexes can be created using one or more columns of a database table, providing the basis for both rapid random lookups and efficient access of ordered records.

When index is used in SQL?

A SQL index is used to retrieve data from a database very fast. Indexing a table or view is, without a doubt, one of the best ways to improve the performance of queries and applications. A SQL index is a quick lookup table for finding records users need to search frequently.


2 Answers

There's no good reason to forego obvious indexes (FKs, etc.) when you're creating the table. It will never noticeably affect performance to have unnecessary indexes on tiny tables, and it's good to take a first cut when your mind is into schema design. Also, some indexes serve to prevent duplicates, which can be useful regardless of table size.

I guess the proper answer to your question is that the number of records in the table should have nothing to do with when to create indexes.

like image 180
dkretz Avatar answered Sep 30 '22 23:09

dkretz


I would create the index entries when I create my table. If you decide to create indices after the table has grown to 100, 1000, 100000 entries it can just take alot of time and perhaps make your database unavailable while you are doing it.

Think about the table first, create the indices you think you'll need, and then move on.

In some cases you will discover that you should have indexed a column, if thats the case, fix it when you discover it.

Creating an index on a searched field is not a pre-optimization, its just what should be done.

like image 28
Gregor Brandt Avatar answered Sep 30 '22 22:09

Gregor Brandt