SQLite Tutorial

  • Home
  • Start Here
  • Views
  • Indexes
  • Triggers
  • Functions
    • Aggregate Functions
    • Date Functions
    • String Functions
    • Window Functions
  • API
    • SQLite Python
    • SQLite Node.js
    • SQLite Java
    • SQLite PHP
  • Try It
Home / SQLite Index

SQLite Index

Summary: in this tutorial, you will learn how to use SQLite indexes to query data faster, speed up sort operation, and enforce unique constraints.

What is an index?

In relational databases, a table is a list of rows. In the same time, each row has the same column structure that consists of cells. Each row also has a consecutive rowid sequence number used to identify the row. Therefore, you can consider a table as a list of pairs: (rowid, row).

Unlike a table, an index has an opposite relationship: (row, rowid). An index is an additional data structure that helps improve the performance of a query.

SQLite Index

SQLite uses B-tree for organizing indexes. Note that B stands for balanced, B-tree is a balanced tree, not a binary tree.

The B-tree keeps the amount of data at both sides of the tree balanced so that the number of levels that must be traversed to locate a row is always in the same approximate number. In addition, querying using equality (=) and ranges (>, >=, <,<=) on the B-tree indexes are very efficient.

How does an index work

Each index must be associated with a specific table. An index consists of one or more columns, but all columns of an index must be in the same table. A table may have multiple indexes.

Whenever you create an index, SQLite creates a B-tree structure to hold the index data.

The index contains data from the columns that you specify in the index and the corresponding rowid value. This helps SQLite quickly locate the row based on the values of the indexed columns.

Imagine an index in the database like an index of a book. By looking at the index, you can quickly identify page numbers based on the keywords.

SQLite CREATE INDEX statement

To create an index, you use the CREATE INDEX statement with the following syntax:

CREATE [UNIQUE] INDEX index_name ON table_name(column_list);

To create an index, you specify three important information:

  • The name of the index after the CREATE INDEX keywords.
  • The name of the table to the index belongs.
  • A list of columns of the index.

In case you want to make sure that values in one or more columns are unique like email and phone, you use the UNIQUE option in the CREATE INDEX statement. The CREATE UNIQUE INDEX creates a new unique index.

SQLite UNIQUE index example

Let’s create a new table named contacts for demonstration.

CREATE TABLE contacts ( first_name text NOT NULL, last_name text NOT NULL, email text NOT NULL );

Try It

Suppose, you want to enforce that the email is unique, you create a unique index as follows:

CREATE UNIQUE INDEX idx_contacts_email ON contacts (email);

Try It

To test this.

First,  insert a row into the contacts table.

INSERT INTO contacts (first_name, last_name, email) VALUES('John','Doe','john.doe@sqlitetutorial.net');

Try It

Second, insert another row with a duplicate email.

INSERT INTO contacts (first_name, last_name, email) VALUES('Johny','Doe','john.doe@sqlitetutorial.net');

Try It

SQLite issued an error message indicating that the unique index has been violated. Because when you inserted the second row, SQLite checked and made sure that the email is unique across of rows in email of the contacts table.

Let’s insert two more rows into the contacts table.

INSERT INTO contacts (first_name, last_name, email) VALUES('David','Brown','david.brown@sqlitetutorial.net'), ('Lisa','Smith','lisa.smith@sqlitetutorial.net');

Try It

If you query data from the contacts table based on a specific email, SQLite will use the index to locate the data. See the following statement:

SELECT first_name, last_name, email FROM contacts WHERE email = 'lisa.smith@sqlitetutorial.net';

Try It

SQLite index example

To check if SQLite uses the index or not, you use the EXPLAIN QUERY PLAN statement as follows:

EXPLAIN QUERY PLAN SELECT first_name, last_name, email FROM contacts WHERE email = 'lisa.smith@sqlitetutorial.net';

Try It

SQLite Index Explain example

SQLite multicolumn index example

If you create an index that consists of one column, SQLite uses that column as the sort key. In case you create an index that has multiple columns, SQLite uses the additional columns as the second, third, … as the sort keys.

SQLite sorts the data on the multicolumn index by the first column specified in the CREATE INDEX statement. Then, it sorts the duplicate values by the second column, and so on.

Therefore, the column order is very important when you create a multicolumn index.

To utilize a multicolumn index, the query must contain the condition that has the same column order as defined in the index.

The following statement creates a multicolumn index on the first_name and last_name columns of the contacts table:

CREATE INDEX idx_contacts_name ON contacts (first_name, last_name);

Try It

