sqlite - mysangle/twentyhours GitHub Wiki
SQLITE
Database ํ์ผ ์์ฑ
- sqlite_schema ํ
์ด๋ธ ์์ฑ
- table๊ณผ index ์ ๋ณด ์ ์ฅ
- type, name, tbl_name, rootpage, sql
- page 1
- 100 byte ํฌ๊ธฐ์ database header ์์ฑ
- page 1์ ๊ฒฝ์ฐ์๋ง ์์ database header๋ฅผ ๊ฐ์ง๊ณ ์์
- database header ๋ค์ page header ์ ์ฅ
- ๋ชจ๋ page๋ ์๋์ ๊ฐ์ ๊ตฌ์กฐ์ header๋ฅผ ๊ฐ๋๋ค.
Description | Value | |
---|---|---|
1 | Page Type | Table Leaf |
2 | First Free Block | 0 |
2 | Cell Count | 0 |
2 | Cell Content Area | 4096 |
1 | Fragment Free Bytes | 0 |
CREATE TABLE users (id INT PRIMARY KEY, username TEXT);
- sqlite_schema ํ ์ด๋ธ์ ๋ค์์ ๋ด์ฉ์ด ์ถ๊ฐ๋จ
type | name | tbl_name | rootpage | sql |
---|---|---|---|---|
table | users | users | 2 | CREATE TABLE users (id INT PRIMARY KEY, username TEXT) |
index | sqlite_autoindex_users_1 | users | 3 |
- page 1์ header ๋ค์ cell pointer์ row ์์ฑ
- [database header][page header][cell pointer 1][cell pointer 2]......[rowid 2 content][rowid 1 content]
- cell pointer 1: rowid 1์ ๊ฐ๋ฆฌํจ๋ค.
- cell pointer 2: rowid 2๋ฅผ ๊ฐ๋ฆฌํจ๋ค.
- rowid 1์ ๋ด์ฉ์ page์ ์ ์ผ ๋์ ์ ์ฅ(Page Offset: 4018)
- rowid 2์ ๋ด์ฉ์ rowid 1์ ๋ด์ฉ ์์ ์ ์ฅ(Page Offset: 3975)
Description | Value | |
---|---|---|
1 | Payload Size | 76 |
1 | Rowid | 1 |
76 | Payload | <76 bytes of payload> |
Description | Value | |
---|---|---|
1 | Payload Size | 41 |
1 | Rowid | 2 |
41 | Payload | <41 bytes of payload> |
- pageload์ ๊ตฌ์กฐ
- [header size][column์ serial type์ ์์๋๋ก ํ์][column์ ๋ด์ฉ]
- header size: column์ ๋ด์ฉ ์๊น์ง์ size
- text์ serial type: 13 + size * 2
- int์ serial type: ๊ฐ์ด 0๊ณผ 1์ธ ๊ฒฝ์ฐ๋ column์ ๋ด์ฉ์ ๋ฃ์ด์ฃผ์ง ์๋๋ค.
- ๊ฐ์ด 0์ธ ๊ฒฝ์ฐ: serial type์ 8์ด ๋๋ค.
- ๊ฐ์ด 1์ธ ๊ฒฝ์ฐ: serial type์ 9๊ฐ ๋๋ค.
- page 2์ users ํ ์ด๋ธ์ root page ์์ฑ
- page 3์ users ํ ์ด๋ธ์ index์ธ sqlite_autoindex_users_1์ root page ์์ฑ
INSERT INTO users VALUES (1, 'alice');
- page 2์ ๋ด์ฉ ์ถ๊ฐ
- page header ๋ค์ cell pointer 1 ์ถ๊ฐ
- page์ ์ ์ผ ๋ค์ rowid 1 ์ถ๊ฐ
- rowid 1์ ๋ด์ฉ
- [08 01 03 09 17 61 6c 69 63 65]
- Pageload Size: 08
- Rowid: 01
- Payload
- [09]: int์ด๊ณ ๊ฐ์ด 1์์ ์๋ฏธ
- [17]: text์ด๊ณ ํฌ๊ธฐ๊ฐ 5์์ ์๋ฏธ(0x17 == 13 + 5 * 2)
- [61 6c 69 63 65]: 'alice'์ ascii ๊ฐ
- page 3์ index ๊ด๋ จ ๋ด์ฉ ์ถ๊ฐ
- page header ๋ค์ cell pointer 1 ์ถ๊ฐ
- page์ ์ ์ผ ๋ค์ Index Leaf Cell ์ถ๊ฐ
- [03 03 09 09]
- id๊ฐ 1์ด๊ณ rowid๊ฐ 1์ธ cell์ ์ ์ฅํ๊ณ ์์์ ์๋ฏธํ๋ค.
Description | Value | |
---|---|---|
1 | Payload Size | 3 |
3 | Payload | <3 bytes of payload> |
- id๊ฐ 1์ธ row๋ฅผ ์ฐพ๋ ๊ณผ์ (select * where id = 1)
- sqlite_autoindex_users_1์์ id๊ฐ 1์ธ cell์ ์ฐพ๋๋ค => ๊ฐ์ด 1
- users ํ ์ด๋ธ์์ rowid๊ฐ 1์ธ row๋ฅผ ์ฐพ๋๋ค.
Page 2์ row๊ฐ ๋ค ์ฐจ๋ ๊ฒฝ์ฐ
-
page 2๋ Table Interior๊ฐ ๋๊ณ , ๊ธฐ์กด์ page 2์ ์๋ ๋ด์ฉ์ด page 4์ 5์ ๋๋์ด์ง๋ค.
-
page 2์ ๋ด์ฉ
- [page header][cell pointer]...[Table Interior Cell]
- page header์ ๋ด์ฉ
- cell pointer๋ฅผ ํตํ Table Interior Cell์ ๋ด์ฉ์ ํตํด ์ด๋ค page๋ฅผ ์ฐพ์์ผ ํ๋์ง ํ๋จํ๋ค.
- ๊ฐ์ฅ ์ค๋ฅธ์ชฝ page์ ๋ด์ฉ์ page header์ ์ ์ฅ๋์ด ์๋ค.
- ์: rowid๊ฐ 58๋ณด๋ค ๊ฐ๊ฑฐ๋ ์์ผ๋ฉด page 4๋ก ๊ฐ๊ณ , 58๋ณด๋ค ํฌ๋ฉด page 5๋ก ๊ฐ๋ค.
- ํ์ฌ 1๊ฐ๋ง ์๋ Table Interior Cell์ ๋ด์ฉ์ ๋ค์๊ณผ ๊ฐ๋ค.
- For rowid < 58 goes to page 4
- [00 00 00 04 3a]
-
page 2์ page header
Description | Value | |
---|---|---|
1 | Page Type | Table Interior |
2 | First Free Block | 49423 |
2 | Cell Count | 1 |
2 | Cell Content Area | 4091 |
1 | Fragment Free Bytes | 109 |
4 | Right Child Page Number | 5 |
- page 2์ Table Interior Cell์ ๋ด์ฉ
Description | Value | |
---|---|---|
4 | Page Number | 4 |
1 | Rowid | 58 |
- page 4์๋ rowid 1๋ถํฐ 58๊น์ง์ row๊ฐ ๋ค์ด๊ฐ๋ค.
- page 5์๋ rowid๊ฐ 58๋ณด๋ค ํฐ row๊ฐ ๋ค์ด๊ฐ๋ค.