mirror of
https://github.com/simonw/datasette.git
synced 2026-05-30 05:37:01 +02:00
Add short-circuit optimization for correlated EXISTS subqueries
Apply optimization technique from emschwartz.me to avoid per-row evaluation of correlated EXISTS subqueries when the restriction_list CTE is empty (e.g., due to INTERSECT of conflicting restrictions). The optimization adds an uncorrelated EXISTS check before the correlated one. SQLite evaluates the uncorrelated check once - if it returns false (empty restriction_list), the AND short-circuits and skips the per-row correlated EXISTS evaluation. Files modified: - datasette/utils/permissions.py: resolve_permissions_from_catalog() - datasette/utils/actions_sql.py: _build_single_action_sql()
This commit is contained in:
parent
232a404743
commit
e57ae03e22
2 changed files with 11 additions and 1 deletions
|
|
@ -405,8 +405,13 @@ async def _build_single_action_sql(
|
|||
|
||||
# Add restriction filter if there are restrictions
|
||||
if restriction_sqls:
|
||||
# Short-circuit optimization: if restriction_list is empty (e.g., due to
|
||||
# INTERSECT of conflicting restrictions), the uncorrelated EXISTS check
|
||||
# returns false once, avoiding per-row evaluation of the correlated subquery.
|
||||
# See: https://emschwartz.me/short-circuiting-correlated-subqueries-in-sqlite/
|
||||
query_parts.append(
|
||||
"""
|
||||
AND EXISTS (SELECT 1 FROM restriction_list)
|
||||
AND EXISTS (
|
||||
SELECT 1 FROM restriction_list r
|
||||
WHERE (r.parent = decisions.parent OR r.parent IS NULL)
|
||||
|
|
|
|||
|
|
@ -324,7 +324,12 @@ async def resolve_permissions_from_catalog(
|
|||
filtered AS (
|
||||
SELECT p.parent, p.child
|
||||
FROM permitted p
|
||||
WHERE EXISTS (
|
||||
-- Short-circuit optimization: if restriction_list is empty (e.g., due to
|
||||
-- INTERSECT of conflicting restrictions), the uncorrelated EXISTS check
|
||||
-- returns false once, avoiding per-row evaluation of the correlated subquery.
|
||||
-- See: https://emschwartz.me/short-circuiting-correlated-subqueries-in-sqlite/
|
||||
WHERE EXISTS (SELECT 1 FROM restriction_list)
|
||||
AND EXISTS (
|
||||
SELECT 1 FROM restriction_list r
|
||||
WHERE (r.parent = p.parent OR r.parent IS NULL)
|
||||
AND (r.child = p.child OR r.child IS NULL)
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue