![]() ![]() To utilize a multicolumn index, the query must contain the condition that has the same column order as defined in the index. Therefore, the column order is very important when you create a multicolumn index. Then, it sorts the duplicate values by the second column, and so on. SQLite sorts the data on the multicolumn index by the first column specified in the CREATE INDEX statement. In case you create an index that has multiple columns, SQLite uses the additional columns as the second, third, … as the sort keys. If you create an index that consists of one column, SQLite uses that column as the sort key. To check if SQLite uses the index or not, you use the EXPLAIN QUERY PLAN statement as follows: EXPLAIN QUERY PLAN See the following statement: SELECTĮmail = Code language: SQL (Structured Query Language) ( sql ) If you query data from the contacts table based on a specific email, SQLite will use the index to locate the data. VALUES( 'David', 'Brown', 'Lisa', 'Smith', Code language: SQL (Structured Query Language) ( sql ) INSERT INTO contacts (first_name, last_name, email) Let’s insert two more rows into the contacts table. 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. ![]() SQLite issued an error message indicating that the unique index has been violated. VALUES( 'Johny', 'Doe', Code language: SQL (Structured Query Language) ( sql ) Second, insert another row with a duplicate email. VALUES( 'John', 'Doe', Code language: SQL (Structured Query Language) ( sql ) ON contacts (email) Code language: SQL (Structured Query Language) ( sql )įirst, insert a row into the contacts table. Suppose, you want to enforce that the email is unique, you create a unique index as follows: CREATE UNIQUE INDEX idx_contacts_email ) Code language: SQL (Structured Query Language) ( sql ) Let’s create a new table named contacts for demonstration. The CREATE UNIQUE INDEX creates a new unique 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 name of the table to the index belongs.The name of the index after the CREATE INDEX keywords.To create an index, you specify three important information: ON table_name(column_list) Code language: SQL (Structured Query Language) ( sql ) To create an index, you use the CREATE INDEX statement with the following syntax: CREATE INDEX index_name By looking at the index, you can quickly identify page numbers based on the keywords. Imagine an index in the database like an index of a book. This helps SQLite quickly locate the row based on the values of the indexed columns. The index contains data from the columns that you specify in the index and the corresponding rowid value. Whenever you create an index, SQLite creates a B-tree structure to hold the index data. An index consists of one or more columns, but all columns of an index must be in the same table. How does an index workĮach index must be associated with a specific table. In addition, querying using equality (=) and ranges (>, >=, <,<=) on the B-tree indexes are very efficient. 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. Note that B stands for balanced, B-tree is a balanced tree, not a binary tree. ![]() SQLite uses B-tree for organizing indexes. An index is an additional data structure that helps improve the performance of a query. Unlike a table, an index has an opposite relationship: (row, rowid). Therefore, you can consider a table as a list of pairs: (rowid, row). Each row also has a consecutive rowid sequence number used to identify the row. In the same time, each row has the same column structure that consists of cells. In relational databases, a table is a list of rows. Summary: in this tutorial, you will learn how to use SQLite indexes to query data faster, speed up sort operation, and enforce unique constraints. ![]()
0 Comments
Leave a Reply. |