Skip to content

Commit 7541108

Browse files
committed
add CTE with fallback for rows with null origin_id
1 parent b509b1a commit 7541108

File tree

2 files changed

+73
-24
lines changed

2 files changed

+73
-24
lines changed

components/places/src/storage/history.rs

Lines changed: 35 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1355,18 +1355,43 @@ pub fn get_top_frecent_site_infos(
13551355
.complement();
13561356

13571357
let infos = db.query_rows_and_then_cached(
1358-
"SELECT h.frecency, h.title, h.url
1359-
FROM moz_places h
1360-
JOIN moz_origins o ON o.id = h.origin_id
1361-
WHERE o.prefix IN ('http', 'https')
1358+
"
1359+
WITH fast AS (
1360+
SELECT h.frecency, h.title, h.url
1361+
FROM moz_places h
1362+
JOIN moz_origins o ON o.id = h.origin_id
1363+
WHERE o.prefix IN ('http', 'https')
1364+
AND h.hidden = 0
1365+
AND h.frecency >= :frecency_threshold
1366+
AND EXISTS (
1367+
SELECT 1 FROM moz_historyvisits v
1368+
WHERE v.place_id = h.id
1369+
AND ((1 << v.visit_type) & :allowed_types) != 0
1370+
LIMIT 1
1371+
)
1372+
ORDER BY h.frecency DESC
1373+
LIMIT :limit
1374+
),
1375+
fallback AS (
1376+
SELECT h.frecency, h.title, h.url
1377+
FROM moz_places h
1378+
WHERE h.origin_id IS NULL
13621379
AND h.hidden = 0
13631380
AND h.frecency >= :frecency_threshold
1364-
AND (h.last_visit_date_local + h.last_visit_date_remote) != 0
1365-
AND EXISTS (SELECT 1 FROM moz_historyvisits v
1366-
WHERE v.place_id = h.id
1367-
AND ((1 << v.visit_type) & :allowed_types) != 0
1368-
LIMIT 1)
1369-
ORDER BY h.frecency DESC
1381+
AND (h.url LIKE 'https:%' OR h.url LIKE 'http:%')
1382+
AND EXISTS (
1383+
SELECT 1 FROM moz_historyvisits v
1384+
WHERE v.place_id = h.id
1385+
AND ((1 << v.visit_type) & :allowed_types) != 0
1386+
LIMIT 1
1387+
)
1388+
ORDER BY h.frecency DESC
1389+
LIMIT :limit
1390+
)
1391+
SELECT * FROM fast
1392+
UNION ALL
1393+
SELECT * FROM fallback
1394+
ORDER BY frecency DESC
13701395
LIMIT :limit",
13711396
rusqlite::named_params! {
13721397
":limit": num_items,

testing/separated/places-bench/src/database.rs

Lines changed: 38 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -333,20 +333,44 @@ const TOP_FRECENT_ORIGINAL: &str = r#"
333333
"#;
334334

335335
const TOP_FRECENT_INDEX_FRIENDLY: &str = r#"
336-
SELECT h.frecency, h.title, h.url
337-
FROM moz_places h
338-
JOIN moz_origins o ON o.id = h.origin_id
339-
WHERE o.prefix IN ('http', 'https')
340-
AND h.hidden = 0
341-
AND h.frecency >= {FRECENCY}
342-
AND (h.last_visit_date_local + h.last_visit_date_remote) != 0
343-
AND EXISTS (
344-
SELECT 1 FROM moz_historyvisits v
345-
WHERE v.place_id = h.id
346-
AND ((1 << v.visit_type) & {ALLOWED_TYPES}) != 0
347-
LIMIT 1
348-
)
349-
ORDER BY h.frecency DESC
336+
/* Fast path: rows where origin_id is set, quick join on moz_origins */
337+
WITH fast AS (
338+
SELECT h.frecency, h.title, h.url
339+
FROM moz_places h
340+
JOIN moz_origins o ON o.id = h.origin_id
341+
WHERE o.prefix IN ('http', 'https')
342+
AND h.hidden = 0
343+
AND h.frecency >= {FRECENCY}
344+
AND EXISTS (
345+
SELECT 1 FROM moz_historyvisits v
346+
WHERE v.place_id = h.id
347+
AND ((1 << v.visit_type) & {ALLOWED_TYPES}) != 0
348+
LIMIT 1
349+
)
350+
ORDER BY h.frecency DESC
351+
LIMIT {LIMIT}
352+
),
353+
/* Fallback: rows not yet assigned an origin_id (yet) */
354+
fallback AS (
355+
SELECT h.frecency, h.title, h.url
356+
FROM moz_places h
357+
WHERE h.origin_id IS NULL
358+
AND h.hidden = 0
359+
AND h.frecency >= {FRECENCY}
360+
AND (h.url LIKE 'https:%' OR h.url LIKE 'http:%')
361+
AND EXISTS (
362+
SELECT 1 FROM moz_historyvisits v
363+
WHERE v.place_id = h.id
364+
AND ((1 << v.visit_type) & {ALLOWED_TYPES}) != 0
365+
LIMIT 1
366+
)
367+
ORDER BY h.frecency DESC
368+
LIMIT {LIMIT}
369+
)
370+
SELECT * FROM fast
371+
UNION ALL
372+
SELECT * FROM fallback
373+
ORDER BY frecency DESC
350374
LIMIT {LIMIT}
351375
"#;
352376

0 commit comments

Comments
 (0)