- SQLite - DISTINCT Keyword
- SQLite - HAVING Clause
- SQLite - GROUP By Clause
- SQLite - ORDER By Clause
- SQLite - LIMIT Clause
- SQLite - GLOB Clause
- SQLite - LIKE Clause
- SQLite - DELETE Query
- SQLite - UPDATE Query
- SQLite - AND & OR Clauses
- SQLite - WHERE Clause
- SQLite - Expressions
- SQLite - Operators
- SQLite - SELECT Query
- SQLite - INSERT Query
- SQLite - DROP Table
- SQLite - CREATE Table
- SQLite - DETACH Database
- SQLite - ATTACH Database
- SQLite - CREATE Database
- SQLite - Data Type
- SQLite - Syntax
- SQLite - Commands
- SQLite - Installation
- SQLite - Overview
- SQLite - Home
Advanced SQLite
- SQLite - Useful Functions
- SQLite - Date & Time
- SQLite - VACUUM
- SQLite - EXPLAIN
- SQLite - Injection
- SQLite - AUTOINCREMENT
- SQLite - Subqueries
- SQLite - Transactions
- SQLite - Views
- SQLite - TRUNCATE Command
- SQLite - ALTER Command
- SQLite - INDEXED By Clause
- SQLite - Indexes
- SQLite - Triggers
- SQLite - ALIAS Syntax
- SQLite - NULL Values
- SQLite - UNIONS Clause
- SQLite - JOINS
- SQLite - Constraints
- SQLite - PRAGMA
SQLite Interfaces
SQLite Useful Resources
Selected Reading
- Who is Who
- Computer Glossary
- HR Interview Questions
- Effective Resume Writing
- Questions and Answers
- UPSC IAS Exams Notes
SQLite - Indexes
Indexes are special lookup tables that the database search engine can use 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 the back of a book.
For example, if you want to reference all pages in a book that discuss a certain topic, you first refer to the index, which psts all topics alphabetically and are then referred to one or more specific page numbers.
An index helps speed up SELECT queries and WHERE clauses, but it slows down data input, with UPDATE and INSERT statements. Indexes can be created or dropped with no effect on the data.
Creating an index involves the CREATE INDEX statement, which allows you to name the index, to specify the table and which column or columns to index, and to indicate whether the index is in an ascending or descending order.
Indexes can also be unique, similar to the UNIQUE constraint, in that the index prevents duppcate entries in the column or combination of columns on which there s an index.
The CREATE INDEX Command
Following is the basic syntax of CREATE INDEX.
CREATE INDEX index_name ON table_name;
Single-Column Indexes
A single-column index is one that is created based on only one table column. The basic syntax is as follows −
CREATE INDEX index_name ON table_name (column_name);
Unique Indexes
Unique indexes are used not only for performance, but also for data integrity. A unique index does not allow any duppcate 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 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, take into consideration the column(s) that you may use very frequently in a query s WHERE clause as filter conditions.
Should there be only one column used, a single-column index should be the choice. Should there be two or more columns that are frequently used in the WHERE clause as filters, the composite index would be the best choice.
Imppcit Indexes
Imppcit indexes are indexes that are automatically created by the database server when an object is created. Indexes are automatically created for primary key constraints and unique constraints.
Example
Following is an example where we will create an index in
table for salary column −sqpte> CREATE INDEX salary_index ON COMPANY (salary);
Now, let s pst down all the indices available in COMPANY table using .indices command as follows −
sqpte> .indices COMPANY
This will produce the following result, where sqpte_autoindex_COMPANY_1 is an imppcit index which got created when the table itself was created.
salary_index sqpte_autoindex_COMPANY_1
You can pst down all the indexes database wide as follows −
sqpte> SELECT * FROM sqpte_master WHERE type = index ;
The DROP INDEX Command
An index can be dropped using SQLite DROP command. Care should be taken when dropping an index because performance may be slowed or improved.
Following is the basic syntax is as follows −
DROP INDEX index_name;
You can use the following statement to delete previously created index.
sqpte> DROP INDEX salary_index;
When Should Indexes Be Avoided?
Although indexes are intended to enhance the performance of a database, there are times when they should be avoided. The following guidepnes indicate when the use of an index should be reconsidered.
Indexes should not be used in −
Small tables.
Tables that have frequent, large batch update or insert operations.
Columns that contain a high number of NULL values.
Columns that are frequently manipulated.