SQL Foundations  «Prev  Next»

Lesson 5What is an index?
ObjectiveUse Database Table Index to speed up your Queries.

Use Table Index to speed up Queries

Using indexes in your SQL database can significantly increase the performance of your queries by enabling faster data retrieval.
Here's a guide on how you can utilize database table indexes to speed up your SQL queries:
  1. Understand the Basics of Indexing: A database index works much like an index in a book; it allows the database to find and retrieve specific rows much faster than it could without an index. But unlike a book's index, the database maintains its indexes automatically. Once an index is created, the database system manages it and uses it whenever it can.
  2. Create Indexes Based on Your Queries: Indexes are most effective when they're designed with your queries in mind. Analyze the queries that are running slow and identify the columns that are used in WHERE clauses, JOIN conditions, ORDER BY, GROUP BY, or SELECT statements. Those are your prime candidates for indexing.
    CREATE INDEX idx_product_description 
    ON Products (Product_Description);

    This statement creates an index named 'idx_product_description' on the 'Product_Description' column in the 'Products' table.
  3. Use Composite Indexes for Complex Queries: If your queries often filter or sort on multiple columns together, consider creating a composite index, which includes multiple columns. The order of columns in the index definition can matter for these multi-column indexes, generally aligning the column order in the index with your query patterns can lead to better performance.
    CREATE INDEX idx_product 
    ON Products (Product_ID, Product_Description);
  4. Limit the Number of Indexes: While indexes can speed up query performance, they come with a cost. Every time you modify data (insert, update, or delete), your indexes need to be updated as well. Having too many indexes can slow down these operations and take up additional storage space. Balance is key.
  5. Monitor and Optimize Your Indexes: Regularly monitor the performance of your indexes and adjust as necessary. You may find that some indexes are not used often, and they could be dropped. Database management systems often include tools for index monitoring and reporting.
  6. Keep Your Database Statistics Up to Date: Database systems use statistics about the distribution of data in your table to make decisions about when and how to use indexes. Keeping these statistics up to date can help the query planner make better decisions and potentially improve performance.

Remember, while indexes can improve database performance, they are not a magic bullet. Always measure the performance of your queries before and after adding indexes to make sure they're having the desired effect.
As you start working with SQL in your database, you will find that you will pay more and more attention to making your query run faster and return results more quickly. Although you will be able to accomplish some of this simply by working with the query itself (you can remove unneeded columns, decrease the number of rows that will be returned, and so on), it is also likely that an index will help.

Table Index

An index is a document that is manually created by the database. It helps the database find information more quickly by indexing locations.

To help understand what an index is about, you might want to consider your favorite file cabinet. If the folders are in the cabinet in random order, finding a folder in short order will be next to impossible. It is more likely that you sort the folders, perhaps adding dividers to help you quickly go to the section of folders that contain the specific folder you need.
With a SQL database, the concept is the same. You will need to decide how you are going to be asking for information in the database, and then use this information to determine if it makes sense to create an index that directly supports that approach.
How to create an index will be discussed next.

Indexes and SQL Tables

Question: I have often heard the term indexes discussed in relation to creating SQL tables.
How do you create indexes?
Answer: The SQL 2011 standard does not support the creation and maintenance of indexes, nor does it provide a definition or mention them in any other way. An index is a set of search values and pointers (in a subsidiary table) that correspond to rows in a table. Indexes speed up queries and improve performance, making data access much more efficient, much like using the index of a book helps you find things more quickly than sequentially searching the pages. As a result, nearly every RDBMS supports some form of indexing, and indeed they are an important part of that product. However, the method used to implement indexing varies greatly, so each product provides its own system to set up and maintain their indexes.
For example, the CREATE INDEX statement is available for most database products such as 1) MySQL, 2) Oracle, 3) DB2, 4)MS SQL Server, 5) MS Access. However, the syntax for the statement can vary considerably. As always, be sure to review the product documentation.

Ad SQL Queries