SQLite index
Index (Index) is a special lookup table used by database search engines to speed up data retrieval. Simply put, an index is a pointer to the data in the table. An index in a database is very similar to an index behind a book.
For example, if you wanted to cite all pages in a book that discusses a certain topic, you would first point to the index, which lists all topics in alphabetical order, and then point to one or more specific page numbers .
Indexes help speed up SELECT queries and WHERE clauses, but they slow down data input 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, which allows you to name the index, specify the table and one or more columns to be indexed, and indicate whether the index is in ascending or descending order.
Indices can also be unique, similar to UNIQUE constraints, preventing duplicate entries on a column or combination of columns.
CREATE INDEX command
The basic syntax of CREATE INDEX is as follows:
Single column index
A single column index is an index created based on only one column of the table. The basic syntax is as follows:
ON table_name (column_name);
Unique index
Using a unique index is not only for performance, 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:
.indices command to list all available indexes on the COMPANY table as follows:
This will produce the following result, where sqlite_autoindex_COMPANY_1 is the implicit index created when the table is created.
sqlite_autoindex_COMPANY_1
You can list all indexes in the database scope as follows:
DROP INDEX command
An index can be deleted using SQLite's DROP command. Special care should be taken when dropping indexes, as performance may either decrease or increase.
The basic syntax is as follows:
You can use the following statement to delete a previously created index:
Under what circumstances should you avoid using indexes?
Although the purpose of indexes is to improve database performance, there are several situations where you need to avoid using indexes. When using indexes, you should reconsider the following guidelines:
Indexes should not be used on smaller tables.
Indices should not be used on tables with frequent large batch update or insert operations.
Indexes should not be used on columns that contain a large number of NULL values.
Indexes should not be used on frequently operated columns.