2. Agenda
How to install SQLite on Ubuntu
SQLite client samples
SQLite performance tuning and optimization
tips
FAQ
Reference
Q&A
2/20
3. How to install SQLite on Ubuntu
Use apt-get
stanley@Stanley-Ubuntu:/etc$ sudo apt-get -y install sqlite3
Reading package lists... Done
Building dependency tree
Reading state information... Done
Suggested packages:
sqlite3-doc
The following NEW packages will be installed:
sqlite3
0 upgraded, 1 newly installed, 0 to remove and 0 not upgraded.
Need to get 28.3kB of archives.
After this operation, 119kB of additional disk space will be used.
Get:1 http://us.archive.ubuntu.com/ubuntu/ lucid/main sqlite3 3.6.22-1 [28.3kB]
Fetched 28.3kB in 1s (21.6kB/s)
Selecting previously deselected package sqlite3.
(Reading database ... 455043 files and directories currently installed.)
Unpacking sqlite3 (from .../sqlite3_3.6.22-1_amd64.deb) ...
Processing triggers for man-db ...
Setting up sqlite3 (3.6.22-1) ...
stanley@Stanley-Ubuntu:/etc$
3/20
5. .help
sqlite> .help
.backup ?DB? FILE Backup DB (default "main") to FILE
.bail ON|OFF Stop after hitting an error. Default OFF
.databases List names and files of attached databases
.dump ?TABLE? ... Dump the database in an SQL text format
If TABLE specified, only dump tables matching
LIKE pattern TABLE.
.echo ON|OFF Turn command echo on or off
.exit Exit this program
.explain ?ON|OFF? Turn output mode suitable for EXPLAIN on or off.
With no args, it turns EXPLAIN on.
.genfkey ?OPTIONS? Options are:
--no-drop: Do not drop old fkey triggers.
--ignore-errors: Ignore tables with fkey errors
--exec: Execute generated SQL immediately
See file tool/genfkey.README in the source
distribution for further information.
5/20
6. .help (cont.)
.import FILE TABLE Import data from FILE into TABLE
.indices ?TABLE? Show names of all indices
If TABLE specified, only show indices for tables
matching LIKE pattern TABLE.
.load FILE ?ENTRY? Load an extension library
.mode MODE ?TABLE? Set output mode where MODE is one of:
csv Comma-separated values
column Left-aligned columns. (See .width)
html HTML <table> code
insert SQL insert statements for TABLE
line One value per line
list Values delimited by .separator string
tabs Tab-separated values
tcl TCL list elements
6/20
7. .help (cont.)
.output FILENAME Send output to FILENAME
.prompt MAIN CONTINUE Replace the standard prompts
.quit Exit this program
.read FILENAME Execute SQL in FILENAME
.restore ?DB? FILE Restore content of DB (default "main") from FILE
.schema ?TABLE? Show the CREATE statements
If TABLE specified, only show tables matching
LIKE pattern TABLE.
.show Show the current values for various settings
.tables ?TABLE? List names of tables
If TABLE specified, only list tables matching
LIKE pattern TABLE.
7/20
8. Create Databases
$ sqlite3
sqlite> attach database '/tmp/db1' as db1;
sqlite> attach database '/tmp/db2' as db2;
sqlite> .database
seq name file
--- --------------- ----------------------------------------------------------
0 main
2 db1 /tmp/db1
3 db2 /tmp/db2
8/20
9. Create Tables
sqlite> create table db1.t11 (id int);
sqlite> create table db1.t12 (id int);
sqlite> create table db2.t21 (id int);
sqlite> create table db2.t22 (id int);
9/20
10. Insert Data
sqlite> insert into db1.t11 values (11);
sqlite> insert into db1.t12 values (12);
sqlite> insert into db2.t21 values (21);
sqlite> insert into db2.t22 values (22);
10/20
12. SQLite performance tuning and
optimization tips
Back it up or lose it!
File base, easy to backup by cp command.
Always add an anonymous primary key!
PRAGMA cache_size = ?
Prior SQLite 2.8.6 split into a BTREE with default “1K” size pages, after 2.8.6 default cache size
is “2K”.
PRAGMA temp_store = 1
The choices are DEFAULT (0), FILE (1) and MEMORY (2).
PRAGMA synchronous = OFF
Default SQLite would wait data write to file, when set synchronous “OFF” and process crash,
the data file might be corrupt.
PRAGMA journal_mode = MEMORY
Transaction would be fast, but if process crash, data file might be corrupt.
USE TRANSACTION
12/20
13. SQLite performance tuning and
optimization tips (Cont.)
Create proper index.
Proper column of indexes.
Proper number of indexes.
Compare with “create index then insert data” and “insert data then
create index”
With mass of data, “insert data then create index” would be faster.
Simplify the database schema
Don't generalize the database schema
Use temporary tables for intermediate result
Avoid using view and sub-queries
Ordering the tables correctly in where clause
13/20
14. SQLite performance tuning and
optimization tips (Cont.)
Optimizing queries
Use EXPLAIN command
Temporary tables
Order subqueries and smaller results returned
first
Use LIMIT and OFFSET
Replace GLOB and LIKE when possible
USE IN instead of OR
14/20
15. SQLite performance tuning and
optimization tips (Cont.)
Reduce data file size
Compact database
Recompiling SQLite with different page size
Compress large data
15/20
16. FAQ
What datatype does SQLite support?
SQLite uses dynamic typing. Content could be INTEGER, REAL,
TEXT, BOLB or NULL
SQLite let me insert a string into a integer column.
SQLite uses dynamic typing.
Is SQLite threadsafe?
SQLite is threadsafe.
Prior to 3.3.1, SQLite could only used in the same thread.
What's the maximum size for varchar?
SQLite does not enforce the length of a VARCHAR.
16/20
17. FAQ (cont.)
Does SQLite support BOLB?
SQLite 3.0 and later supported.
How to delete columns from existing table?
Alter table only support “add column” and “rename table”. Else, you need to
re-create table.
Does SQLite support foreign key?
As of verson 3.6.19, SQLite supports foreign key constraint.
I deleted lots of data but data file didn't get smaller.
You need to run “VACUUM” command to shrink it.
In SQLite 3.1, you could use auto_vacuum pragma to shrink it automatically.
But not recommanded, because it take a long time to do it.
17/20