SQLite

SQLite Show Indexes

“An index is a type of data structure that integrates and optimizes the values of a column (or columns) for searching. Indexes (or indices) are unique to database lookups in that they allow certain rows in a table to be located without scanning the entire table. As a result, indexes can significantly improve the performance of some queries. Indexes are always associated with a single table; however, they can comprise many columns from the table.Normally, indexes permit duplicate values; however, the UNIQUE keyword prevents duplicate values from being inserted or updated into a table with a non-unique value. Because NULL isn’t considered a value, a UNIQUE index won’t stop one or more NULLs from appearing. You must specify NOT NULL in the initial table definition to prevent NULLs.

When you drop an index, it is removed from the database, but the table it is associated with is not affected.

With an example, we will learn what indexes are in SQLite, how to set indexes on single or many columns in SQLite, and how to remove or drop SQLite indexes; then, we will discuss how to show them in SQLite.”

Syntax of SQLite Indexes

CREATE [UNIQUE] INDEX [IF NOT EXISTS] index_name ON table_name(columns..n);

The create index statement is used in the above syntax to build a new index; the provided index name refers to a specific index name that we need to generate. The keywords ON and INDEX are keywords, and the specified table refers to an existing table with a column name.

If we have more than one field and that data needs to be defined uniquely so that we may use the unique keyword, we can use the unique keyword; a unique clause is an optional component of this syntax.

Key Points of SQLite Index

  • The usage of an index speeds up SELECT searches and WHERE clauses; however, it decelerates data input in UPDATE and INSERT operations. There is no influence on the data when indexes are created or eliminated.
  • An index is created with the CREATE INDEX statement. It allows you to name the index, provide the table and which index of column or columns, and define whether the index should be in ascending or descending order.
  • Indexes are also unique, in the same way that the UNIQUE constraint forbids duplicate records in the column or collection of columns on which the index is created.

How do Indexes Work in SQLite

A specific table must be connected with each index. An index may include one or even more fields, but they must all be in the same table. Several indexes can be found in a table.

SQLite provides a B-tree structure to store index data whenever you add an index. The indexes in SQLite are organized using the B-tree method. The B-tree is a balanced tree rather than a binary tree, as the B stands for balanced.

The index comprises values from the columns which we defined in the index, as well as the row_id value for each of those columns. SQLite can now rapidly locate the row using the entries of the indexed fields.

Imagine a database index similar to a book index. We may quickly locate several pages based on keywords by searching through the index.

When Should Indexes Not Be Used?

Although indexes are meant to improve database efficiency, there are situations when they should be disregarded. When should an index be used? The following factors may assist you in making your decision.

It is not recommended to utilize indexes in:

  • Tables with a small footprint
  • Tables that are updated or inserted in huge batches frequently.
  • NULL-heavy columns.
  • Frequently modified columns.

Example 1

First, we must build the table to see its indexes. We have shown a table below, which we have created with the CREATE command in SQLite and also defined fields of this table.

CREATE TABLE story_books(

book_id INT PRIMARY KEY ,

book_name CHAR(10) ,

book_price FLAOT

);

Now, we have created an index of a single column. A One-column index is built around a single database column. We can pass multiple columns at a time to create indexes of a particular table. We have given a single field name book_name from the table story_books in the following query.

# CREATE INDEX idx_story_books_book ON story_books(book_name);

By using the .indexes method, the index created in the table is viewable as follows:

Unique indexes are utilized for data integrity as well as performance. A unique index prevents duplicate values from being entered into the table. Using the following query, we have created a UNIQUE index on the story_book table’s book_id column.

CREATE UNIQUE INDEX idx_book_id

ON story_books (book_id);

The output shows the index has been successfully created.

We have created a selective index on a story_book table by including only a portion of the records in the index. As we used a WHERE clause, only those records where the book_price is not NULL get a partial index. Records with a NULL value for book_name will not be part of the index.

CREATE INDEX idx_book_price

ON story_books (book_price)

WHERE book_price IS NOT NULL;

When executing the .indexes command, it shows the indexes in the table. As you can see, that book_price index is included.

Example 2

SQLite’s DROP command is used to remove an index. Dropping an index should be done with caution because functionality may be affected. Here, with the DROP command, we have an IF EXIST clause. If the chosen index does not exist, no exception will be generated. It is not mandatory to use the IF EXISTS clause; we can skip this and simply drop the table. We have dropped the index book_name from the table story_book by the following query.

DROP INDEX IF EXISTS idx_story_books_book_name;

The index with the column book_name is dropped as shown in the shell.

Example 3

We have used the sqlite_master table instead of using the .indexes command. Although this table contains more than simply indexes, you may use the WHERE clause to limit it to indexes only.

SELECT

Name,tbl_name

...> FROM sqlite_master

...> WHERE type = 'index';

We have got the table to which each index corresponds. The tbl_name column keeps track of this as follows.

Conclusion

In this section, we have gone over the basics of establishing an index in SQLite and discuss how to show that indexes in SQLite with some examples. These examples are easy to use and also the ways which show the indexes in the SQLite. When you read the entire article, you might be extremely clear.

About the author

Omar Farooq

Hello Readers, I am Omar and I have been writing technical articles from last decade. You can check out my writing pieces.