SQLite 3
  Scott MacVicar
PHP Barcelona 2008
Who Am I?


• Employed by Jelsoft Enterprises Ltd
• PHP (SQLite3, imagick, svn)
• GD
Overview

• What is SQLite?
• Why use SQLite?
• Who uses SQLite?
• SQLite Explained
• Difference between SQLite 2 and 3
• P...
What is SQLite?

• Embedded SQL Database Engine
• Serverless - accesses disk directly
• Single file
• Cross Platform / Arch...
Why use SQLite?


• Zero Configuration
• Portable
• Public Domain
Public Domain?


May you do good and not evil.

May you find forgiveness for yourself and forgive others.

May you share fr...
¿Public Domain?
Big SQLite Users



                                            •   Lightroom

                                           ...
Big SQLite Users

•   Mail

•   Safari

•   Aperture

•   Core Data

•   iPhone
             sqlite3 ~/Library/Mail/Envelo...
Big SQLite Users



           •   Gears

           •   Android
Big SQLite Users




•   Virus Information
Big SQLite Users


           •   History

           •   Bookmarks

           •   Downloads
Big SQLite Users




•   Metadata
Where can SQLite be used?

• Replacement for a custom file format
• Temporary data storage (Unit Tests) - :memory:
• Altern...
Versions


• SQLite - 2.8.17
• PDO - 3.3.7 + 2.8.17   (3.6.2 in PHP 5.3)

• SQLite3 - 3.6.2, minimum is 3.3.9
SQLite 3
•   64-bit Rowids            •   Connections shared
                                 between threads
•   BLOB sup...
SQLite v2

• Smaller subset of SQL supported
• Larger file format
• Database format incompatible with v3
• No Unicode suppo...
Examples


• Firefox 3.0
  • Profile directory contains places.sqlite
  • Schema on the next page
• iPhone SMS
Firefox Places Schema
PDO
SQLite3
Live Demo (Hopefully)
iPhone SMS Schema




 /private/var/root/Library/SMS/sms.db
 ~/Library/Application Support/MobileSync/Backup
Custom Functions


• Define SQL Functions in PHP
• Implement missing features
• Aggregation functions
Function Example
Custom Collations

•   Sorting in alphabetical   ☹   ☺
    order
                              a   a
•   ICU also provides...
Fulltext Indexing
Fulltext Searching
SQLite3 Extension

        SQLite3        SQLite3Stmt
open              paramCount
close             close
exec           ...
SQLite3 Extension

   SQLite3Result
fetchArray
reset
finalize
columnType
columnName
numColumns
Improving Performance

• Pragma
  • cache_size
  • fullfsync
  • synchronous
• Shared Cache
• Use Transactions
Benchmarks


•   Dual Xeon 3.2GHz,
    2GB RAM, Centos 5

•   MySQL vs SQLite

•   Synthetic Benchmark -
    YMMV
        ...
Benchmarks - Tests

• 15 tests
• INSERT
• SELECT
• UPDATE
• DELETE
• DROP
Benchmarks - Insert
                          MySQL
10.0                      SQLite 2
                          SQLite 3
...
Benchmarks - Select
                          MySQL
10.0                      SQLite 2
                          SQLite 3
...
Benchmarks - Indices
                         MySQL
1.2                      SQLite 2
                         SQLite 3
0....
Benchmarks - Update
                                   MySQL
5.00                               SQLite 2
                 ...
Benchmarks - Delete
                                        MySQL
2.0                                     SQLite 2
       ...
Benchmarks - Summary


• SQLite 3 is faster in almost every test
• Using MyISAM would show faster results
• Doesn’t test c...
Converting from MySQL

• varchar, char -> text
• smallint, tinyint, int -> integer
• auto_increment -> autoincrement
• unk...
Unsupported SQL - Examples




                Original Image by Clive Arundell
Limitations

• Very large datasets
• High concurrency
• No Foreign Keys
• Nested Transactions
• Writing to Views
Miscellaneous


•   Integrity Check

•   Reclaim Unused Space

•   Backup
Tools - SQLite Manager
Resources

• http://www.sqlite.org
• http://www.php.net/sqlite3
• http://pecl.php.net/sqlite3
• http://talks.macvicar.net
...
Upcoming SlideShare
Loading in...5
×

SQLite 3

12,305

Published on

Talk on SQLite given at PHP Barcelona 2008

Published in: Technology, Education
0 Comments
15 Likes
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total Views
12,305
On Slideshare
0
From Embeds
0
Number of Embeds
1
Actions
Shares
0
Downloads
0
Comments
0
Likes
15
Embeds 0
No embeds

No notes for slide

