SlideShare a Scribd company logo
How SQL Database Engines Work
D. Richard Hipp
OpenSQL
2008-11-15
How SQL Database Engines Work
D. Richard Hipp
OpenSQL
2008-11-15
SQLite
with occasional comments on how the
operation of SQLite compares to other
SQL database engines.
Why Do You Care?
So you can better understand what your query is
doing.
So that you can appreciate how much work the
database is saving you.
So that you can fix things when they go wrong.
So that you can write better SQL that runs faster
and uses less memory and disk space.
How sqlite works
Key Concept
SQL is a peculiar programming language
Each SQL statement is a separate program
SQL describes what instead of how
An RDBMS consists of...
Compiler to translate SQL into procedures
Virtual Machine to evaluate the procedures
Example
SELECT * FROM table1;
Translates into:
Open database file containing table1
Rewind the file
while not at end-of-file
read all columns out of current record
return the columns to the caller
advance file to the next record
end-while
close the file
Imagine what this translates into:
SELECT eqptid, enclosureid
FROM eqpt
WHERE typeid IN (
SELECT typeid FROM typespec
WHERE attrid=(
SELECT attrid FROM attribute
WHERE name='detect_autoactuate'
)
AND value=1
INTERSECT
SELECT typeid FROM typespec
WHERE attrid=(
SELECT attrid FROM attribute
WHERE name='algorithm'
)
AND value IN ('sensor','wetbulb')
)
The Whole Point Of SQL...
A few lines of SQL generates the equivalent of
hundreds of lines of procedural code.
By adding an index, entirely new procedures are
used without recoding.
The SQL Query Optimizer is charged with picking
the algorithm
so that the application developer doesn't have to
Ins and Outs of SQL
Compile SQL
into a program
Run the
programSQL Prep'ed
Stmt
Result
Front Half Back Half
Ins and Outs of SQL
Compile SQL
into a program
Run the
programSQL Prep'ed
Stmt
Result
Byte code
Tree of structures
Native machine code
See Aho & Ullman
The “Dragon Book”
Virtual Machine
Architecture Of SQLite
Virtual Machine
B-Tree
Pager
OS Interface
Parser
Tokenizer
Code Generator
Architecture Of SQLite
Parser
Tokenizer
Code Generator
Virtual Machine
B-Tree
Pager
OS Interface
Virtual Machine
B-Tree
Pager
OS Interface
Compile SQL
into a program
Run the
programSQL
Prep'ed
Stmt
Result
Parser
Tokenizer
Code Generator
Virtual Machine
B-Tree
Pager
OS Interface
Compile SQL
into a program
Run the
programSQL
Prep'ed
Stmt
Result
Parser
Tokenizer
Code Generator
Architecture Of SQLite
Parser
Tokenizer
Code Generator
Virtual Machine
B-Tree
Pager
OS Interface
Byte code interpreter
Big switch statement
inside a for loop.
Other engines walk a
tree of structures
Similar to JVM or Parrot
or P-Code
Architecture Of SQLite
Parser
Tokenizer
Code Generator
Virtual Machine
B-Tree
Pager
OS Interface
Ordered key/value pairs
with unique keys
O(logN) insert, seek,
and delete
O(1) next and previous
Architecture Of SQLite
Parser
Tokenizer
Code Generator
Virtual Machine
B-Tree
Pager
OS Interface
Architecture Of SQLite
Parser
Tokenizer
Code Generator
Virtual Machine
B-Tree
Pager
OS Interface
Atomic commit and
rollback
Uniform size pages
numbered from 1
No interpretation of
page content
Cache
Architecture Of SQLite
Parser
Tokenizer
Code Generator
Virtual Machine
B-Tree
Pager
OS Interface
Platform-specific
interface to the OS
Run-time changeable
Portability layer
CREATE TABLE tab(
Fruit TEXT,
GrownIn TEXT,
UnitPrice REAL
);
CREATE TABLE tab(
Fruit TEXT,
GrownIn TEXT,
UnitPrice REAL
);
Key Data
SELECT unitprice FROM tab WHERE fruit='Peach'
SELECT unitprice FROM tab WHERE rowid=93
CREATE INDEX idx1 ON tab(fruit)
idx1 tab
CREATE INDEX idx1 ON tab(fruit)
1:1
CREATE INDEX idx1 ON tab(fruit)
CREATE INDEX idx1 ON tab(fruit)
+
SELECT unitprice FROM tab WHERE fruit='Peach'
SELECT unitprice FROM tab
WHERE fruit='Orange'
AND grownin='CA'
CREATE INDEX idx2 ON tab(grownin)
idx2 tab
SELECT unitprice FROM tab
WHERE fruit='Orange'
AND grownin='CA'
CREATE INDEX idx3 ON tab(fruit, grownin)
idx3 tab
CREATE INDEX idx2 ON tab(fruit, grownin)
CREATE INDEX idx2 ON tab(fruit, grownin)
SELECT unitprice FROM tab
WHERE fruit='Orange'
AND grownin='CA'
SELECT unitprice FROM tab
WHERE fruit='Orange'
AND grownin='CA'
4 ways to do this query
(so far...)
SELECT unitprice FROM tab
WHERE fruit='Orange'
AND grownin='CA' 1
SELECT unitprice FROM tab
WHERE fruit='Orange'
AND grownin='CA' 2
SELECT unitprice FROM tab
WHERE fruit='Orange'
AND grownin='CA' 3
SELECT unitprice FROM tab
WHERE fruit='Orange'
AND grownin='CA' 4
CREATE INDEX idx4 ON tab(fruit, grownin, unitprice)
idx4 tab
SELECT unitprice FROM tab
WHERE fruit='Orange'
AND grownin='CA' 5
SELECT unitprice FROM tab
WHERE fruit='Orange'
SELECT grownin FROM tab
WHERE fruit='Orange'
AND unitprice<1.00
SELECT grownin FROM tab
WHERE fruit='Orange'
AND unitprice<1.00
How sqlite works
SELECT * FROM tab ORDER BY fruit
sorter
SELECT * FROM tab ORDER BY rowid
SELECT * FROM tab ORDER BY rowid DESC
SELECT * FROM tab ORDER BY fruit
SELECT * FROM tab ORDER BY fruit DESC
SELECT * FROM tab ORDER BY fruit
SELECT * FROM tab ORDER BY fruit DESC
SELECT * FROM tab ORDER BY fruit, grownin
SELECT * FROM tab ORDER BY fruit, grownin, unitprice
SELECT * FROM tab ORDER BY fruit DESC, grownin DESC
SELECT * FROM tab
ORDER BY fruit DESC, grownin DESC, unitprice DESC
SELECT * FROM tab ORDER BY fruit DESC, grownin
SELECT * FROM tab ORDER BY fruit, unitprice
SELECT * FROM tab ORDER BY grownin
SELECT grownin, unitprice FROM tab
WHERE fruit='Orange'
ORDER BY grownin
Index Column Order
SELECT x, y, z FROM t1 WHERE w=5 AND x=6 ORDER BY x, y;
CREATE INDEX t1i1 ON t1(w,x,y,z);
Index Column Order
SELECT x, y, z FROM t1 WHERE w=5 AND x=6 ORDER BY x, y;
CREATE INDEX t1i1 ON t1(w,x,y,z);
WHERE clause terms first
Index Column Order
SELECT x, y, z FROM t1 WHERE w=5 AND x=6 ORDER BY x, y;
CREATE INDEX t1i1 ON t1(w,x,y,z);
ORDER BY terms second.
No gaps!
Can overlap WHERE clause.
Index Column Order
SELECT x, y, z FROM t1 WHERE w=5 AND x=6 ORDER BY x, y;
CREATE INDEX t1i1 ON t1(w,x,y,z);
Result set columns can appear
anywhere in the index. Gaps allowed
SELECT unitprice FROM tab
WHERE fruit='Orange'
OR grownin='CA'
Pes ky O R
SELECT unitprice FROM tab
WHERE fruit='Orange'
OR grownin='CA'
SELECT unitprice FROM tab
WHERE fruit='Orange'
OR grownin='CA'
UNION
SELECT unitprice FROM tab
WHERE fruit='Orange'
OR grownin='CA'
UNION
Sorter
SELECT unitprice FROM tab
WHERE fruit='Orange'
OR grownin='CA'
UNION
SELECT unitprice FROM tab
WHERE fruit='Orange'
AND grownin='CA'
INTERSECT
6
SELECT unitprice FROM tab
WHERE fruit='Orange'
AND grownin='CA' 4
SELECT unitprice FROM tab
WHERE fruit='Orange'
AND grownin='CA' 5
SELECT unitprice FROM tab
WHERE (fruit='Orange' AND grownin='CA')
OR grownin='NC'
INTERSECT
UNION
SELECT unitprice FROM tab
WHERE (fruit='Orange' AND grownin='CA')
OR grownin='NC'
UNION
INTERSECT
grownin='NC'
grownin='CA'
fruit='Orange'
AND
OR
Remember This:
SQL is a programming language
There is a compiler
There is a virtual machine
Indices are not magic – they need to make sense
Avoid too many indices
The more options the optimizer has, the more
chances it has to make a poor choice.

