Introduction4 SQLite


Published on

Published in: Technology
  • Be the first to comment

  • Be the first to like this

No Downloads
Total views
On SlideShare
From Embeds
Number of Embeds
Embeds 0
No embeds

No notes for slide

Introduction4 SQLite

  1. 1. Introduction 4 SQLite Stanley Huang 1/20
  2. 2. AgendaHow to install SQLite on UbuntuSQLite client samplesSQLite performance tuning and optimization tipsFAQReferenceQ&A 2/20
  3. 3. How to install SQLite on UbuntuUse 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 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
  4. 4. SQLite client samples$ which sqlite3/usr/bin/sqlite3$ sqlite3sqlite> 4/20
  5. 5. .helpsqlite> .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. 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. 7. .help (cont.).output FILENAME Send output to FILENAME.prompt MAIN CONTINUE Replace the standard prompts.quit Exit this 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 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. 8. Create Databases$ sqlite3sqlite> attach database /tmp/db1 as db1;sqlite> attach database /tmp/db2 as db2;sqlite> .databaseseq name file--- --------------- ----------------------------------------------------------0 main2 db1 /tmp/db13 db2 /tmp/db2 8/20
  9. 9. Create Tablessqlite> 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. 10. Insert Datasqlite> 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
  11. 11. Show Tables/Schemasqlite> .restore db1sqlite> .tablest11 t12sqlite> .restore db2sqlite> .tablest21 t22sqlite> .schema t21CREATE TABLE t11(id int); 11/20
  12. 12. SQLite performance tuning and optimization tipsBack 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. 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 schemaDont generalize the database schemaUse temporary tables for intermediate resultAvoid using view and sub-queriesOrdering the tables correctly in where clause 13/20
  14. 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. 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. 16. FAQWhat datatype does SQLite support? SQLite uses dynamic typing. Content could be INTEGER, REAL, TEXT, BOLB or NULLSQLite 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.Whats the maximum size for varchar? SQLite does not enforce the length of a VARCHAR. 16/20
  17. 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 didnt 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
  18. 18. ReferenceSQLite Document of SQLite of SQLite 18/20
  19. 19. Q&A 19/20
  20. 20. Thanks 20/20