Indexes
=======
* An index is a data structure (usually a B-tree) for fast column lookups.

* You don't need to (and probably shouldn't) put an index on every column, but if for example a column is used in the WHERE clause of an SQL statement, you should consider adding an index to that column.

* If a table is not indexed, queries may go slow because the database may have to do a full table scan (i.e. linear search).

CREATE TABLE RandomData(
	ID INTEGER PRIMARY KEY,
	Num FLOAT NOT NULL
);

INSERT INTO RandomData(Num) VALUES (RANDOM());
INSERT INTO RandomData(Num) VALUES (RANDOM());
INSERT INTO RandomData(Num) VALUES (RANDOM());
INSERT INTO RandomData(Num) VALUES (RANDOM());
INSERT INTO RandomData(Num) VALUES (RANDOM());
INSERT INTO RandomData(Num) VALUES (RANDOM());
INSERT INTO RandomData(Num) VALUES (RANDOM());

SELECT * FROM RandomData;

INSERT INTO RandomData(Num) SELECT a.Num/b.Num AS Num FROM RandomData AS a JOIN RandomData AS b ON a.ID != b.ID;
INSERT INTO RandomData(Num) SELECT a.Num/b.Num AS Num FROM RandomData AS a JOIN RandomData AS b ON a.ID != b.ID;
INSERT INTO RandomData(Num) SELECT a.Num/b.Num AS Num FROM RandomData AS a JOIN RandomData AS b ON a.ID != b.ID;

SELECT COUNT(*) FROM RandomData;
SELECT * FROM RandomData WHERE ID=1234567;  /* SQLite automatically puts an index on PRIMARY KEY and UNIQUE columns */
SELECT * FROM RandomData WHERE Num>=.998 AND Num<=.999;  /* Not indexed; it takes a second. */

CREATE INDEX Idx_RandomData_Num ON RandomData(Num);    /* Naming convention for an index:  Idx_TableName_ColumnName; */

SELECT * FROM RandomData WHERE Num>=.998 AND Num<=.999;

DROP INDEX Idx_RandomData_Num;

SELECT * FROM RandomData LIMIT 10;

