C.W.K.
Stream
Lesson 08 of 10 · published

Connection Pragma + 설정

~12 min · python, pragma, configuration

Level 0Scout
0 XP0/80 lessons0/10 achievements
0/120 XP to next level120 XP to go0% complete

외울 만한 'production-ready opener'

SQLite 쓰는 대부분 production Python 앱이 매번 고정 PRAGMA list 설정하는 작은 connection-opening helper 갖게 됨. List + 이유:

  • journal_mode = WAL — 동시 reader/writer (track 4).
  • synchronous = NORMAL — WAL 와 안전, FULL 보다 빠름.
  • foreign_keys = ON — FK 실제 enforce (track 4).
  • busy_timeout = 5000 — 일시 락에 실패 X, 잠깐 wait.
  • temp_store = MEMORY — temp B-tree 디스크 X RAM.
  • cache_size = -64000 — read-heavy 워크로드 64 MB page cache.
  • mmap_size = 134217728 — 큰 파일 빠른 read 위 128 MB mmap I/O.
Self-reference: 피파의 backend/store/conversations.py 가 정확히 이 set (project-tuned cache + mmap size) 으로 모든 connection open. 100k row JSONL+SQLite history 에서 WebUI 가 즉시 느낌인 이유 = 이 7 설정.

Code

모든 프로젝트에 복붙할 opener·python
import sqlite3, atexit

def open_db(path: str) -> sqlite3.Connection:
    conn = sqlite3.connect(path, timeout=30.0, isolation_level=None)
    conn.row_factory = sqlite3.Row
    for pragma in (
        'PRAGMA journal_mode = WAL',
        'PRAGMA synchronous = NORMAL',
        'PRAGMA foreign_keys = ON',
        'PRAGMA busy_timeout = 5000',
        'PRAGMA temp_store = MEMORY',
        'PRAGMA cache_size = -64000',     # 64 MB
        'PRAGMA mmap_size = 134217728',   # 128 MB
    ):
        conn.execute(pragma)
    atexit.register(lambda: (conn.execute('PRAGMA optimize'), conn.close()))
    return conn

External links

Exercise

기본 설정으로 sqlite3 쓰는 본인 Python 스크립트 골라. Connection opener 를 위 production-ready 로 교체. 갖고 있는 벤치마크 재실행. 어느 PRAGMA 가 본인 워크로드에 가장 큰 win 줬는지 기록.

Progress

Progress is local-only — sign in to sync across devices.
이 페이지에서 버그를 발견하셨거나 피드백이 있으세요?문제 신고

댓글 0

🔔 답글 알림 (로그인 필요)
로그인댓글을 남기려면 로그인해 주세요.

아직 댓글이 없어요. 첫 댓글을 남겨보세요.