If you query the contacts table with one of the following conditions in the WHERE clause, SQLite will utilize the multicolumn index to search for data.

1) filter data by the first_name column.

WHERE first_name = 'John';

2)filter data by both first_name and last_name columns:

WHERE first_name = 'John' AND last_name = 'Doe';

However, SQLite will not use the multicolumn index if you use one of the following conditions.

1)filter by the last_name column only.

WHERE last_name = 'Doe';

2) filter by first_name OR last_name columns.

last_name = 'Doe' OR first_name = 'John';

SQLite Show Indexes

To find all indexes associated with a table, you use the following command:

PRAGMA index_list('table_name');

For example, this statement shows all the indexes of the contacts table:

PRAGMA index_list('playlist_track');

Here is the output:

SQLite index - show indexes

To get the information about the columns in an index, you use the following command:

PRAGMA index_info('idx_contacts_name');

This example returns the column list of the index idx_contacts_name:

Another way to get all indexes from a database is to query from the sqlite_master table:

SELECT type, name, tbl_name, sql FROM sqlite_master WHERE type= 'index';

SQLite DROP INDEX statement

To remove an index from a database, you use the DROP INDEX statement as follows:

DROP INDEX [IF EXISTS] index_name;

In this syntax, you specify the name of the index that you want to drop after the DROP INDEX keywords. The IF EXISTS option removes an index only if it exists.

For example, you use the following statement to remove the idx_contacts_name index:

DROP INDEX idx_contacts_name;

Try It

The idx_contacts_name index is removed completely from the database.

In this tutorial, you have learned about SQLite index and how to utilize indexes for improving the performance of query or enforcing unique constraints.

  • Was this tutorial helpful ?
  • YesNo
Previous SQLite Create View
Next SQLite Trigger

Getting Started

  • What Is SQLite
  • Download & Install SQLite
  • SQLite Sample Database
  • SQLite Commands

SQLite Tutorial

  • SQLite Select
  • SQLite Order By
  • SQLite Select Distinct
  • SQLite Where
  • SQLite Limit
  • SQLite BETWEEN
  • SQLite IN
  • SQLite Like
  • SQLite IS NULL
  • SQLite GLOB
  • SQLite Join
  • SQLite Inner Join
  • SQLite Left Join
  • SQLite Cross Join
  • SQLite Self-Join
  • SQLite Full Outer Join
  • SQLite Group By
  • SQLite Having
  • SQLite Union
  • SQLite Except
  • SQLite Intersect
  • SQLite Subquery
  • SQLite EXISTS
  • SQLite Case
  • SQLite Insert
  • SQLite Update
  • SQLite Delete
  • SQLite Replace
  • SQLite Transaction

SQLite Data Definition

  • SQLite Data Types
  • SQLite Date & Time
  • SQLite Create Table
  • SQLite Primary Key
  • SQLite Foreign Key
  • SQLite NOT NULL Constraint
  • SQLite UNIQUE Constraint
  • SQLite CHECK constraints
  • SQLite AUTOINCREMENT
  • SQLite Alter Table
  • SQLite Rename Column
  • SQLite Drop Table
  • SQLite Create View
  • SQLite Drop View
  • SQLite Index
  • SQLite Expression-based Index
  • SQLite Trigger
  • SQLite VACUUM
  • SQLite Transaction
  • SQLite Full-text Search

SQLite Tools

  • SQLite Commands
  • SQLite Show Tables
  • SQLite Describe Table
  • SQLite Dump
  • SQLite Import CSV
  • SQLite Export CSV

SQLite Functions

  • SQLite AVG
  • SQLite COUNT
  • SQLite MAX
  • SQLite MIN
  • SQLite SUM

SQLite Interfaces

  • SQLite PHP
  • SQLite Node.js
  • SQLite Java
  • SQLite Python

About SQLite Tutorial

SQLite Tutorial website helps you master SQLite quickly and easily. It explains the complex concepts in simple and easy-to-understand ways so that you can both understand SQLite fast and know how to apply it in your software development work more effectively.

Looking for a tutorial…

If you did not find the tutorial that you are looking for, you can use the following search box. In case the tutorial is not available, you can request for it using the request for a SQLite tutorial form.

Recent Tutorials

  • SQLite IIF
  • SQLite Generated Columns
  • SQLite Getting Started
  • SQLite Programming Interfaces
  • SQLite Concat
  • SQLite INSTEAD OF Triggers
  • SQLite Join
  • SQLite IS NULL

Site Links

  • Home
  • About
  • Contact
  • Resources
  • Privacy Policy

Copyright © 2021 SQLite Tutorial. All Rights Reserved.