Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions docs/spec/db-schema.md
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,7 @@ last_change_date_time: TIMESTAMP NOT NULL
UNIQUE(tenant_id, identifier)
INDEX(tenant_id, cf_document_id, human_coding_scheme) -- for upsert matching
INDEX(cf_document_id, depth) -- for the tree view's level detection (also covers `INDEX(cf_document_id)` alone)
GIN INDEX(subject_uri jsonb_path_ops) -- reverse lookup "items setting this subject": subject_uri @> '[{"identifier": ...}]'
```

### cf_association
Expand Down Expand Up @@ -348,6 +349,7 @@ last_change_date_time: TIMESTAMP NOT NULL
UNIQUE(tenant_id, identifier)
INDEX(tenant_id, cf_document_id, human_coding_scheme) -- upsertマッチング用
INDEX(cf_document_id, depth) -- ツリービューLevel判定用(INDEX(cf_document_id) 単独の用途もカバー)
GIN INDEX(subject_uri jsonb_path_ops) -- 分野逆引き「この分野を設定している項目」: subject_uri @> '[{"identifier": ...}]'
```

### cf_association
Expand Down
35 changes: 35 additions & 0 deletions migrations/versions/d1a2b3c4e5f6_gin_index_cf_items_subject_uri.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
"""add GIN index on cf_items.subject_uri (jsonb_path_ops)

Enables fast reverse lookup "which items set this subject" via the JSONB
containment query ``subject_uri @> '[{"identifier": "<subject-id>"}]'``.
``jsonb_path_ops`` is the smaller / faster GIN operator class for ``@>``.

Revision ID: d1a2b3c4e5f6
Revises: c704faa62b4f
Create Date: 2026-06-20 00:00:00.000000

"""
from typing import Sequence, Union

from alembic import op

# revision identifiers, used by Alembic.
revision: str = "d1a2b3c4e5f6"
down_revision: Union[str, None] = "c704faa62b4f"
branch_labels: Union[str, Sequence[str], None] = None
depends_on: Union[str, Sequence[str], None] = None


def upgrade() -> None:
op.create_index(
"ix_cf_items_subject_uri_gin",
"cf_items",
["subject_uri"],
unique=False,
postgresql_using="gin",
postgresql_ops={"subject_uri": "jsonb_path_ops"},
)


def downgrade() -> None:
op.drop_index("ix_cf_items_subject_uri_gin", table_name="cf_items")
7 changes: 7 additions & 0 deletions src/models/cf_item.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,13 @@ class CFItem(Base):
UniqueConstraint("tenant_id", "identifier", name="uq_cf_items_tenant_identifier"),
Index("ix_cf_items_tenant_document_coding", "tenant_id", "cf_document_id", "human_coding_scheme"),
Index("ix_cf_items_document_depth", "cf_document_id", "depth"),
# Reverse lookup "items setting this subject": subject_uri @> '[{"identifier": ...}]'.
Index(
"ix_cf_items_subject_uri_gin",
"subject_uri",
postgresql_using="gin",
postgresql_ops={"subject_uri": "jsonb_path_ops"},
),
)

id: Mapped[uuid.UUID] = mapped_column(UUID(as_uuid=True), primary_key=True, default=uuid.uuid4)
Expand Down
Loading