# Database Reference This project keeps local SQLite metadata databases for Danbooru, Gelbooru, and a deduplicated quality union. ## Databases | DB | Purpose | |----|---------| | `quality_80_union.db` | Main query DB: Gelbooru 80 selection union Danbooru 80 selection, deduped by md5 | | `gelbooru.db` | Normalized Gelbooru source metadata | | `danbooru2025_candidate.db` | Danbooru source metadata with md5 | | `danbooru.db` | Older Danbooru DB retained for compatibility | SQLite `-shm` and `-wal` files are runtime sidecars. They are not part of the durable data model. ## quality_80_union.db ### images 2,988,704 rows. Primary key: `md5`. | Column | Type | Meaning | |--------|------|---------| | `md5` | TEXT PK | File hash used for deduplication | | `canonical_rating` | TEXT | Stricter merged rating | | `canonical_file_url` | TEXT | Unified original file URL | | `canonical_preview_url` | TEXT | Unified preview URL, mostly Gelbooru | | `canonical_sample_url` | TEXT | Unified sample URL, mostly Gelbooru | | `canonical_source` | TEXT | Unified source URL | | `canonical_width` | INTEGER | Unified image width | | `canonical_height` | INTEGER | Unified image height | | `present_in_gelbooru` | INTEGER | 1 if this md5 exists in Gelbooru | | `present_in_danbooru` | INTEGER | 1 if this md5 exists in Danbooru | | `selected_by_gelbooru80` | INTEGER | 1 if selected by Gelbooru 80th-threshold rule | | `selected_by_danbooru80` | INTEGER | 1 if selected by Danbooru 80th-threshold rule | | `quality_score` | REAL | `max(gelbooru_score_pct, danbooru_score_pct)`, range `0..1` | | `quality_tier` | TEXT | Current tier label, `p80` | | `gelbooru_id` | INTEGER | Gelbooru post ID, nullable | | `gelbooru_rating` | TEXT | Gelbooru rating, nullable | | `gelbooru_score` | INTEGER | Gelbooru raw score, nullable | | `gelbooru_score_pct` | REAL | Gelbooru rating-bucket percentile, nullable | | `gelbooru_file_url` | TEXT | Gelbooru original file URL, nullable | | `gelbooru_preview_url` | TEXT | Gelbooru preview URL, nullable | | `gelbooru_sample_url` | TEXT | Gelbooru sample URL, nullable | | `gelbooru_source` | TEXT | Gelbooru source URL/text, nullable | | `gelbooru_tags_text` | TEXT | Gelbooru raw tag string, nullable | | `gelbooru_status` | TEXT | Gelbooru post status, nullable | | `danbooru_id` | INTEGER | Danbooru post ID, nullable | | `danbooru_rating` | TEXT | Danbooru rating mapped to full names, nullable | | `danbooru_score` | INTEGER | Danbooru raw score, nullable | | `danbooru_score_pct` | REAL | Danbooru rating-bucket percentile, nullable | | `danbooru_file_url` | TEXT | Danbooru original file URL, nullable | | `danbooru_source` | TEXT | Danbooru source URL/text, nullable | | `danbooru_pixiv_id` | INTEGER | Pixiv artwork ID from Danbooru, nullable | | `danbooru_fav_count` | INTEGER | Danbooru favorite count, nullable | | `danbooru_file_size` | INTEGER | Danbooru file size in bytes, nullable | | `danbooru_file_ext` | TEXT | Danbooru file extension, nullable | | `image_parquet_file` | TEXT | Future local image parquet shard filename, nullable | Source-specific fields are nullable because the DB is a union. Use `present_in_gelbooru` and `present_in_danbooru` to interpret which side fields are expected to exist. `image_parquet_file` is intentionally nullable until image parquet shards are written by a later storage pipeline. ### URL Fields | Field | Non-empty rows | |-------|---------------:| | `canonical_file_url` | 2,988,704 | | `canonical_preview_url` | 2,696,467 | | `canonical_sample_url` | 1,667,457 | | `canonical_source` | 2,766,129 | | `gelbooru_file_url` | 2,696,467 | | `gelbooru_preview_url` | 2,696,467 | | `gelbooru_sample_url` | 1,667,457 | | `gelbooru_source` | 2,451,875 | | `danbooru_file_url` | 2,428,840 | | `danbooru_source` | 2,386,988 | Use `canonical_file_url` for a single original image URL. Use source-specific URL fields when the caller needs provenance. ### Tags | Table | Meaning | |-------|---------| | `gelbooru_image_tags(md5, tag_name)` | Gelbooru tag associations | | `danbooru_image_tags(md5, tag_name, tag_type)` | Danbooru tag associations | ### Quality Score `quality_score` uses `rating_bucket_cume_dist_v1`. ```text pct(score, rating) = count(score <= current_score within rating) / count(rating) quality_score = max(gelbooru_score_pct, danbooru_score_pct) ``` Current verified counts: | Metric | Count | |--------|------:| | Total images | 2,988,704 | | `quality_score >= 0.8` | 2,973,255 | | `quality_score < 0.8` | 15,449 | | `quality_score = 1.0` | 9 | | Selected by Gelbooru 80 | 2,034,916 | | Selected by Danbooru 80 | 1,850,329 | | Selected by both | 896,541 | | Selected by neither | 0 | | Present in both sources | 2,136,603 | The DB is still the union of both raw 80th-threshold selections. The `<0.8` rows are boundary rows caused by discrete raw-score buckets after true percentile repair. ## gelbooru.db Verified counts: | Metric | Count | |--------|------:| | posts | 9,956,288 | | tags | 931,306 | | post_tags | 321,358,388 | Main schema: ```text posts(id, created_at, score, width, height, md5, directory, image, rating, source, change, owner, creator_id, parent_id, sample, preview_height, preview_width, tags, title, has_notes, has_comments, file_url, preview_url, sample_url, sample_height, sample_width, status, post_locked, has_children, attr_limit, attr_offset, attr_count) tags(id, name) post_tags(post_id, tag_id) ``` ## danbooru2025_candidate.db Verified counts: | Metric | Count | |--------|------:| | posts | 9,113,284 | | tags | 859,440 | | post_tags | 330,953,940 | Main schema: ```text posts(id, pixiv_id, source, rating, score, fav_count, image_width, image_height, file_size, file_ext, md5, file_url, created_at, updated_at, uploader_id, parent_id) tags(id, name, type) post_tags(post_id, tag_id) ``` Danbooru raw ratings are `g`, `s`, `q`, `e`; the union DB maps them to `general`, `sensitive`, `questionable`, `explicit`. ## Rebuild and Repair ```bash /Users/charslee/miniconda3/bin/python3 convert_gelbooru.py --overwrite /Users/charslee/miniconda3/bin/python3 convert_danbooru.py \ --output-db /Users/charslee/Repo/private/pixiv_spider/danbooru2025_candidate.db /Users/charslee/miniconda3/bin/python3 build_quality_union_db.py --overwrite /Users/charslee/miniconda3/bin/python3 repair_quality_percentiles.py ``` ## Known Technical Debt - `quality_80_union.db` keeps staging tables: `selected_md5`, `selected_gelbooru_posts`, `selected_danbooru_posts`. - `quality_80_union.db` keeps percentile lookup tables: `gel_score_percentiles`, `dan_score_percentiles`. - Do not vacuum or rebuild the 26 GB union DB by default on low free disk space. A safe cleanup needs enough temporary room for SQLite to rewrite the database.