SlideShare a Scribd company logo
1 of 20
Introduction 4 SQLite


        Stanley Huang
    wenline1001@gmail.com




                            1/20
Agenda
How to install SQLite on Ubuntu
SQLite client samples
SQLite performance tuning and optimization
 tips
FAQ
Reference
Q&A

                                2/20
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
SQLite client samples
$ which sqlite3
/usr/bin/sqlite3
$ sqlite3
sqlite>




                         4/20
.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
.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
.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
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
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
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
Show Tables/Schema
sqlite> .restore db1
sqlite> .tables
t11 t12
sqlite> .restore db2
sqlite> .tables
t21 t22
sqlite> .schema t21
CREATE TABLE t11(id int);


                            11/20
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
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
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
SQLite performance tuning and
  optimization tips (Cont.)
Reduce data file size
  Compact database
  Recompiling SQLite with different page size
  Compress large data




                                     15/20
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
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
Reference
SQLite Document
http://sqlite.org/docs.html
Pragma of SQLite
http://www.sqlite.org/pragma.html
Limits of SQLite
http://www.sqlite.org/limits.html


                                    18/20
Q&A




      19/20
Thanks




         20/20

More Related Content

What's hot

android sqlite
android sqliteandroid sqlite
android sqlite
Deepa Rani
 
Microsoft sql server architecture
Microsoft sql server architectureMicrosoft sql server architecture
Microsoft sql server architecture
Naveen Boda
 

What's hot (20)

A brief introduction to SQLite PPT
A brief introduction to SQLite PPTA brief introduction to SQLite PPT
A brief introduction to SQLite PPT
 
SQLite database in android
SQLite database in androidSQLite database in android
SQLite database in android
 
Database
DatabaseDatabase
Database
 
android sqlite
android sqliteandroid sqlite
android sqlite
 
Getting Started with SQLite
Getting Started with SQLiteGetting Started with SQLite
Getting Started with SQLite
 
Persitance Data with sqlite
Persitance Data with sqlitePersitance Data with sqlite
Persitance Data with sqlite
 
Android Training (Storing data using SQLite)
Android Training (Storing data using SQLite)Android Training (Storing data using SQLite)
Android Training (Storing data using SQLite)
 
Mysql
MysqlMysql
Mysql
 
Using sqlite database in android with sqlite manager browser add ons
Using sqlite database in android with sqlite manager browser add onsUsing sqlite database in android with sqlite manager browser add ons
Using sqlite database in android with sqlite manager browser add ons
 
Microsoft sql server architecture
Microsoft sql server architectureMicrosoft sql server architecture
Microsoft sql server architecture
 
HPE NonStop SQL WebDBS - Introduction
HPE NonStop SQL WebDBS - IntroductionHPE NonStop SQL WebDBS - Introduction
HPE NonStop SQL WebDBS - Introduction
 
Android database tutorial
Android database tutorialAndroid database tutorial
Android database tutorial
 
PGConf.ASIA 2019 Bali - A step towards SQL/MED - DATALINK - Gilles Darold
PGConf.ASIA 2019 Bali - A step towards SQL/MED - DATALINK - Gilles DaroldPGConf.ASIA 2019 Bali - A step towards SQL/MED - DATALINK - Gilles Darold
PGConf.ASIA 2019 Bali - A step towards SQL/MED - DATALINK - Gilles Darold
 
MYSQL-Database
MYSQL-DatabaseMYSQL-Database
MYSQL-Database
 
Geek Sync | SQL Security Principals and Permissions 101
Geek Sync | SQL Security Principals and Permissions 101Geek Sync | SQL Security Principals and Permissions 101
Geek Sync | SQL Security Principals and Permissions 101
 
Geek Sync | Data Integrity Demystified - Deborah Melkin | IDERA
Geek Sync | Data Integrity Demystified - Deborah Melkin | IDERAGeek Sync | Data Integrity Demystified - Deborah Melkin | IDERA
Geek Sync | Data Integrity Demystified - Deborah Melkin | IDERA
 
