From f0f3d70181aebf865415a46aa3967857f91c510a Mon Sep 17 00:00:00 2001 From: David Whittington Date: Mon, 23 Sep 2024 16:38:22 -0500 Subject: [PATCH] perf(sqlite data): switch OR to a UNION in data attributes query PE-6806 The OR was preventing effective index usage. --- src/database/sql/data/content-attributes.sql | 36 +++++++++++++++----- 1 file changed, 27 insertions(+), 9 deletions(-) diff --git a/src/database/sql/data/content-attributes.sql b/src/database/sql/data/content-attributes.sql index 41b623a0..272ae7dd 100644 --- a/src/database/sql/data/content-attributes.sql +++ b/src/database/sql/data/content-attributes.sql @@ -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