SQLite 3

  1. 1. SQLite 3 Scott MacVicar PHP Barcelona 2008
  2. 2. Who Am I? • Employed by Jelsoft Enterprises Ltd • PHP (SQLite3, imagick, svn) • GD
  3. 3. Overview • What is SQLite? • Why use SQLite? • Who uses SQLite? • SQLite Explained • Difference between SQLite 2 and 3 • PDO / SQLite3
  4. 4. What is SQLite? • Embedded SQL Database Engine • Serverless - accesses disk directly • Single file • Cross Platform / Architecture • Small footprint • Manifest typing
  5. 5. Why use SQLite? • Zero Configuration • Portable • Public Domain
  6. 6. Public Domain? May you do good and not evil. May you find forgiveness for yourself and forgive others. May you share freely, never taking more than you give.
  7. 7. ¿Public Domain?
  8. 8. Big SQLite Users • Lightroom • AIR sqlite3 ~/Pictures/Lightroom/Lightroom Library.aglib
  9. 9. Big SQLite Users • Mail • Safari • Aperture • Core Data • iPhone sqlite3 ~/Library/Mail/Envelope Index sqlite3 ~/Library/Caches/com.apple.Safari/Cache.db sqlite3 ~/Pictures/Aperture Library.aplibrary/Aperture.aplib
  10. 10. Big SQLite Users • Gears • Android
  11. 11. Big SQLite Users • Virus Information
  12. 12. Big SQLite Users • History • Bookmarks • Downloads
  13. 13. Big SQLite Users • Metadata
  14. 14. Where can SQLite be used? • Replacement for a custom file format • Temporary data storage (Unit Tests) - :memory: • Alternative to enterprise DB for demo • Local node storage • Communication between apps • Embedded Devices
  15. 15. Versions • SQLite - 2.8.17 • PDO - 3.3.7 + 2.8.17 (3.6.2 in PHP 5.3) • SQLite3 - 3.6.2, minimum is 3.3.9
  16. 16. SQLite 3 • 64-bit Rowids • Connections shared between threads • BLOB support • Runtime loadable • Improved concurrency extensions • UTF-16 / UTF-8 • Fulltext searching • User defined collations • ICU support • Named bound • OS Interface parameters
  17. 17. SQLite v2 • Smaller subset of SQL supported • Larger file format • Database format incompatible with v3 • No Unicode support • No reading while writing
  18. 18. Examples • Firefox 3.0 • Profile directory contains places.sqlite • Schema on the next page • iPhone SMS
  19. 19. Firefox Places Schema
  20. 20. PDO
  21. 21. SQLite3
  22. 22. Live Demo (Hopefully)
  23. 23. iPhone SMS Schema /private/var/root/Library/SMS/sms.db ~/Library/Application Support/MobileSync/Backup
  24. 24. Custom Functions • Define SQL Functions in PHP • Implement missing features • Aggregation functions
  25. 25. Function Example
  26. 26. Custom Collations • Sorting in alphabetical ☹ ☺ order a a • ICU also provides this i é functionality u i • Use createCollation to é ó define comparison ó u function
  27. 27. Fulltext Indexing
  28. 28. Fulltext Searching
  29. 29. SQLite3 Extension SQLite3 SQLite3Stmt open paramCount close close exec execute version reset lastInsertRowID clear loadExtension bindValue escapeString bindParam prepare query createFunction createAggregate lastErrorMsg
  30. 30. SQLite3 Extension SQLite3Result fetchArray reset finalize columnType columnName numColumns
  31. 31. Improving Performance • Pragma • cache_size • fullfsync • synchronous • Shared Cache • Use Transactions
  32. 32. Benchmarks • Dual Xeon 3.2GHz, 2GB RAM, Centos 5 • MySQL vs SQLite • Synthetic Benchmark - YMMV Original Image by Ben McLeod
  33. 33. Benchmarks - Tests • 15 tests • INSERT • SELECT • UPDATE • DELETE • DROP
  34. 34. Benchmarks - Insert MySQL 10.0 SQLite 2 SQLite 3 7.5 SQLite 3 (nosync) 5.0 2.5 0 Test 1 Test 2
  35. 35. Benchmarks - Select MySQL 10.0 SQLite 2 SQLite 3 7.5 SQLite 3 (nosync) 5.0 2.5 0 Test 3 Test 4
  36. 36. Benchmarks - Indices MySQL 1.2 SQLite 2 SQLite 3 0.9 SQLite 3 (nosync) 0.6 0.3 0 Test 5 Test 6
  37. 37. Benchmarks - Update MySQL 5.00 SQLite 2 SQLite 3 3.75 SQLite 3 (nosync) 2.50 1.25 0 Test 7 Test 8 Test 9
  38. 38. Benchmarks - Delete MySQL 2.0 SQLite 2 SQLite 3 1.6 SQLite 3 (nosync) 1.2 0.8 0.4 0 Test 11 Test 12 Test 13 Test 14
  39. 39. Benchmarks - Summary • SQLite 3 is faster in almost every test • Using MyISAM would show faster results • Doesn’t test concurrency
  40. 40. Converting from MySQL • varchar, char -> text • smallint, tinyint, int -> integer • auto_increment -> autoincrement • unknown types converted to text • MySQLi functions have SQLite3 equivalents
  41. 41. Unsupported SQL - Examples Original Image by Clive Arundell
  42. 42. Limitations • Very large datasets • High concurrency • No Foreign Keys • Nested Transactions • Writing to Views
  43. 43. Miscellaneous • Integrity Check • Reclaim Unused Space • Backup
  44. 44. Tools - SQLite Manager
  45. 45. Resources • http://www.sqlite.org • http://www.php.net/sqlite3 • http://pecl.php.net/sqlite3 • http://talks.macvicar.net • https://addons.mozilla.org/en-US/firefox/ addon/5817

×