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)
    1. sqlite_autoindex_users_1์—์„œ id๊ฐ€ 1์ธ cell์„ ์ฐพ๋Š”๋‹ค => ๊ฐ’์ด 1
    2. 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๊ฐ€ ๋“ค์–ด๊ฐ„๋‹ค.