More Related Content

PPTX
Data Warehouse and Business Intelligence - Recipe 4 - Staging area - how to v...
PDF
More SQL in MySQL 8.0
PDF
Tables in dd
PPTX
Data Warehouse and Business Intelligence - Recipe 3
PPTX
Big file tablespaces
PPTX
Data Warehouse and Business Intelligence - Recipe 1
PDF
JSON Array Indexes in MySQL
PPTX
Recipes 6 of Data Warehouse and Business Intelligence - Naming convention tec...
Data Warehouse and Business Intelligence - Recipe 4 - Staging area - how to v...
More SQL in MySQL 8.0
Tables in dd
Data Warehouse and Business Intelligence - Recipe 3
Big file tablespaces
Data Warehouse and Business Intelligence - Recipe 1
JSON Array Indexes in MySQL
Recipes 6 of Data Warehouse and Business Intelligence - Naming convention tec...

What's hot (19)

PPT
Recipe 14 of Data Warehouse and Business Intelligence - Build a Staging Area ...
PPTX
Recipe 5 of Data Warehouse and Business Intelligence - The null values manage...
PPTX
Data Warehouse and Business Intelligence - Recipe 7 - A messaging system for ...
PPTX
ata Warehouse and Business Intelligence - Recipe 7 - A messaging system for O...
PPTX
OakTable World 2015 - Using XMLType content with the Oracle In-Memory Column...
PPTX
Starting with JSON Path Expressions in Oracle 12.1.0.2
PPTX
vFabric SQLFire Introduction
PDF
UKOUG Tech14 - Using Database In-Memory Column Store with Complex Datatypes
PDF
Introduction to sq lite
PPTX
Data Warehouse and Business Intelligence - Recipe 2
PDF
Understanding Query Execution
PPTX
MS Sql Server: Customizing Your Data Base Design
PPTX
Oracle Developer Day, 20 October 2009, Oracle De Meern, Holland: Oracle Datab...
DOCX
Multiple files single target single interface
PPTX
cPanel now supports MySQL 8.0 - My Top Seven Features
PDF
Oracle10g External Tables
PPTX
Queries in sq lite
PPTX
MySQL Replication Evolution -- Confoo Montreal 2017
PPTX
Confoo 2021 - MySQL Indexes & Histograms
Recipe 14 of Data Warehouse and Business Intelligence - Build a Staging Area ...
Recipe 5 of Data Warehouse and Business Intelligence - The null values manage...
Data Warehouse and Business Intelligence - Recipe 7 - A messaging system for ...
ata Warehouse and Business Intelligence - Recipe 7 - A messaging system for O...
OakTable World 2015 - Using XMLType content with the Oracle In-Memory Column...
Starting with JSON Path Expressions in Oracle 12.1.0.2
vFabric SQLFire Introduction
UKOUG Tech14 - Using Database In-Memory Column Store with Complex Datatypes
Introduction to sq lite
Data Warehouse and Business Intelligence - Recipe 2
Understanding Query Execution
MS Sql Server: Customizing Your Data Base Design
Oracle Developer Day, 20 October 2009, Oracle De Meern, Holland: Oracle Datab...
Multiple files single target single interface
cPanel now supports MySQL 8.0 - My Top Seven Features
Oracle10g External Tables
Queries in sq lite
MySQL Replication Evolution -- Confoo Montreal 2017
Confoo 2021 - MySQL Indexes & Histograms
Ad

