GRACEFUL WAY OF USING INDEXES
Indexes are a special kind of lookup table that is used by the database search engine to speed up data retrieval from tables. Basically, an index is a pointer that points to the tuples of a table. Why are indexes used? Indexes help in faster data retrieval from databases. Basically, they speed up select queries and the WHERE clause. But at the same time, they degrade the performance of INSERT and UPDATE queries. Hence, we should be very careful while using indexes.
Although indexes are intended to enhance a database's performance, there are times when they should be avoided. The following guidelines indicate when the use of an index should be reconsidered:
Indexes should not be used on small tables.
For example, if you have a small reference table with only a few rows, it's not necessary to add an index. In fact, adding an index may even slow down the queries since the database engine may spend more time searching through the index than scanning the table itself.
Indexes should not be used on columns that return a high percentage of data rows when used as a filter condition in a query's WHERE clause.
For instance, if you have a table with a "Gender" column that only has two values - "Male" and "Female" - and you want to filter the table based on gender, then adding an index on the "Gender" column may not be helpful since it will return almost half of the table rows, defeating the purpose of using an index.
Tables that have frequent, large batch update jobs run can be indexed. However, the batch job's performance is slowed considerably by the index.
For example, if you have a table with a lot of data that is frequently updated, adding an index can slow down the update performance since the database engine has to update both the table and the index. However, if you have a large batch update job that runs overnight, then adding an index can speed up the select queries that run during the day. After the batch job has completed, you can drop the index and then re-create it to avoid the overhead of maintaining the index during the update process.
Indexes should not be used on columns that contain a high number of NULL values.
For example, if you have a column that is frequently null, adding an index may not help much since the index will contain many null values, taking up space and adding overhead to the database engine.
Columns that are frequently manipulated should not be indexed.
For example, if you have a column that is frequently updated or deleted, adding an index can slow down the performance since the database engine has to update both the table and the index.
In summary, indexes are a powerful tool to improve database performance, but they should be used judiciously.
Always consider the tradeoffs before adding an index and carefully evaluate whether the benefits outweigh the costs. By following these guidelines, you can use indexes in a graceful way that enhances your database's performance without introducing unnecessary overhead.
Comments
Post a Comment