Sql server backup internals
Sql server backup internalsSql server backup internals
Sql server backup internals
 
Android Database
Android DatabaseAndroid Database
Android Database
 
Difference between sql server 2008 and sql server 2012
Difference between sql server 2008 and sql server 2012Difference between sql server 2008 and sql server 2012
Difference between sql server 2008 and sql server 2012
 
Developers’ mDay 2021: Bogdan Kecman, Oracle – MySQL nekad i sad
Developers’ mDay 2021: Bogdan Kecman, Oracle – MySQL nekad i sadDevelopers’ mDay 2021: Bogdan Kecman, Oracle – MySQL nekad i sad
Developers’ mDay 2021: Bogdan Kecman, Oracle – MySQL nekad i sad
 

Viewers also liked (12)

MySQL進階介紹
MySQL進階介紹MySQL進階介紹
MySQL進階介紹
 
MySQL入門介紹
MySQL入門介紹MySQL入門介紹
MySQL入門介紹
 
Python sqlite3
Python sqlite3Python sqlite3
Python sqlite3
 
HDMI
HDMIHDMI
HDMI
 
Hdmi cables
Hdmi cablesHdmi cables
Hdmi cables
 
SQLite
SQLiteSQLite
SQLite
 
Sqlite
SqliteSqlite
Sqlite
 
SQLite
SQLiteSQLite
SQLite
 
Graphical User Interface (Gui)
Graphical User Interface (Gui)Graphical User Interface (Gui)
Graphical User Interface (Gui)
 
Uml - An Overview
Uml - An OverviewUml - An Overview
Uml - An Overview
 
XML - Parte 2
XML - Parte 2XML - Parte 2
XML - Parte 2
 
Extreme programming
Extreme programmingExtreme programming
Extreme programming
 

Similar to Introduction4 SQLite

database-querry-student-note
database-querry-student-notedatabase-querry-student-note
database-querry-student-note
Leerpiny Makouach
 
My sql with querys
My sql with querysMy sql with querys
My sql with querys
NIRMAL FELIX
 
Sqlite3 command reference
Sqlite3 command referenceSqlite3 command reference
Sqlite3 command reference
Raghu nath
 
Myth busters - performance tuning 103 2008
Myth busters - performance tuning 103 2008Myth busters - performance tuning 103 2008
Myth busters - performance tuning 103 2008
paulguerin
 
mysqlanditsbasiccommands-150226033905-conversion-gate02.pdf
mysqlanditsbasiccommands-150226033905-conversion-gate02.pdfmysqlanditsbasiccommands-150226033905-conversion-gate02.pdf
mysqlanditsbasiccommands-150226033905-conversion-gate02.pdf
pradnyamulay
 

Similar to Introduction4 SQLite (20)

Sq lite
Sq liteSq lite
Sq lite
 
sql_bootcamp.pdf
sql_bootcamp.pdfsql_bootcamp.pdf
sql_bootcamp.pdf
 
database-querry-student-note
database-querry-student-notedatabase-querry-student-note
database-querry-student-note
 
Introduction to Oracle Database.pptx
Introduction to Oracle Database.pptxIntroduction to Oracle Database.pptx
Introduction to Oracle Database.pptx
 
My sql with querys
My sql with querysMy sql with querys
My sql with querys
 
SQL Server 2008 Performance Enhancements
SQL Server 2008 Performance EnhancementsSQL Server 2008 Performance Enhancements
SQL Server 2008 Performance Enhancements
 
Sq lite module5
Sq lite module5Sq lite module5
Sq lite module5
 
Getting Started with MySQL I
Getting Started with MySQL IGetting Started with MySQL I
Getting Started with MySQL I
 
Sqlite3 command reference
Sqlite3 command referenceSqlite3 command reference
Sqlite3 command reference
 
