Embedding SQL Engine to Your Application Iwo Panowicz Percona - - PowerPoint PPT Presentation

embedding sql engine to your application
SMART_READER_LITE
LIVE PREVIEW

Embedding SQL Engine to Your Application Iwo Panowicz Percona - - PowerPoint PPT Presentation

Embedding SQL Engine to Your Application Iwo Panowicz Percona Whats an Embedded Database? A library embedded in the application, that implements methods to access and manipulate data. A database running on an embedded computer mostly


slide-1
SLIDE 1

Embedding SQL Engine to Your Application

Iwo Panowicz Percona

slide-2
SLIDE 2

2

What’s an Embedded Database?

  • A library embedded in the application, that implements methods to

access and manipulate data.

  • A database running on an embedded computer mostly adapted for

embedded purposes.

slide-3
SLIDE 3

Libraries embedded in the application

slide-4
SLIDE 4

4

What’s an Embedded Database?

  • … but why not just flat flies/xml/json?

○ creating a database management system is harder than you might

thought

○ saving time, ○ not reinventing the wheel.

  • Mobile applications
  • IoT
  • Microservices
slide-5
SLIDE 5

5

Libraries

  • Usually lightweight.

○ SQLite can be fit under 300KB and be made to run in minimal stack

space (~4KB) and heap space (~100KB).

  • Doesn’t require any connection to a database instance.
  • Widely used in IoT and mobile applications.
  • Low latencies.
  • Usually used with much smaller data sets that with conventional DBMS.
  • Help processing data

○ In-memory databases are often used as temporary databases that

require no permanent storage.

  • DBA not needed.
slide-6
SLIDE 6

6

Sample use case

  • Mobile contacts manager
  • E-book reader database
  • Web browser cookie storage
  • … anything that stores data.

○ like airplanes

slide-7
SLIDE 7

7

MySQL Embedded, libmysqld

  • The embedded MySQL server library makes it possible to run a

full-featured MySQL server inside a client application.

  • The main benefits are increased speed and more simple management for

embedded applications.

  • Written in C/C++ and available only for C/C++.
  • The API is identical for the embedded MySQL version and the

libmysqlclient.

  • Deprecated in 5.7 and fully removed in 8.0.
slide-8
SLIDE 8

8

MySQL Embedded, major restrictions

  • No user-defined functions (UDFs).
  • No networking (handled by MySQL).

○ No Replication.

  • No Event Scheduler.
  • No Performance Schema.
slide-9
SLIDE 9

9

MySQL Embedded, restrictions

... #include "mysql.h" MYSQL *mysql; MYSQL_RES *results; MYSQL_ROW record; ... int main(void) { [1] mysql_library_init(num_elements, server_options, server_groups); [2] mysql = mysql_init(NULL); [3] mysql_options(mysql, MYSQL_READ_DEFAULT_GROUP, "libmysqld_client"); [4] mysql_options(mysql, MYSQL_OPT_USE_EMBEDDED_CONNECTION, NULL);

slide-10
SLIDE 10

1

MySQL Embedded, restrictions

[5 ] mysql_real_connect(mysql, NULL,NULL,NULL, "database1", 0,NULL,0); [6 ] mysql_query(mysql, "SELECT column1, column2 FROM table1"); [7 ] results = mysql_store_result(mysql); while((record = mysql_fetch_row(results))) { printf("%s - %s \n", record[0], record[1]); } [8 ] mysql_free_result(results); [9 ] mysql_close(mysql); [10] mysql_library_end(); return 0; }

slide-11
SLIDE 11

1 1

SQLite

  • Small. Fast. Reliable. Choose any three.
  • Extremely popular embedded database systems. ProxySQL uses it.
  • SQLite author claims that reads and writes small blobs 35% faster than

the same blobs can be read from or written to individual files on disk using fread() or fwrite(). YMMV.

  • SQLite database holding 10-kilobyte blobs uses about 20% less disk

space than storing the blobs in individual files. YMMV.

slide-12
SLIDE 12

1 2

SQLite

  • Zero-Configuration apart for a schema.
  • In-Memory databases
  • Single Database Files
  • SQL statements compile into virtual machine code
  • Implements most of the SQL-92

○ basic support of triggers ○ basic support ALTER TABLE

slide-13
SLIDE 13

1 3

SQLite

slide-14
SLIDE 14

1 4

Interface

  • SQLite interface elements can be grouped into three

categories:

○ List of objects ■ list of all abstract objects/datatypes used

(sqlite3_stmt).

