Dev/Sqlite/Tips
Contents
Avoid LIKE
- Like is not performant, as it will search both lowercase and uppercase. You can get around this with PRAGMA case_sensitive_like=OFF;
- Like does not use an index.
Consider the following table:
CREATE TABLE tbl1 ( id integer NOT NULL PRIMARY KEY , name varchar(255) NOT NULL ); CREATE INDEX tbl1__name ON tbl1 (name);
Now, consider the following query:
SELECT id FROM tbl1 WHERE name LIKE 'a%';
The clause name LIKE 'a%'
does not use the index tbl1__name
.
= Alternative: Use comparison
If the like has a wildcard at the end, and the data is standardized to one case (all lowercase or all uppercase), use an equality instead:
SELECT id FROM tbl1 WHERE name >= 'a' AND name < 'b';
This uses the index tbl1__name
and returns the same data.
Avoid ORDER BY
ORDER BY will do an expensive sort operation which may not use an index. This will come into play with multiple-table joins
SELECT t1.id FROM tbl1 t1 JOIN tbl2 t2 ON t1.id = t2.id ORDER BY t2.age ;
In most cases, SQLite will only apply one index. In this example, it will be the index for the JOIN. No index gets applied for the score column, even if one is available In some cases, SQLite will actually sort the result-set by the ORDER BY first, before it applies the WHERE.
Alternative: denormalize the table
Putting all the columns in one table and building an index across them.
Alternative: multiple where clauses
SELECT t1.id FROM tbl1 t1 JOIN tbl2 t2 ON t1.id = t2.id WHERE t2.age > 100 AND t2.age <= 130 ; SELECT t1.id FROM tbl1 t1 JOIN tbl2 t2 ON t1.id = t2.id WHERE t2.age > 70 AND t2.age <= 100 ;
Beware bulk INSERTs into a table with a PRIMARY KEY
INSERT INTO INSERT INTO tbl2 (id, key) SELECT t1.id FROM tbl1 t1 JOIN tbl2 t2 ON t1.id = t2.id WHERE t2.age > 100 AND t2.age <= 130 ;
Instead, insert into the table with an ORDER BY
ATTACH
ATTACH database does not affect performance. There is no difference between the following
SELECT t1.id FROM tbl1 t1 JOIN tbl2 t2 ON t1.id = t2.id ;
ATTACH 'db2.sqlite3' AS 'db2'; SELECT t1.id FROM tbl1 t1 JOIN db2.tbl2 t2 ON t1.id = t2.id ;
Get a Solid State Drive
SQLite does a lot of disk reading and writing