Add additional indexes for address bar autocomplete#7484
Conversation
…plete performance First, we replace the index on moz_places(origin_id) with an index on (origin_id, frecency). This improves the speed at which we can do an ORDER BY frecency in the api/matcher.rs URL_SQL query. Next, we add an index on moz_origins(host, frecency). This has two purposes: it improves the performance of the api/matcher.rs ORIGIN_SQL query, but it also has the nice side effect of defending against degenerate behaviour if the moz_origins table stats get poisoned by poor analyze timing.
bench_origin_autocomplete tests the api/matcher.rs ORIGIN_SQL query before and after the introduction of the moz_origins(host, frecency) index. bench_origin_frecency_index tests the api/matcher.rs URL_SQL query before and after the replacement of the moz_places(origin_id) index with an index on (origin_id, frecency).
|
Thanks for this - I'll dig into this some more later this week. |
I should say more I guess :) In particular, we copied the schema from desktop for most of places, so I'd really like to know where desktop is now for this part of the schema. Then we can maybe chat to the desktop experts like @mak77 |
|
It depends on how you query the database, which queries were used to benchmark? For host, one problem is we have an index on (prefix, host) but that index should have been on (host, prefix)... that's https://bugzilla.mozilla.org/show_bug.cgi?id=2025999. |
It doesn't have either of these indexes, but I could open a change there if there's interest
I found these candidates by looking at the queries in the address bar autocomplete code path, and in particular these were meant to target the URL_SQL and ORIGIN_SQL queries.
I agree, this change is not necessarily a clear and obvious win. The main reason for which I was even exploring adding new indexes was to work around the bad query stats degenerate case I describe in bug 2056115, but I think you can make an argument that #7485 is the "real" root cause fix for that. I only kept pursuing this in addition because it seemed to make a decent, but marginal improvement even in the non-degenerate case. As I understand, these run on every keystroke in the address bar, so it could be worth pursuing such micro-optimizations even if small. But I will let you be the judge of that.
Well actually, the motivation in adding frecency to the moz_places index was not to avoid a lookup, but to improve the ORDER BY performance in URL_SQL. It's not reflected as clearly in the benchmarks here, but on my real life places DB i saw a 115x improvement with this index, from 80ms to 0.7ms, even after fixing the bad table stats. But you are right that adding frecency to the moz_origins index on the other hand merely avoids a lookup (although without frecency it's actually a slight net loss perf-wise in my testing -- my main motivation for adding the moz_origins index was to fix the degenerate table stats issue, not to improve general perf).
Such a change would also prevent my degenerative table stats case. So if that's the direction you want to go, I could remove the change to moz_origins here. Although I think the change to moz_places is maybe worth considering independently. I should note that while investigating this, I also considered several other candidates for new indexes in the address bar autocomplete path which I didn't include here because I figured they'd have diminishing returns:
etc. Thanks, Shawn |
|
Your analysis is appreciated. My points were mostly the motivations behind desktop shape. Mobile has slight differences that may also make worth to take more optimizations. We'll keep these measurements into account, but at least for now Desktop won't add compund indices on frecency, we have bigger fishes to fry, to be honest. |
|
Makes sense to me, thank you for the insight. I believe mobile also kills in flight queries as you input additional characters. In light of that thinking I'll close this PR and, if nobody else is taking it, perhaps try and look into opening a change to reverse the columns in that existing index on desktop and mobile |
|
I attempted to put up a change for the index column order reversal on desktop in bug 2025999. If that goes OK, then I can make the equivalent change on mobile as a follow up |
This PR aims to address cause 2 identified in bug https://bugzilla.mozilla.org/show_bug.cgi?id=2056115.
In rare cases where the table stats of the places DB get stuck in a poisoned state, the lack of an index on moz_origins(host) can cause extremely poor performance. One way of mitigating this scenario is by indexing moz_origins(host).
Aside from that, while investigating this bug I also observed that indexes on moz_origins(host, frecency) and moz_places(origin_id, frecency) could help improve the performance of some queries in the address bar autocomplete path even in the absense of the poisoned table stats.
Here you can see the benchmark results on my machine, including two benchmarks which cover the poisoned-stats case (which I didn't include in the changes here since I intend to address the root cause of this in another PR):
Observe how these changes fix the poisoned stats case but also provide a notable improvement in the healthy stats case.
I used Claude to assist with this investigation, and also this is my first contribution to a Mozilla project. So this change may warrant some scruitiny from those more experienced with the codebase. But to the best of my knowledge, it seems to be a positive change which helps in part to address the original issue I faced.
Thanks, Shawn
Pull Request checklist
[ci full]to the PR title.