○ List of Constants ■ list of numeric constants (#define SQLITE_OK) ○ List of Functions ■ List of all functions and methods (sqlite3_initialize).

Most applications only use a handful.

slide-15
SLIDE 15

1 5

Tokenizer

  • When a SQL statement is to be evaluated it is first sent to

tokenizer.

  • The tokenizer breaks the SQL text into tokens and hands

those tokens one by one to the parser.

slide-16
SLIDE 16

1 6

Parser

  • The parser assigns meaning to tokens based on their context.

○ Assembles tokens into a parse tree.

  • SQLite uses own solutions, called Lemon.

○ … which does the same job as YACC/BISON.

  • Lemon is thread-safe.
  • Lemon and its grammar file define the SQL language

that SQLite understands.

slide-17
SLIDE 17

1 7

Code Generator

  • Code Generator analyzes the parser tree and generates opcode

that performs the work of the SQL statement.

  • The opcode is not SQLite’s API.

○ Details about the opcode change between releases.

  • Sample opcode:

sqlite> EXPLAIN SELECT 1; addr opcode p1 p2 p3 p4 p5 comment

  • --- ------------- ---- ---- ---- ------------- -- -------------

0 Init 0 1 0 00 Start at 1 1 Integer 1 1 0 00 r[1]=1 2 ResultRow 1 1 0 00 output=r[1] 3 Halt 0 0 0 00

slide-18
SLIDE 18

1 8

Virtual Machine

  • Executes the Code Generator program.
  • Some functions are implemented/inlined as bytecode directly

by the code generator:

○ typeof(), coalesce()

  • Some opcodes that Virtual Machine is executing are

self-altering.

slide-19
SLIDE 19

1 9

B-Tree

  • SQLite stores

○ each table in a separate Binary Tree ○ each index in a separate Binary Tree

  • All Binary Trees are stored in a single file.
  • The file format is stable and is guaranteed to be

compatible moving forward.

slide-20
SLIDE 20

2

Pager

  • Records are stored in fixed-sized pages.

○ Page size can be any power of 2 between 512 and 65536. ○ The default is 4096 (4KB).

  • It is responsible for reading, writing and caching pages.
  • It provides the rollback and atomic commit abstraction
  • It takes care of locking of the database file.
slide-21
SLIDE 21

2 1

OS Interface

  • It is called VFS.
  • VFS is what makes SQLite portable across operating systems

○ provides methods for reading/writing data from a file ○ obtaining randomness, ○ finding current time, etc.

  • Whenever any of the other modules in SQLite needs to

communicate with the operating system, they invoke methods in the VFS.

slide-22
SLIDE 22

2 2

Accessories

  • All helpers, that SQL uses:

○ memory allocation, ○ caseless string comparisons, ○ and even own printf() implementation.

slide-23
SLIDE 23
  • While it works great for many use-cases, it is not recommended for cases

when:

○ Data is separated from the application (client/server). ■ NFS is NOT an option.

  • but if you really need it then check if fnctl() works.
  • Async I/O might be faster for NFS.

○ The dataset is very large or complex. ○ High concurrency is needed.

2 3

SQLite; when not to use

slide-24
SLIDE 24

2 4

SQLite

slide-25
SLIDE 25

2 5

SQLite

slide-26
SLIDE 26

2 6

Berkeley DB

  • Initially released in 1996.
  • Key-value database.
  • Database objects can use various access methods: btree, hash, heap,

queue, recno.

  • Currently, Berkeley DB name is given to three different products:

○ Berkeley DB (C edition) ○ Berkeley DB Java Edition ○ Berkeley DB XML

slide-27
SLIDE 27

2 7

Berkeley DB

  • Each major release cycle has introduced a single new major feature. Most

Notable features:

1.85 Last UCB (University of California, Berkeley) release 2.0 Transactions, recovery 3.0 Queue AM, POSIX threads, subdatabases 4.0 Secondary Indices 4.1 Replication (master-slave) 4.2 Encryption 4.4 Sequence numbers 4.5 MVCC 5.0 Full Text, R-Tries, SQL Api, JDBC

slide-28
SLIDE 28

2 8

Berkeley DB

  • Oracle provides four Berkeley DB products:

○ Berkeley DB Data Store ○ Berkeley DB Concurrent Data Store ○ Berkeley DB Transactional Data Store ○ Berkeley DB High Availability

slide-29
SLIDE 29

2 9

Berkeley DB

  • Berkeley DB Data Store

○ An embeddable, high-performance data store. ○ Supports ■ multiple concurrent threads of control ■ multiple concurrent processes of control ○ Does not support locking. ■ Concurrent writes need to be locked on an application side.

slide-30
SLIDE 30

3

Berkeley DB

  • Berkeley DB Concurrent Data Store

○ Provides built-in concurrency and locking feature. ■ Multiple-reads, single-writer at time. ■ Deadlock-free ○ Application is unaware of which is happening.

slide-31
SLIDE 31

3 1

Berkeley DB

  • Berkeley DB Transactional Data Store

○ Adds support for transactions and database recovery. ■ Commits and rollbacks. ■ High concurrency read/write operations. ○ By default, the serializable isolation level is used (degree 3 isolation). ■ READ-COMMITTED (degree 2 isolation) and

READ-UNCOMMITTED (degree 1 isolation) are also possible.

○ Deadlocks can happen.

slide-32
SLIDE 32

3 2

Berkeley DB

  • Berkeley DB High Availability

○ Providing support for replication. ○ A single master system handles all updates. ○ A distributes these updates to multiple replicas. ○ All replicas can handle read operations. ○ Automatic failover: ■ If the master system fails for any reason, one of the replicas takes

  • ver as the new master system and distributes updates to the

remaining replicas.

slide-33
SLIDE 33

3 3

Berkeley DB

  • While BerkeleyDB is a really advanced system, it is not:

○ a relational database, ○ network, client-server database ■ Berkeley DB Server is a Java application

  • … and has a limited functionality.
slide-34
SLIDE 34

3 4

Berkeley DB

slide-35
SLIDE 35

3 5

Berkeley DB

  • Access Methods

○ General purpose support for creating and accessing

Berkeley DB database files

○ Useful in the absence of transactions ○ Usually for very simple applications only.

slide-36
SLIDE 36

3 6

Berkeley DB

  • Transactions

○ Allows a group of database changes to be treated as an

atomic unit so that either all of the changes are done,

  • r none of the changes are done.

○ This module is useful outside of the Berkeley DB package

for processes that want to transaction-protect their own data modifications.

slide-37
SLIDE 37

3 7

Berkeley DB

  • Lock

○ General purpose lock manager. ○ As Transactions, is useful outside of the Berkeley DB

package for processes that require a portable, fast, configurable lock manager.

slide-38
SLIDE 38

3 8

Berkeley DB

  • Buffer pool

○ Shared memory buffer pool implementation. ○ Cache allows multiple processes and threads

within processes to share access to databases.

○ Buffer pool is page-oriented, and and can be reused

for different purposes.

slide-39
SLIDE 39

3 9

Berkeley DB

  • Logging

○ Write-ahead redo logs. ○ It’s rather unlikely this module can be reused for different

applications.

slide-40
SLIDE 40

4

Berkeley DB

  • SQLite API

○ Drop-in replacement. Almost. ○ There are a few subtleties: ■ BEGIN IMMEDIATE or BEGIN EXCLUSIVE depending on what

you are doing. If you do not, you can end up with deadlocks.

■ SQLITE_BUSY will never happen in BerkeleyDB

  • as there’s no such concept in BerkeleyDB.
slide-41
SLIDE 41

4 1

Berkeley DB

slide-42
SLIDE 42

4 2

Berkeley DB

slide-43
SLIDE 43

4 3

RocksDB

  • Facebook’s fork of Google’s LevelDB.
  • Key-value storage.
  • Optimized to use many CPUs and make efficient use of fast storage, such

as solid-state drives.

  • Based on Log-Structured-Merge-Tree idea.
slide-44
SLIDE 44

4 4

RocksDB

  • The main idea is to minimize amount of random writes.
  • Random writes are slow. Sequential writes are fine.
  • RockDB introduces two major concepts:

○ Memtable ○ SStable

slide-45
SLIDE 45

4 5

RocksDB

  • Memtable:

○ A buffer that can temporarily host the incoming writes in memory. ■ memtable usually are sorted (in RocksDB, the default memtable

implementation is SkipList),

  • but that is not required.
slide-46
SLIDE 46
  • SSTable:

○ when Memtable is full, the data is saved to the storage, ○ All data is saved in Sorted Strings Table. ○ SSTables are immutable.

4 6

RocksDB

slide-47
SLIDE 47
  • SSTable:

○ when MemTable is full, the data is saved to the storage. ■ RocksDB saves data also in redo-logs. ○ All data is saved in Sorted Strings Table. ○ SSTables are immutable.

4 7

RocksDB

slide-48
SLIDE 48
  • SSTable:

○ when MemTable is full, the data is saved to the storage. ■ RocksDB saves data also in redo-logs. ○ All data is saved in Sorted Strings Table. ○ SSTables are immutable. ○ But what if an application constantly updates the same key?

4 8

RocksDB

slide-49
SLIDE 49
  • But what if an application constantly updates the same key?

○ RocksDB will not check if the records exists, but will process like the

key never existed before.

○ When an application will try to get the data RocksDB will search all

SSTables for the most recent version of data.

■ The above statement simplifies the real behaviour of SST, which is

more subtle.

4 9

RocksDB

slide-50
SLIDE 50
  • RocksDB comes with a background thread that compacts SSTable.
  • Compaction is:

○ merging two or more SSTables into one, larger SSTable, ○ removing duplicates and deleted items, ○ compaction is customizable, thus it can be extended for features like

TTL.

5

RocksDB

slide-51
SLIDE 51

5 1

RocksDB

slide-52
SLIDE 52

52

Rate My Session

slide-53
SLIDE 53

5 3

Q&A