|
| 1 | +Tutorial |
| 2 | +-------- |
| 3 | +### What is an index? |
| 4 | + |
| 5 | +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 row-id sequence number used to identify the row. Therefore, you can consider a table as a list of pairs: (rowid, row). |
| 6 | + |
| 7 | +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. |
| 8 | + |
| 9 | +SQLite uses B-tree for organizing indexes. Note that B stands for balanced, B-tree is a balanced tree, not a binary tree. |
| 10 | + |
| 11 | +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. |
| 12 | + |
| 13 | +### SQLite CREATE INDEX statement |
| 14 | +To create an index, you use the CREATE INDEX statement with the following syntax: |
| 15 | + |
| 16 | + CREATE [UNIQUE] INDEX index_name |
| 17 | + ON table_name(column_list); |
| 18 | + |
| 19 | +To create an index, you specify three important information: |
| 20 | + |
| 21 | +- The name of the index after the CREATE INDEX keywords. |
| 22 | +- The name of the table to the index belongs. |
| 23 | +- A list of columns of the index. |
| 24 | + |
| 25 | +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. |
| 26 | + |
| 27 | +Suppose, you want to enforce that the email is unique, you create a unique index as follows: |
| 28 | + |
| 29 | + .exec |
| 30 | + CREATE TABLE contacts ( |
| 31 | + first_name text NOT NULL, |
| 32 | + last_name text NOT NULL, |
| 33 | + email text NOT NULL |
| 34 | + ); |
| 35 | + |
| 36 | + CREATE UNIQUE INDEX idx_contacts_email |
| 37 | + ON contacts (email); |
| 38 | + |
| 39 | + INSERT INTO contacts (first_name, last_name, email) |
| 40 | + VALUES('David','Brown','[email protected]'), |
| 41 | + ('Johny','Doe','[email protected]'), |
| 42 | + ('Lisa','Smith','[email protected]'); |
| 43 | + |
| 44 | +If you want to insert a duplicated row, an error message will appear, 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. |
| 45 | + |
| 46 | +If you query data from the contacts table based on a specific email, SQLite will use the index to locate the data |
| 47 | + |
| 48 | + .exec |
| 49 | + CREATE TABLE contacts ( |
| 50 | + first_name text NOT NULL, |
| 51 | + last_name text NOT NULL, |
| 52 | + email text NOT NULL |
| 53 | + ); |
| 54 | + |
| 55 | + CREATE UNIQUE INDEX idx_contacts_email |
| 56 | + ON contacts (email); |
| 57 | + |
| 58 | + INSERT INTO contacts (first_name, last_name, email) |
| 59 | + VALUES('David','Brown','[email protected]'), |
| 60 | + ('Johny','Doe','[email protected]'), |
| 61 | + ('Lisa','Smith','[email protected]'); |
| 62 | + |
| 63 | + SELECT |
| 64 | + first_name, |
| 65 | + last_name, |
| 66 | + email |
| 67 | + FROM |
| 68 | + contacts |
| 69 | + WHERE |
| 70 | + |
| 71 | + |
| 72 | +To check if SQLite uses the index or not, you use the EXPLAIN QUERY PLAN |
| 73 | + |
| 74 | + .exec |
| 75 | + CREATE TABLE contacts ( |
| 76 | + first_name text NOT NULL, |
| 77 | + last_name text NOT NULL, |
| 78 | + email text NOT NULL |
| 79 | + ); |
| 80 | + |
| 81 | + CREATE UNIQUE INDEX idx_contacts_email |
| 82 | + ON contacts (email); |
| 83 | + |
| 84 | + INSERT INTO contacts (first_name, last_name, email) |
| 85 | + VALUES('David','Brown','[email protected]'), |
| 86 | + ('Johny','Doe','[email protected]'), |
| 87 | + ('Lisa','Smith','[email protected]'); |
| 88 | + |
| 89 | + EXPLAIN QUERY PLAN |
| 90 | + SELECT |
| 91 | + first_name, |
| 92 | + last_name, |
| 93 | + email |
| 94 | + FROM |
| 95 | + contacts |
| 96 | + WHERE |
| 97 | + |
| 98 | + |
| 99 | +### SQLite multicolumn index |
| 100 | +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. |
| 101 | + |
| 102 | +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. |
| 103 | + |
| 104 | +Therefore, the column order is very important when you create a multicolumn index. |
| 105 | + |
| 106 | +To utilize a multicolumn index, the query must contain the condition that has the same column order as defined in the index. |
| 107 | + |
| 108 | +### SQLite DROP INDEX statement |
| 109 | +To remove an index from a database, you use the DROP INDEX statement as follows: |
| 110 | + |
| 111 | + DROP INDEX [IF EXISTS] index_name; |
| 112 | + |
| 113 | +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. |
| 114 | + |
| 115 | +Exercise |
| 116 | +-------- |
| 117 | +Create an index for the following table, index by customerID. Then select all. |
| 118 | + |
| 119 | +Tutorial Code |
| 120 | +------------- |
| 121 | + CREATE TABLE Customer ( name TEXT,customerID INTERGER, city TEXT, email TEXT); |
| 122 | + |
| 123 | + INSERT INTO Customer ( name, customerID, city,email) VALUES |
| 124 | + ("John", 1, "Huston", [email protected]), ("Eric", 2, "Paris", "[email protected]"), ("Jessica",3, "Kiev", "[email protected]"), ("Mike", 4, "Paris", "[email protected]"), ("Jeff", 5, Antananarivo, "[email protected]"),("Ben", 6, Antananarivo, "[email protected]"); |
| 125 | + |
| 126 | + -- write your code here |
| 127 | + |
| 128 | +Expected Output |
| 129 | +--------------- |
| 130 | + "John", 1, "Huston", [email protected]" |
| 131 | + "Eric", 2, "Paris", "[email protected]" |
| 132 | + "Jessica",3, "Kiev", "[email protected]" |
| 133 | + "Mike", 4, "Paris", "[email protected]" |
| 134 | + "Jeff", 5, Antananarivo, "[email protected]" |
| 135 | + "Ben", 6, Antananarivo, "[email protected]" |
| 136 | + |
| 137 | +Solution |
| 138 | +-------- |
| 139 | + CREATE TABLE Customer ( name TEXT,customerID INTERGER, city TEXT, email TEXT); |
| 140 | + |
| 141 | + INSERT INTO Customer ( name, customerID, city,email) VALUES |
| 142 | + ("John", 1, "Huston", [email protected]), ("Eric", 2, "Paris", "[email protected]"), ("Jessica",3, "Kiev", "[email protected]"), ("Mike", 4, "Paris", "[email protected]"), ("Jeff", 5, Antananarivo, "[email protected]"),("Ben", 6, Antananarivo, "[email protected]"); |
| 143 | + |
| 144 | + -- write your code here |
| 145 | + |
| 146 | + CREATE UNIQUE INDEX idx_Cus_id |
| 147 | + ON Customer (customerID); |
| 148 | + |
| 149 | + Select * from Customer |
0 commit comments