Skip to content

Commit

Permalink
perf(sqlite data): switch OR to a UNION in data attributes query PE-6806
Browse files Browse the repository at this point in the history
The OR was preventing effective index usage.
  • Loading branch information
djwhitt committed Sep 23, 2024
1 parent 60d421b commit f0f3d70
Showing 1 changed file with 27 additions and 9 deletions.
36 changes: 27 additions & 9 deletions src/database/sql/data/content-attributes.sql
Original file line number Diff line number Diff line change
Expand Up @@ -36,15 +36,33 @@ INSERT OR REPLACE INTO data_roots (
)

-- selectDataAttributes
SELECT
cd.hash,
cd.data_size,
cd.original_source_content_type,
cdi.verified
FROM contiguous_data cd
LEFT JOIN contiguous_data_ids cdi ON cdi.contiguous_data_hash = cd.hash
LEFT JOIN data_roots dr ON dr.contiguous_data_hash = cd.hash
WHERE cdi.id = :id OR dr.data_root = :data_root
SELECT *
FROM (
SELECT
cd.hash,
cd.data_size,
cd.original_source_content_type,
cdi.verified
FROM contiguous_data cd
LEFT JOIN contiguous_data_ids cdi ON cdi.contiguous_data_hash = cd.hash
LEFT JOIN data_roots dr ON dr.contiguous_data_hash = cd.hash
WHERE cdi.id = :id
LIMIT 1
)
UNION
SELECT *
FROM (
SELECT
cd.hash,
cd.data_size,
cd.original_source_content_type,
cdi.verified
FROM contiguous_data cd
LEFT JOIN contiguous_data_ids cdi ON cdi.contiguous_data_hash = cd.hash
LEFT JOIN data_roots dr ON dr.contiguous_data_hash = cd.hash
WHERE dr.data_root = :data_root
LIMIT 1
)
LIMIT 1

-- selectDataParent
Expand Down

0 comments on commit f0f3d70

Please sign in to comment.