{"id":566,"date":"2015-12-14T10:42:33","date_gmt":"2015-12-14T03:42:33","guid":{"rendered":"http:\/\/www.sqlitetutorial.net\/?page_id=566"},"modified":"2024-04-27T20:51:28","modified_gmt":"2024-04-27T13:51:28","slug":"sqlite-index","status":"publish","type":"page","link":"https:\/\/www.sqlitetutorial.net\/sqlite-index\/","title":{"rendered":"SQLite Index"},"content":{"rendered":"\n<p><strong>Summary<\/strong>: in this tutorial, you will learn how to use SQLite indexes to query data faster, speed up sort operations, and enforce unique constraints.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">What is an index?<\/h2>\n\n\n\n<p>In relational databases, a table is a list of rows. At the same time, each row has the same column structure that consists of cells. Each row also has a consecutive <a href=\"https:\/\/www.sqlitetutorial.net\/sqlite-autoincrement\/\">rowid<\/a> sequence number used to identify the row. Therefore, you can consider a table as a list of pairs: (rowid, row).<\/p>\n\n\n\n<p>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.<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img loading=\"lazy\" decoding=\"async\" width=\"663\" height=\"264\" src=\"https:\/\/www.sqlitetutorial.net\/wp-content\/uploads\/2015\/12\/SQLite-Index.png\" alt=\"SQLite Index\" class=\"wp-image-567\" srcset=\"https:\/\/www.sqlitetutorial.net\/wp-content\/uploads\/2015\/12\/SQLite-Index.png 663w, https:\/\/www.sqlitetutorial.net\/wp-content\/uploads\/2015\/12\/SQLite-Index-300x119.png 300w\" sizes=\"auto, (max-width: 663px) 100vw, 663px\" \/><\/figure>\n\n\n\n<p>SQLite uses B-tree for organizing indexes. Note that <strong>B<\/strong> stands for balanced, B-tree is a balanced tree, not a binary tree.<\/p>\n\n\n\n<p>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 (&gt;, &gt;=, &lt;,&lt;=) on the B-tree indexes is very efficient.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">How does an index work<\/h2>\n\n\n\n<p>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.<\/p>\n\n\n\n<p>Whenever you create an index, SQLite creates a B-tree structure to hold the index data.<\/p>\n\n\n\n<p>The index contains data from the columns you specify in the index and the corresponding <code>rowid<\/code> value. This helps SQLite quickly locate the row based on the values of the indexed columns.<\/p>\n\n\n\n<p>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.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">SQLite CREATE INDEX statement<\/h2>\n\n\n\n<p>To create an index, you use the <code>CREATE INDEX<\/code> statement with the following syntax:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-1\" data-shcb-language-name=\"SQL (Structured Query Language)\" data-shcb-language-slug=\"sql\"><span><code class=\"hljs language-sql\"><span class=\"hljs-keyword\">CREATE<\/span> &#91;<span class=\"hljs-keyword\">UNIQUE<\/span>] <span class=\"hljs-keyword\">INDEX<\/span> index_name \n<span class=\"hljs-keyword\">ON<\/span> table_name(column_list);<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-1\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">SQL (Structured Query Language)<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">sql<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>To create an index, you specify the following information:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>The index name after the <code>CREATE INDEX<\/code> keywords.<\/li>\n\n\n\n<li>The name of the table to which the index belongs after the <code>ON<\/code> keyword.<\/li>\n\n\n\n<li>A list of columns of the index inside parentheses () after the table name.<\/li>\n<\/ul>\n\n\n\n<p>If you want to ensure the values in one or more columns are unique like email and phone, you can use the <code>UNIQUE<\/code> option in the <code>CREATE INDEX<\/code> statement to create a new <a href=\"https:\/\/www.sqlitetutorial.net\/sqlite-index\/sqlite-unique-index\/\">unique index<\/a>.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">SQLite UNIQUE index example<\/h3>\n\n\n\n<p>Let&#8217;s <a href=\"https:\/\/www.sqlitetutorial.net\/sqlite-create-table\/\">create a new table<\/a> named <code>contacts<\/code> for demonstration.<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-2\" data-shcb-language-name=\"SQL (Structured Query Language)\" data-shcb-language-slug=\"sql\"><span><code class=\"hljs language-sql\"><span class=\"hljs-keyword\">CREATE<\/span> <span class=\"hljs-keyword\">TABLE<\/span> contacts (\n\tfirst_name <span class=\"hljs-built_in\">text<\/span> <span class=\"hljs-keyword\">NOT<\/span> <span class=\"hljs-literal\">NULL<\/span>,\n\tlast_name <span class=\"hljs-built_in\">text<\/span> <span class=\"hljs-keyword\">NOT<\/span> <span class=\"hljs-literal\">NULL<\/span>,\n\temail <span class=\"hljs-built_in\">text<\/span> <span class=\"hljs-keyword\">NOT<\/span> <span class=\"hljs-literal\">NULL<\/span>\n);<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-2\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">SQL (Structured Query Language)<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">sql<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p><a class=\"sql\" href=\"https:\/\/www.sqlitetutorial.net\/tryit\/query\/sqlite-index\/#1\" target=\"_blank\" rel=\"noopener noreferrer\">Try It<\/a><\/p>\n\n\n\n<p>Suppose, you want to enforce that the email is unique, you create a unique index as follows:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-3\" data-shcb-language-name=\"SQL (Structured Query Language)\" data-shcb-language-slug=\"sql\"><span><code class=\"hljs language-sql\"><span class=\"hljs-keyword\">CREATE<\/span> <span class=\"hljs-keyword\">UNIQUE<\/span> <span class=\"hljs-keyword\">INDEX<\/span> idx_contacts_email \n<span class=\"hljs-keyword\">ON<\/span> contacts (email);<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-3\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">SQL (Structured Query Language)<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">sql<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p><a class=\"sql\" href=\"https:\/\/www.sqlitetutorial.net\/tryit\/query\/sqlite-index\/#2\" target=\"_blank\" rel=\"noopener noreferrer\">Try It<\/a><\/p>\n\n\n\n<p>To test this.<\/p>\n\n\n\n<p>First, <a href=\"https:\/\/www.sqlitetutorial.net\/sqlite-insert\/\">insert a row into<\/a> the <code>contacts<\/code> table.<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-4\" data-shcb-language-name=\"SQL (Structured Query Language)\" data-shcb-language-slug=\"sql\"><span><code class=\"hljs language-sql\"><span class=\"hljs-keyword\">INSERT<\/span> <span class=\"hljs-keyword\">INTO<\/span> contacts (first_name, last_name, email)\n<span class=\"hljs-keyword\">VALUES<\/span>(<span class=\"hljs-string\">'John'<\/span>,<span class=\"hljs-string\">'Doe'<\/span>,<span class=\"hljs-string\">'john.doe@sqlitetutorial.net'<\/span>);<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-4\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">SQL (Structured Query Language)<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">sql<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p><a class=\"sql\" href=\"https:\/\/www.sqlitetutorial.net\/tryit\/query\/sqlite-index\/#3\" target=\"_blank\" rel=\"noopener noreferrer\">Try It<\/a><\/p>\n\n\n\n<p>Second, insert another row with a duplicate email.<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-5\" data-shcb-language-name=\"SQL (Structured Query Language)\" data-shcb-language-slug=\"sql\"><span><code class=\"hljs language-sql\"><span class=\"hljs-keyword\">INSERT<\/span> <span class=\"hljs-keyword\">INTO<\/span> contacts (first_name, last_name, email)\n<span class=\"hljs-keyword\">VALUES<\/span>(<span class=\"hljs-string\">'Johny'<\/span>,<span class=\"hljs-string\">'Doe'<\/span>,<span class=\"hljs-string\">'john.doe@sqlitetutorial.net'<\/span>);<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-5\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">SQL (Structured Query Language)<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">sql<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p><a class=\"sql\" href=\"https:\/\/www.sqlitetutorial.net\/tryit\/query\/sqlite-index\/#4\" target=\"_blank\" rel=\"noopener noreferrer\">Try It<\/a><\/p>\n\n\n\n<p>SQLite issued an error message indicating that the unique index has been violated. <\/p>\n\n\n\n<p>When you inserted the second row, SQLite checked and made sure that the email was unique across rows in the <code>email<\/code> column of the <code>contacts<\/code> table.<\/p>\n\n\n\n<p>Let&#8217;s insert two more rows into the <code>contacts<\/code> table.<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-6\" data-shcb-language-name=\"SQL (Structured Query Language)\" data-shcb-language-slug=\"sql\"><span><code class=\"hljs language-sql\"><span class=\"hljs-keyword\">INSERT<\/span> <span class=\"hljs-keyword\">INTO<\/span> contacts (first_name, last_name, email)\n<span class=\"hljs-keyword\">VALUES<\/span>(<span class=\"hljs-string\">'David'<\/span>,<span class=\"hljs-string\">'Brown'<\/span>,<span class=\"hljs-string\">'david.brown@sqlitetutorial.net'<\/span>),\n      (<span class=\"hljs-string\">'Lisa'<\/span>,<span class=\"hljs-string\">'Smith'<\/span>,<span class=\"hljs-string\">'lisa.smith@sqlitetutorial.net'<\/span>);<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-6\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">SQL (Structured Query Language)<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">sql<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p><a class=\"sql\" href=\"https:\/\/www.sqlitetutorial.net\/tryit\/query\/sqlite-index\/#5\" target=\"_blank\" rel=\"noopener noreferrer\">Try It<\/a><\/p>\n\n\n\n<p>If you <a href=\"https:\/\/www.sqlitetutorial.net\/sqlite-select\/\">query data<\/a> from the <code>contacts<\/code> table based on a specific email, SQLite will use the index to locate the data. See the following statement:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-7\" data-shcb-language-name=\"SQL (Structured Query Language)\" data-shcb-language-slug=\"sql\"><span><code class=\"hljs language-sql\"><span class=\"hljs-keyword\">SELECT<\/span>\n\tfirst_name,\n\tlast_name,\n\temail\n<span class=\"hljs-keyword\">FROM<\/span>\n\tcontacts\n<span class=\"hljs-keyword\">WHERE<\/span>\n\temail = <span class=\"hljs-string\">'lisa.smith@sqlitetutorial.net'<\/span>;<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-7\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">SQL (Structured Query Language)<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">sql<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p><a class=\"sql\" href=\"https:\/\/www.sqlitetutorial.net\/tryit\/query\/sqlite-index\/#6\" target=\"_blank\" rel=\"noopener noreferrer\">Try It<\/a><\/p>\n\n\n\n<figure class=\"wp-block-image\"><img loading=\"lazy\" decoding=\"async\" width=\"348\" height=\"42\" src=\"https:\/\/www.sqlitetutorial.net\/wp-content\/uploads\/2015\/12\/SQLite-index-example.jpg\" alt=\"SQLite index example\" class=\"wp-image-587\" srcset=\"https:\/\/www.sqlitetutorial.net\/wp-content\/uploads\/2015\/12\/SQLite-index-example.jpg 348w, https:\/\/www.sqlitetutorial.net\/wp-content\/uploads\/2015\/12\/SQLite-index-example-300x36.jpg 300w\" sizes=\"auto, (max-width: 348px) 100vw, 348px\" \/><\/figure>\n\n\n\n<p>To check if SQLite uses the index or not, you use the <code>EXPLAIN QUERY PLAN<\/code> statement as follows:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-8\" data-shcb-language-name=\"SQL (Structured Query Language)\" data-shcb-language-slug=\"sql\"><span><code class=\"hljs language-sql\"><span class=\"hljs-keyword\">EXPLAIN<\/span> <span class=\"hljs-keyword\">QUERY<\/span> PLAN \n<span class=\"hljs-keyword\">SELECT<\/span>\n\tfirst_name,\n\tlast_name,\n\temail\n<span class=\"hljs-keyword\">FROM<\/span>\n\tcontacts\n<span class=\"hljs-keyword\">WHERE<\/span>\n\temail = <span class=\"hljs-string\">'lisa.smith@sqlitetutorial.net'<\/span>;<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-8\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">SQL (Structured Query Language)<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">sql<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p><a class=\"sql\" href=\"https:\/\/www.sqlitetutorial.net\/tryit\/query\/sqlite-index\/#7\" target=\"_blank\" rel=\"noopener noreferrer\">Try It<\/a><\/p>\n\n\n\n<figure class=\"wp-block-image\"><img loading=\"lazy\" decoding=\"async\" width=\"636\" height=\"40\" src=\"https:\/\/www.sqlitetutorial.net\/wp-content\/uploads\/2015\/12\/SQLite-Index-Explain-example.jpg\" alt=\"SQLite Index Explain example\" class=\"wp-image-588\" srcset=\"https:\/\/www.sqlitetutorial.net\/wp-content\/uploads\/2015\/12\/SQLite-Index-Explain-example.jpg 636w, https:\/\/www.sqlitetutorial.net\/wp-content\/uploads\/2015\/12\/SQLite-Index-Explain-example-300x19.jpg 300w\" sizes=\"auto, (max-width: 636px) 100vw, 636px\" \/><\/figure>\n\n\n\n<h3 class=\"wp-block-heading\">SQLite multicolumn index example<\/h3>\n\n\n\n<p>If you create an index consisting of one column, SQLite uses that column as the sort key. However, if you create an index with multiple columns, SQLite uses the additional columns as subsequent sort keys.<\/p>\n\n\n\n<p>SQLite <a href=\"https:\/\/www.sqlitetutorial.net\/sqlite-order-by\/\">sorts the data<\/a> on the multicolumn index by the first column specified in the <code>CREATE INDEX<\/code> statement. Then, it sorts the duplicate values by the second column, and so on.<\/p>\n\n\n\n<p>Therefore, the column order is crucial when creating a multicolumn index.<\/p>\n\n\n\n<p>To fully utilize a multicolumn index, the query must include conditions that match the column order defined in the index.<\/p>\n\n\n\n<p>The following statement creates a multicolumn index on the <code>first_name<\/code> and <code>last_name<\/code> columns of the <code>contacts<\/code> table:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-9\" data-shcb-language-name=\"SQL (Structured Query Language)\" data-shcb-language-slug=\"sql\"><span><code class=\"hljs language-sql\"><span class=\"hljs-keyword\">CREATE<\/span> <span class=\"hljs-keyword\">INDEX<\/span> idx_contacts_name \n<span class=\"hljs-keyword\">ON<\/span> contacts (first_name, last_name);<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-9\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">SQL (Structured Query Language)<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">sql<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p><a class=\"sql\" href=\"https:\/\/www.sqlitetutorial.net\/tryit\/query\/sqlite-index\/#8\" target=\"_blank\" rel=\"noopener noreferrer\">Try It<\/a><\/p>\n\n\n\n<p>If you query the <code>contacts<\/code> table with one of the following conditions in the <code><a href=\"https:\/\/www.sqlitetutorial.net\/sqlite-where\/\">WHERE<\/a><\/code>&nbsp;clause, SQLite will utilize the multicolumn index to search for data.<\/p>\n\n\n\n<p>1) Filter data by the <code>first_name<\/code> column.<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-10\" data-shcb-language-name=\"SQL (Structured Query Language)\" data-shcb-language-slug=\"sql\"><span><code class=\"hljs language-sql\">WHERE\n\tfirst_name = 'John';<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-10\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">SQL (Structured Query Language)<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">sql<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>2) Filter data by both <code>first_name<\/code> and <code>last_name<\/code> columns:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-11\" data-shcb-language-name=\"SQL (Structured Query Language)\" data-shcb-language-slug=\"sql\"><span><code class=\"hljs language-sql\">WHERE\n\tfirst_name = 'John' AND last_name = 'Doe';<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-11\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">SQL (Structured Query Language)<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">sql<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>However, SQLite will not use the multicolumn index if you use one of the following conditions.<\/p>\n\n\n\n<p>1) Filter by the <code>last_name<\/code> column only.<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-12\" data-shcb-language-name=\"SQL (Structured Query Language)\" data-shcb-language-slug=\"sql\"><span><code class=\"hljs language-sql\">WHERE\n\tlast_name = 'Doe';<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-12\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">SQL (Structured Query Language)<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">sql<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>2) Filter by <code>first_name<\/code> OR <code>last_name<\/code> columns.<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-13\" data-shcb-language-name=\"SQL (Structured Query Language)\" data-shcb-language-slug=\"sql\"><span><code class=\"hljs language-sql\">last_name = 'Doe' OR first_name = 'John';<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-13\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">SQL (Structured Query Language)<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">sql<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<h2 class=\"wp-block-heading\">SQLite Show Indexes<\/h2>\n\n\n\n<p>To find all indexes associated with a table, you use the following command:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-14\" data-shcb-language-name=\"SQL (Structured Query Language)\" data-shcb-language-slug=\"sql\"><span><code class=\"hljs language-sql\"><span class=\"hljs-keyword\">PRAGMA<\/span> index_list(<span class=\"hljs-string\">'table_name'<\/span>);<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-14\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">SQL (Structured Query Language)<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">sql<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>For example, this statement shows all the indexes of the <code>contacts<\/code> table:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-15\" data-shcb-language-name=\"SQL (Structured Query Language)\" data-shcb-language-slug=\"sql\"><span><code class=\"hljs language-sql\"><span class=\"hljs-keyword\">PRAGMA<\/span> index_list(<span class=\"hljs-string\">'contacts'<\/span>);<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-15\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">SQL (Structured Query Language)<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">sql<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>Here is the output:<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img loading=\"lazy\" decoding=\"async\" width=\"445\" height=\"61\" src=\"https:\/\/www.sqlitetutorial.net\/wp-content\/uploads\/2019\/08\/SQLite-index-show-indexes.png\" alt=\"SQLite index - show indexes\" class=\"wp-image-2137\" srcset=\"https:\/\/www.sqlitetutorial.net\/wp-content\/uploads\/2019\/08\/SQLite-index-show-indexes.png 445w, https:\/\/www.sqlitetutorial.net\/wp-content\/uploads\/2019\/08\/SQLite-index-show-indexes-300x41.png 300w\" sizes=\"auto, (max-width: 445px) 100vw, 445px\" \/><\/figure>\n\n\n\n<p>To get the information about the columns in an index, you use the following command:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-16\" data-shcb-language-name=\"SQL (Structured Query Language)\" data-shcb-language-slug=\"sql\"><span><code class=\"hljs language-sql\"><span class=\"hljs-keyword\">PRAGMA<\/span> index_info(<span class=\"hljs-string\">'idx_contacts_name'<\/span>);<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-16\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">SQL (Structured Query Language)<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">sql<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>This example returns the column list of the index <code>idx_contacts_name<\/code>:<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img loading=\"lazy\" decoding=\"async\" width=\"187\" height=\"63\" src=\"https:\/\/www.sqlitetutorial.net\/wp-content\/uploads\/2019\/08\/SQLite-index-column-list.png\" alt=\"\" class=\"wp-image-2138\"\/><\/figure>\n\n\n\n<p>Another way to get all indexes from a database is to query from the <code>sqlite_master<\/code> table:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-17\" data-shcb-language-name=\"SQL (Structured Query Language)\" data-shcb-language-slug=\"sql\"><span><code class=\"hljs language-sql\"><span class=\"hljs-keyword\">SELECT<\/span>\n   <span class=\"hljs-keyword\">type<\/span>, \n   <span class=\"hljs-keyword\">name<\/span>, \n   tbl_name, \n   <span class=\"hljs-keyword\">sql<\/span>\n<span class=\"hljs-keyword\">FROM<\/span>\n   sqlite_master\n<span class=\"hljs-keyword\">WHERE<\/span>\n   <span class=\"hljs-keyword\">type<\/span>= <span class=\"hljs-string\">'index'<\/span>;<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-17\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">SQL (Structured Query Language)<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">sql<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<h2 class=\"wp-block-heading\">SQLite DROP INDEX statement<\/h2>\n\n\n\n<p>To remove an index from a database, you use the <code><a href=\"https:\/\/www.sqlitetutorial.net\/sqlite-index\/sqlite-drop-index\/\">DROP INDEX<\/a><\/code> statement:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-18\" data-shcb-language-name=\"SQL (Structured Query Language)\" data-shcb-language-slug=\"sql\"><span><code class=\"hljs language-sql\"><span class=\"hljs-keyword\">DROP<\/span> <span class=\"hljs-keyword\">INDEX<\/span> &#91;<span class=\"hljs-keyword\">IF<\/span> <span class=\"hljs-keyword\">EXISTS<\/span>] index_name;<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-18\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">SQL (Structured Query Language)<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">sql<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>In this syntax, specify the index name you want to remove after the <code>DROP INDEX<\/code> keywords. The <code>IF EXISTS<\/code> option deletes the index only if it exists.<\/p>\n\n\n\n<p>For example, you use the following statement to remove the <code>idx_contacts_name<\/code> index:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-19\" data-shcb-language-name=\"SQL (Structured Query Language)\" data-shcb-language-slug=\"sql\"><span><code class=\"hljs language-sql\"><span class=\"hljs-keyword\">DROP<\/span> <span class=\"hljs-keyword\">INDEX<\/span> idx_contacts_name;<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-19\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">SQL (Structured Query Language)<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">sql<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p><a class=\"sql\" href=\"https:\/\/www.sqlitetutorial.net\/tryit\/query\/sqlite-index\/#9\" target=\"_blank\" rel=\"noopener noreferrer\">Try It<\/a><\/p>\n\n\n\n<p>The <code>idx_contacts_name<\/code> index is removed completely from the database.<\/p>\n\n\n\n<p>In this tutorial, you have learned about SQLite indexes and how to utilize them to improve the performance of queries or enforce unique constraints.<\/p>\n<div class=\"helpful-block-content\" data-title=\"\">\n\t<header>\n\t\t<div class=\"wth-question\">Was this tutorial helpful ?<\/div>\n\t\t<div class=\"wth-thumbs\">\n\t\t\t<button\n\t\t\t\tdata-post=\"566\"\n\t\t\t\tdata-post-url=\"https:\/\/www.sqlitetutorial.net\/sqlite-index\/\"\n\t\t\t\tdata-post-title=\"SQLite Index\"\n\t\t\t\tdata-response=\"1\"\n\t\t\t\tclass=\"wth-btn-rounded wth-yes-btn\"\n\t\t\t>\n\t\t\t\t<svg\n\t\t\t\t\txmlns=\"http:\/\/www.w3.org\/2000\/svg\"\n\t\t\t\t\tviewBox=\"0 0 24 24\"\n\t\t\t\t\tfill=\"none\"\n\t\t\t\t\tstroke=\"currentColor\"\n\t\t\t\t\tstroke-width=\"2\"\n\t\t\t\t\tstroke-linecap=\"round\"\n\t\t\t\t\tstroke-linejoin=\"round\"\n\t\t\t\t\tclass=\"feather feather-thumbs-up block w-full h-full\"\n\t\t\t\t>\n\t\t\t\t\t<path\n\t\t\t\t\t\td=\"M14 9V5a3 3 0 0 0-3-3l-4 9v11h11.28a2 2 0 0 0 2-1.7l1.38-9a2 2 0 0 0-2-2.3zM7 22H4a2 2 0 0 1-2-2v-7a2 2 0 0 1 2-2h3\"\n\t\t\t\t\t><\/path>\n\t\t\t\t<\/svg>\n\t\t\t\t<span class=\"sr-only\"> Yes <\/span>\n\t\t\t<\/button>\n\n\t\t\t<button\n\t\t\t\tdata-response=\"0\"\n\t\t\t\tdata-post=\"566\"\n\t\t\t\tdata-post-url=\"https:\/\/www.sqlitetutorial.net\/sqlite-index\/\"\n\t\t\t\tdata-post-title=\"SQLite Index\"\n\t\t\t\tclass=\"wth-btn-rounded wth-no-btn\"\n\t\t\t>\n\t\t\t\t<svg\n\t\t\t\t\txmlns=\"http:\/\/www.w3.org\/2000\/svg\"\n\t\t\t\t\tviewBox=\"0 0 24 24\"\n\t\t\t\t\tfill=\"none\"\n\t\t\t\t\tstroke=\"currentColor\"\n\t\t\t\t\tstroke-width=\"2\"\n\t\t\t\t\tstroke-linecap=\"round\"\n\t\t\t\t\tstroke-linejoin=\"round\"\n\t\t\t\t>\n\t\t\t\t\t<path\n\t\t\t\t\t\td=\"M10 15v4a3 3 0 0 0 3 3l4-9V2H5.72a2 2 0 0 0-2 1.7l-1.38 9a2 2 0 0 0 2 2.3zm7-13h2.67A2.31 2.31 0 0 1 22 4v7a2.31 2.31 0 0 1-2.33 2H17\"\n\t\t\t\t\t><\/path>\n\t\t\t\t<\/svg>\n\t\t\t\t<span class=\"sr-only\"> No <\/span>\n\t\t\t<\/button>\n\t\t<\/div>\n\t<\/header>\n\n\t<div class=\"wth-form hidden\">\n\t\t<div class=\"wth-form-wrapper\">\n\t\t\t<div class=\"wth-title\"><\/div>\n\t\t\t\n\t\t\t<textarea class=\"wth-message\"><\/textarea>\n\n\t\t\t<button class=\"btn btn-primary wth-btn-submit\">Send<\/button>\n\t\t\t<button class=\"btn wth-btn-cancel\">Cancel<\/button>\n\t\t\n\t\t<\/div>\n\t<\/div>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>In this tutorial, you have learned about SQLite index and how to utilize indexes for improving the performance of query or enforcing unique constraints.<\/p>\n","protected":false},"author":1,"featured_media":0,"parent":0,"menu_order":3,"comment_status":"closed","ping_status":"closed","template":"","meta":{"footnotes":""},"class_list":["post-566","page","type-page","status-publish","hentry"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.2 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>SQLite Index: An Essential Guide to SQLite Indexes<\/title>\n<meta name=\"description\" content=\"In this tutorial, you will learn how to utilize SQLite index to query data faster, speed up sort operation, and enforce unique constraints.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.sqlitetutorial.net\/sqlite-index\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"SQLite Index: An Essential Guide to SQLite Indexes\" \/>\n<meta property=\"og:description\" content=\"In this tutorial, you will learn how to utilize SQLite index to query data faster, speed up sort operation, and enforce unique constraints.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.sqlitetutorial.net\/sqlite-index\/\" \/>\n<meta property=\"og:site_name\" content=\"SQLite Tutorial\" \/>\n<meta property=\"article:modified_time\" content=\"2024-04-27T13:51:28+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/www.sqlitetutorial.net\/wp-content\/uploads\/2015\/12\/SQLite-Index.png\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data1\" content=\"5 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/www.sqlitetutorial.net\/sqlite-index\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/www.sqlitetutorial.net\/sqlite-index\/\"},\"author\":{\"name\":\"admin\",\"@id\":\"https:\/\/www.sqlitetutorial.net\/#\/schema\/person\/6d69b968cad0102e30d6694ed8dc6427\"},\"headline\":\"SQLite Index\",\"datePublished\":\"2015-12-14T03:42:33+00:00\",\"dateModified\":\"2024-04-27T13:51:28+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/www.sqlitetutorial.net\/sqlite-index\/\"},\"wordCount\":861,\"image\":{\"@id\":\"https:\/\/www.sqlitetutorial.net\/sqlite-index\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/www.sqlitetutorial.net\/wp-content\/uploads\/2015\/12\/SQLite-Index.png\",\"inLanguage\":\"en-US\"},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/www.sqlitetutorial.net\/sqlite-index\/\",\"url\":\"https:\/\/www.sqlitetutorial.net\/sqlite-index\/\",\"name\":\"SQLite Index: An Essential Guide to SQLite Indexes\",\"isPartOf\":{\"@id\":\"https:\/\/www.sqlitetutorial.net\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\/\/www.sqlitetutorial.net\/sqlite-index\/#primaryimage\"},\"image\":{\"@id\":\"https:\/\/www.sqlitetutorial.net\/sqlite-index\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/www.sqlitetutorial.net\/wp-content\/uploads\/2015\/12\/SQLite-Index.png\",\"datePublished\":\"2015-12-14T03:42:33+00:00\",\"dateModified\":\"2024-04-27T13:51:28+00:00\",\"description\":\"In this tutorial, you will learn how to utilize SQLite index to query data faster, speed up sort operation, and enforce unique constraints.\",\"breadcrumb\":{\"@id\":\"https:\/\/www.sqlitetutorial.net\/sqlite-index\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/www.sqlitetutorial.net\/sqlite-index\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/www.sqlitetutorial.net\/sqlite-index\/#primaryimage\",\"url\":\"https:\/\/www.sqlitetutorial.net\/wp-content\/uploads\/2015\/12\/SQLite-Index.png\",\"contentUrl\":\"https:\/\/www.sqlitetutorial.net\/wp-content\/uploads\/2015\/12\/SQLite-Index.png\",\"width\":663,\"height\":264},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/www.sqlitetutorial.net\/sqlite-index\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/www.sqlitetutorial.net\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"SQLite Index\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/www.sqlitetutorial.net\/#website\",\"url\":\"https:\/\/www.sqlitetutorial.net\/\",\"name\":\"SQLite Tutorial\",\"description\":\"A Step-by-step SQLite Tutorial\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/www.sqlitetutorial.net\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Person\",\"@id\":\"https:\/\/www.sqlitetutorial.net\/#\/schema\/person\/6d69b968cad0102e30d6694ed8dc6427\",\"name\":\"admin\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"SQLite Index: An Essential Guide to SQLite Indexes","description":"In this tutorial, you will learn how to utilize SQLite index to query data faster, speed up sort operation, and enforce unique constraints.","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/www.sqlitetutorial.net\/sqlite-index\/","og_locale":"en_US","og_type":"article","og_title":"SQLite Index: An Essential Guide to SQLite Indexes","og_description":"In this tutorial, you will learn how to utilize SQLite index to query data faster, speed up sort operation, and enforce unique constraints.","og_url":"https:\/\/www.sqlitetutorial.net\/sqlite-index\/","og_site_name":"SQLite Tutorial","article_modified_time":"2024-04-27T13:51:28+00:00","og_image":[{"url":"https:\/\/www.sqlitetutorial.net\/wp-content\/uploads\/2015\/12\/SQLite-Index.png","type":"","width":"","height":""}],"twitter_card":"summary_large_image","twitter_misc":{"Est. reading time":"5 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/www.sqlitetutorial.net\/sqlite-index\/#article","isPartOf":{"@id":"https:\/\/www.sqlitetutorial.net\/sqlite-index\/"},"author":{"name":"admin","@id":"https:\/\/www.sqlitetutorial.net\/#\/schema\/person\/6d69b968cad0102e30d6694ed8dc6427"},"headline":"SQLite Index","datePublished":"2015-12-14T03:42:33+00:00","dateModified":"2024-04-27T13:51:28+00:00","mainEntityOfPage":{"@id":"https:\/\/www.sqlitetutorial.net\/sqlite-index\/"},"wordCount":861,"image":{"@id":"https:\/\/www.sqlitetutorial.net\/sqlite-index\/#primaryimage"},"thumbnailUrl":"https:\/\/www.sqlitetutorial.net\/wp-content\/uploads\/2015\/12\/SQLite-Index.png","inLanguage":"en-US"},{"@type":"WebPage","@id":"https:\/\/www.sqlitetutorial.net\/sqlite-index\/","url":"https:\/\/www.sqlitetutorial.net\/sqlite-index\/","name":"SQLite Index: An Essential Guide to SQLite Indexes","isPartOf":{"@id":"https:\/\/www.sqlitetutorial.net\/#website"},"primaryImageOfPage":{"@id":"https:\/\/www.sqlitetutorial.net\/sqlite-index\/#primaryimage"},"image":{"@id":"https:\/\/www.sqlitetutorial.net\/sqlite-index\/#primaryimage"},"thumbnailUrl":"https:\/\/www.sqlitetutorial.net\/wp-content\/uploads\/2015\/12\/SQLite-Index.png","datePublished":"2015-12-14T03:42:33+00:00","dateModified":"2024-04-27T13:51:28+00:00","description":"In this tutorial, you will learn how to utilize SQLite index to query data faster, speed up sort operation, and enforce unique constraints.","breadcrumb":{"@id":"https:\/\/www.sqlitetutorial.net\/sqlite-index\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.sqlitetutorial.net\/sqlite-index\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.sqlitetutorial.net\/sqlite-index\/#primaryimage","url":"https:\/\/www.sqlitetutorial.net\/wp-content\/uploads\/2015\/12\/SQLite-Index.png","contentUrl":"https:\/\/www.sqlitetutorial.net\/wp-content\/uploads\/2015\/12\/SQLite-Index.png","width":663,"height":264},{"@type":"BreadcrumbList","@id":"https:\/\/www.sqlitetutorial.net\/sqlite-index\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/www.sqlitetutorial.net\/"},{"@type":"ListItem","position":2,"name":"SQLite Index"}]},{"@type":"WebSite","@id":"https:\/\/www.sqlitetutorial.net\/#website","url":"https:\/\/www.sqlitetutorial.net\/","name":"SQLite Tutorial","description":"A Step-by-step SQLite Tutorial","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.sqlitetutorial.net\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Person","@id":"https:\/\/www.sqlitetutorial.net\/#\/schema\/person\/6d69b968cad0102e30d6694ed8dc6427","name":"admin"}]}},"_links":{"self":[{"href":"https:\/\/www.sqlitetutorial.net\/wp-json\/wp\/v2\/pages\/566","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.sqlitetutorial.net\/wp-json\/wp\/v2\/pages"}],"about":[{"href":"https:\/\/www.sqlitetutorial.net\/wp-json\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"https:\/\/www.sqlitetutorial.net\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.sqlitetutorial.net\/wp-json\/wp\/v2\/comments?post=566"}],"version-history":[{"count":4,"href":"https:\/\/www.sqlitetutorial.net\/wp-json\/wp\/v2\/pages\/566\/revisions"}],"predecessor-version":[{"id":3376,"href":"https:\/\/www.sqlitetutorial.net\/wp-json\/wp\/v2\/pages\/566\/revisions\/3376"}],"wp:attachment":[{"href":"https:\/\/www.sqlitetutorial.net\/wp-json\/wp\/v2\/media?parent=566"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}