OOW16 - Oracle Database 12c - The Best Oracle Database 12c New Features for D...
OOW16 - Oracle Database 12c - The Best Oracle Database 12c New Features for D...OOW16 - Oracle Database 12c - The Best Oracle Database 12c New Features for D...
OOW16 - Oracle Database 12c - The Best Oracle Database 12c New Features for D...
 
OOW16 - Oracle Database 12c - The Best Oracle Database 12c New Features for D...
OOW16 - Oracle Database 12c - The Best Oracle Database 12c New Features for D...OOW16 - Oracle Database 12c - The Best Oracle Database 12c New Features for D...
OOW16 - Oracle Database 12c - The Best Oracle Database 12c New Features for D...
 
Oracle 10g
Oracle 10gOracle 10g
Oracle 10g
 
7. SQL.pptx
7. SQL.pptx7. SQL.pptx
7. SQL.pptx
 
Myth busters - performance tuning 103 2008
Myth busters - performance tuning 103 2008Myth busters - performance tuning 103 2008
Myth busters - performance tuning 103 2008
 
MySQL and its basic commands
MySQL and its basic commandsMySQL and its basic commands
MySQL and its basic commands
 
Lab2 ddl commands
Lab2 ddl commandsLab2 ddl commands
Lab2 ddl commands
 
Cordova training - Day 9 - SQLITE
Cordova training - Day 9 - SQLITECordova training - Day 9 - SQLITE
Cordova training - Day 9 - SQLITE
 
mysqlanditsbasiccommands-150226033905-conversion-gate02.pdf
mysqlanditsbasiccommands-150226033905-conversion-gate02.pdfmysqlanditsbasiccommands-150226033905-conversion-gate02.pdf
mysqlanditsbasiccommands-150226033905-conversion-gate02.pdf
 
Sql tutorial
Sql tutorialSql tutorial
Sql tutorial
 
My sql.ppt
My sql.pptMy sql.ppt
My sql.ppt
 

Recently uploaded

EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptxEIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
Earley Information Science
 

Recently uploaded (20)

Understanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdfUnderstanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdf
 
[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf
 
Handwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed textsHandwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed texts
 
08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men
 
Tech Trends Report 2024 Future Today Institute.pdf
Tech Trends Report 2024 Future Today Institute.pdfTech Trends Report 2024 Future Today Institute.pdf
Tech Trends Report 2024 Future Today Institute.pdf
 
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
 
GenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationGenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day Presentation
 
Boost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfBoost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdf
 
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law DevelopmentsTrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
 
How to convert PDF to text with Nanonets
How to convert PDF to text with NanonetsHow to convert PDF to text with Nanonets
How to convert PDF to text with Nanonets
 
Strategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a FresherStrategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a Fresher
 
🐬 The future of MySQL is Postgres 🐘
🐬  The future of MySQL is Postgres   🐘🐬  The future of MySQL is Postgres   🐘
🐬 The future of MySQL is Postgres 🐘
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected Worker
 
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
 
Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024
 
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptxEIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
 
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
 
Data Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonData Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt Robison
 
Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...
 
Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024
 

Introduction4 SQLite

  • 1. Introduction 4 SQLite Stanley Huang wenline1001@gmail.com 1/20
  • 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
  • 4. SQLite client samples $ which sqlite3 /usr/bin/sqlite3 $ sqlite3 sqlite> 4/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
  • 11. Show Tables/Schema sqlite> .restore db1 sqlite> .tables t11 t12 sqlite> .restore db2 sqlite> .tables t21 t22 sqlite> .schema t21 CREATE TABLE t11(id int); 11/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
  • 18. Reference SQLite Document http://sqlite.org/docs.html Pragma of SQLite http://www.sqlite.org/pragma.html Limits of SQLite http://www.sqlite.org/limits.html 18/20
  • 19. Q&A 19/20
  • 20. Thanks 20/20