SQLite Index (Index)
An index (Index) is a special lookup table that a database search engine uses to speed up data retrieval. Simply put, an index is a pointer to data in a table. An index in a database is very similar to an index in a book.
Take the table of contents (index) page of a Chinese dictionary as an example: we can quickly find the needed character through an index (directory) sorted by pinyin, stroke count, radical, and so on.
Indexes help speed up SELECT queries and WHERE clauses, but they slow down data entry when using UPDATE and INSERT statements. Indexes can be created or deleted without affecting the data.
Use the CREATE INDEX statement to create an index. It allows you to name the index, specify the table and one or more columns to be indexed, and indicate whether the index is sorted in ascending or descending order.
An index can also be unique, similar to a UNIQUE constraint, preventing duplicate entries on a column or a combination of columns.
The CREATE INDEX Command
CREATE INDEX The basic syntax is as follows:
CREATE INDEX index_name ON table_name;
Single-Column Indexes
A single-column index is an index created on only one column of a table. The basic syntax is as follows:
CREATE INDEX index_name
ON table_name (column_name);
Unique Indexes
A unique index is used not only for performance but also for data integrity. A unique index does not allow any duplicate values to be inserted into the table. The basic syntax is as follows:
CREATE UNIQUE INDEX index_name
on table_name (column_name);
Composite Indexes
A composite index is an index created on two or more columns of a table. The basic syntax is as follows:
CREATE INDEX index_name
on table_name (column1, column2);
Whether to create a single-column index or a composite index depends on the columns you use very frequently in the WHERE clause as query filter conditions.
If only one column is used, choose to use a single-column index. If two or more columns are frequently used in the WHERE clause for filtering, choose to use a composite index.
Implicit Indexes
Implicit indexes are indexes automatically created by the database server when an object is created. Indexes are automatically created for primary key constraints and unique constraints.
Example
Here is an example where we will create an index on the salary column of the COMPANY table:
sqlite> CREATE INDEX salary_index ON COMPANY (salary);
Now, let's use the .indices or .indexes command to list all available indexes on the COMPANY table, as follows:
sqlite> .indices COMPANY
This will produce the following result, where sqlite_autoindex_COMPANY_1 is an implicit index created when the table was created.
salary_index
sqlite_autoindex_COMPANY_1
You can list all indexes in the database, as follows:
sqlite> SELECT * FROM sqlite_master WHERE type = 'index';
DROP INDEX Command
An index can be deleted using the SQLite DROP command. Special care should be taken when deleting an index, because performance may decrease or increase.
The basic syntax is as follows:
DROP INDEX index_name;
You can use the following statement to drop the previously created index:
sqlite> DROP INDEX salary_index;
When should indexes be avoided?
Although the purpose of an index is to improve database performance, there are several situations where you should avoid using an index. When using an index, the following guidelines should be reconsidered:
-
Indexes should not be used on small tables.
-
Indexes should not be used on tables that undergo frequent, large-volume updates or inserts.
-
Indexes should not be used on columns that contain a large number of NULL values.
-
Indexes should not be used on columns that are frequently operated on.