Physical Design   «Prev  Next»

Lesson 3Indexing
ObjectiveExplain the purpose of creating Indexes

Mastering SQL Index Creation: Your Comprehensive Guide

If you have ever found yourself wondering, "How do I create an index using SQL?" then you have come to the right place. In this extensive guide, we will explore the ins and outs of SQL indexing, providing you with the knowledge and skills to optimize your database performance and query speed, from the basics of indexing to advanced techniques.

How do I create an index using SQL?

  1. Understanding Indexes: Before we dive into creating an index using SQL, let's take a moment to understand what an index is and why it's crucial for database performance. An index is a database object that helps speed up data retrieval. Think of it as a road map that points to the location of specific records in a table. By using an index, you can avoid scanning the entire table, thus improving query performance.
  2. Index Types: There are two main types of indexes in SQL:
    • Clustered Index: A clustered index determines the physical order of data storage in a table. It is the default index type and is created automatically when a primary key constraint is defined for a table. There can only be one clustered index per table.
    • Non-Clustered Index: A non-clustered index doesn't affect the physical order of data storage but creates a separate structure to store the index data. You can have multiple non-clustered indexes for a table.
To create an index using SQL, you'll need to use the CREATE INDEX statement. The basic syntax for creating an index is as follows:
CREATE [UNIQUE] INDEX index_name
ON table_name (column1, column2, ...);

For example, to create an index on the 'email' column in a 'users' table, you would use the following command:
CREATE INDEX email_index
ON users (email);

Index Creation Best Practices

Creating an index using SQL is just the beginning. To make the most out of your indexes, it's essential to follow best practices that help improve query performance and database efficiency.
  1. Choose the Right Columns: Not every column in a table needs to be indexed. In fact, indexing too many columns can slow down your database performance. Choose columns that are frequently used in WHERE clauses, JOIN conditions, and ORDER BY statements.
  2. Keep Indexes Narrow: The narrower an index, the less storage space it requires, and the faster it is to maintain and search. Avoid creating indexes on large columns or using multiple columns in an index unless necessary.
  3. Use Covering Indexes: A covering index includes all the columns required by a query. By using a covering index, you can avoid additional table lookups, thus speeding up query execution.


Advanced Indexing Techniques

Once you've mastered the basics of creating an index using SQL, it's time to explore some advanced indexing techniques that can further enhance your database performance.
  1. Using Indexed Views: An indexed view is a view with a unique clustered index. By creating an indexed view, you can pre-calculate and store the results of complex queries, thus improving query performance.
  2. Using Filtered Indexes: A filtered index is an index that includes only a subset of rows in a table. By using filtered indexes, you can reduce index size and maintenance costs for specific queries.
  3. Using Partitioning: Partitioning is a technique that divides a table into smaller, more manageable pieces.

Whenever you add rows to a table the RDBMS writes them to the end of the table, regardless of the values in the table. For example, a list of orders Stories on CD placed with its distributors would go in chronological order, with no concern for the titles or the values for CDNo.
Order table consisting of 1) OrderNo 2) OrderDate 3) CDNo 4) OrderCost 5) Received
Order table consisting of 1) OrderNo 2) OrderDate 3) CDNo 4) OrderCost 5) Received

Ordered Records

Because the records are not ordered according to CDNo, the RDBMS would need to search every record of this table to find all orders for Northern Tales (CDNo 101). If users frequently needed to search the Orders table to find every order for a particular CD, the amount of time the RDBMS took to search the entire table (which could be quite large) would impact worker productivity.
Rather than force the RDBMS to examine every record, it is possible to create an index of values in fields frequently used in searches and joins. An index is an ordered list of values in a field, exactly like the index in the back of a book.

An index consisting of 1) CDNo 2) OrderNo
An index consisting of 1) CDNo 2) OrderNo

Rather than search the table, the RDBMS can search the index and, when it finds the value it wants, use the already recorded positions to quickly pull the data from the table.

Deciding Which Indexes to Create

You have no choice as to whether the DBMS creates indexes for your primary keys; you get them whether you want them or not. In addition, you can create indexes on any column or combination of columns you want. However, before you jump headfirst into creating indexes on every column in every table, you must consider some trade-offs:
  1. Indexes take up space in the database. Given that disk space is relatively inexpensive today, this is usually not a major drawback.
  2. When you insert, modify, or delete data in indexed columns, the DBMS must update the index as well as the base table. This may slow down data modification operations, especially if the tables have a lot of rows.
  3. Indexes definitely speed up access to data.

SEMrush Software