Skip to content

Commit 8499cbd

Browse files
authored
Optimize top frecent query (#6969)
* Optimize top frecent query * add index to get perf speedup
1 parent e8792ef commit 8499cbd

File tree

5 files changed

+269
-32
lines changed

5 files changed

+269
-32
lines changed

components/places/sql/create_shared_schema.sql

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,12 @@ CREATE INDEX IF NOT EXISTS idx_places_outgoing_by_frecency
4949
ON moz_places(frecency DESC)
5050
WHERE hidden = 0 AND (sync_change_counter > 0 OR sync_status != 2); -- 2 is SyncStatus::Normal
5151

52+
-- partial index for get_top_frecent_site_infos
53+
CREATE INDEX IF NOT EXISTS top_frecent_cover_idx
54+
ON moz_places(frecency DESC, id DESC)
55+
WHERE hidden = 0
56+
AND (last_visit_date_local + last_visit_date_remote) != 0
57+
AND (url GLOB 'http:*' OR url GLOB 'https:*');
5258

5359
CREATE TABLE IF NOT EXISTS moz_places_tombstones (
5460
guid TEXT PRIMARY KEY
@@ -84,6 +90,9 @@ CREATE INDEX IF NOT EXISTS fromindex ON moz_historyvisits(from_visit);
8490
CREATE INDEX IF NOT EXISTS dateindex ON moz_historyvisits(visit_date);
8591
CREATE INDEX IF NOT EXISTS islocalindex ON moz_historyvisits(is_local);
8692

93+
-- Speeds up queries frecency queries, specifically get_top_frecent_site_infos
94+
CREATE INDEX IF NOT EXISTS idx_visits_place_type ON moz_historyvisits(place_id, visit_type);
95+
8796
-- Greatly helps the multi-join query in frecency.
8897
CREATE INDEX IF NOT EXISTS visits_from_type_idx ON moz_historyvisits(from_visit, visit_type);
8998

components/places/src/db/schema.rs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ use sql_support::ConnExt;
1414

1515
use super::db::{Pragma, PragmaGuard};
1616

17-
pub const VERSION: u32 = 19;
17+
pub const VERSION: u32 = 20;
1818

1919
// Shared schema and temp tables for the read-write and Sync connections.
2020
const CREATE_SHARED_SCHEMA_SQL: &str = include_str!("../../sql/create_shared_schema.sql");
@@ -333,6 +333,14 @@ pub fn upgrade_from(db: &Connection, from: u32) -> rusqlite::Result<()> {
333333
// Manually call analyze so the planner can start using the indexes immediately
334334
db.execute("ANALYZE moz_places", [])?;
335335
}
336+
19 => {
337+
// Create the new indexes by just calling the shared schema file
338+
// top_frecent_cover_idx, idx_visits_place_type
339+
db.execute_batch(CREATE_SHARED_SCHEMA_SQL)?;
340+
// Manually call analyze so the planner can start using the indexes immediately
341+
db.execute("ANALYZE moz_places", [])?;
342+
db.execute("ANALYZE moz_historyvisits", [])?;
343+
}
336344
// Add more migrations here...
337345

338346
// Any other from value indicates that something very wrong happened

components/places/src/storage/history.rs

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1357,17 +1357,18 @@ pub fn get_top_frecent_site_infos(
13571357
let infos = db.query_rows_and_then_cached(
13581358
"SELECT h.frecency, h.title, h.url
13591359
FROM moz_places h
1360-
WHERE EXISTS (
1361-
SELECT v.visit_type
1360+
WHERE h.hidden = 0
1361+
AND (h.last_visit_date_local + h.last_visit_date_remote) != 0
1362+
AND (h.url LIKE 'http:%' OR h.url LIKE 'https:%')
1363+
AND h.frecency >= :frecency_threshold
1364+
AND EXISTS (
1365+
SELECT 1
13621366
FROM moz_historyvisits v
1363-
WHERE h.id = v.place_id
1364-
AND (SUBSTR(h.url, 1, 6) == 'https:' OR SUBSTR(h.url, 1, 5) == 'http:')
1365-
AND (h.last_visit_date_local + h.last_visit_date_remote) != 0
1366-
AND ((1 << v.visit_type) & :allowed_types) != 0
1367-
AND h.frecency >= :frecency_threshold AND
1368-
NOT h.hidden
1367+
WHERE v.place_id = h.id
1368+
AND ((1 << v.visit_type) & :allowed_types) != 0
1369+
LIMIT 1
13691370
)
1370-
ORDER BY h.frecency DESC
1371+
ORDER BY h.frecency DESC, h.id DESC
13711372
LIMIT :limit",
13721373
rusqlite::named_params! {
13731374
":limit": num_items,

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

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,17 @@ mod database;
88
mod matching;
99

1010
use criterion::{criterion_group, criterion_main};
11-
use database::{bench_match_url, bench_outgoing_candidates, bench_search_frecent};
11+
use database::{
12+
bench_match_url, bench_outgoing_candidates, bench_search_frecent, bench_top_frecent,
13+
};
1214
use matching::bench_match_anywhere;
1315

1416
criterion_group!(
1517
bench_db,
1618
bench_search_frecent,
1719
bench_match_url,
1820
bench_outgoing_candidates,
21+
bench_top_frecent
1922
);
2023
criterion_group!(bench_mem, bench_match_anywhere);
2124
criterion_main!(bench_db, bench_mem);

0 commit comments

Comments
 (0)