Similar to How sqlite works (20)

PPTX
Sql killedserver
PPTX
My SQL Skills Killed the Server
PPT
15 Ways to Kill Your Mysql Application Performance
PPT
What's New for Developers in SQL Server 2008?
PPT
SQL Server 2008 Overview
PPT
MDI Training DB2 Course
PPT
Mainframe Technology Overview
PDF
[db tech showcase Tokyo 2017] C23: Lessons from SQLite4 by SQLite.org - Richa...
PPT
Introduction to Threading in .Net
PDF
SQL Basics
PDF
Sql basics
PDF
Sql General
PPTX
OOW16 - Oracle Database 12c - The Best Oracle Database 12c New Features for D...
PPTX
OOW16 - Oracle Database 12c - The Best Oracle Database 12c New Features for D...
PPT
ORACLE PL SQL
PPTX
android sqlite
PDF
Introduction to SQLite in Adobe AIR
PPTX
HPD SQL Training - Beginner - 20220916.pptx
PPT
JDBC
PPT
MySQL Presentation
Sql killedserver
My SQL Skills Killed the Server
15 Ways to Kill Your Mysql Application Performance
What's New for Developers in SQL Server 2008?
SQL Server 2008 Overview
MDI Training DB2 Course
Mainframe Technology Overview
[db tech showcase Tokyo 2017] C23: Lessons from SQLite4 by SQLite.org - Richa...
Introduction to Threading in .Net
SQL Basics
Sql basics
Sql General
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 PL SQL
android sqlite
Introduction to SQLite in Adobe AIR
HPD SQL Training - Beginner - 20220916.pptx
JDBC
MySQL Presentation
Ad

Recently uploaded (20)

PDF
Build a system with the filesystem maintained by OSTree @ COSCUP 2025
PDF
Architecting across the Boundaries of two Complex Domains - Healthcare & Tech...
PDF
Per capita expenditure prediction using model stacking based on satellite ima...
PPTX
Programs and apps: productivity, graphics, security and other tools
PDF
Network Security Unit 5.pdf for BCA BBA.
PDF
Blue Purple Modern Animated Computer Science Presentation.pdf.pdf
PPTX
Digital-Transformation-Roadmap-for-Companies.pptx
PDF
cuic standard and advanced reporting.pdf
PDF
Spectral efficient network and resource selection model in 5G networks
PDF
Empathic Computing: Creating Shared Understanding
PDF
Reach Out and Touch Someone: Haptics and Empathic Computing
PDF
Profit Center Accounting in SAP S/4HANA, S4F28 Col11
PDF
Advanced methodologies resolving dimensionality complications for autism neur...
PDF
Machine learning based COVID-19 study performance prediction
PDF
Approach and Philosophy of On baking technology
PPTX
Spectroscopy.pptx food analysis technology
PDF
Agricultural_Statistics_at_a_Glance_2022_0.pdf
DOCX
The AUB Centre for AI in Media Proposal.docx
PDF
7 ChatGPT Prompts to Help You Define Your Ideal Customer Profile.pdf
PDF
TokAI - TikTok AI Agent : The First AI Application That Analyzes 10,000+ Vira...
Build a system with the filesystem maintained by OSTree @ COSCUP 2025
Architecting across the Boundaries of two Complex Domains - Healthcare & Tech...
Per capita expenditure prediction using model stacking based on satellite ima...
Programs and apps: productivity, graphics, security and other tools
Network Security Unit 5.pdf for BCA BBA.
Blue Purple Modern Animated Computer Science Presentation.pdf.pdf
Digital-Transformation-Roadmap-for-Companies.pptx
cuic standard and advanced reporting.pdf
Spectral efficient network and resource selection model in 5G networks
Empathic Computing: Creating Shared Understanding
Reach Out and Touch Someone: Haptics and Empathic Computing
Profit Center Accounting in SAP S/4HANA, S4F28 Col11
Advanced methodologies resolving dimensionality complications for autism neur...
Machine learning based COVID-19 study performance prediction
Approach and Philosophy of On baking technology
Spectroscopy.pptx food analysis technology
Agricultural_Statistics_at_a_Glance_2022_0.pdf
The AUB Centre for AI in Media Proposal.docx
7 ChatGPT Prompts to Help You Define Your Ideal Customer Profile.pdf
TokAI - TikTok AI Agent : The First AI Application That Analyzes 10,000+ Vira...

How sqlite works