Serve sorted searches from an index, and page past an id of 0 (fkie-cad/mcritweb#59) - #162
Open
r0ny123 wants to merge 1 commit into
Open
Serve sorted searches from an index, and page past an id of 0 (fkie-cad/mcritweb#59)#162r0ny123 wants to merge 1 commit into
r0ny123 wants to merge 1 commit into
Conversation
A search sorted by anything but the id had no index to be served from: the planner filtered on the search field and then sorted every matching document in memory (a blocking SORT stage on every such query, on functions the whole filtered set), which is the ~10x reported in fkie-cad/mcritweb#59. Each field the results can be sorted by now carries a compound (field, id) index; the tie-break follows the direction of the sort field, so the same index is walked backwards for the descending order instead of needing a second one. The plan for every sortable field is LIMIT -> FETCH -> IXSCAN afterwards, which a mongo-backed test asserts. As with the v1.6.2 indexes, the first start after upgrading builds them. Paging through a sorted listing also stopped early whenever a page ended on id 0: the forward cursor was built behind `if last_element_key:`, which is False for function 0, sample 0 and the unknown family 0. It is `is not None` now, with a test that pages a listing to the end in both directions.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Closes fkie-cad/mcritweb#59 (labelled
mcrit): searches sorted by anything but the id had no index to be served from.What changed
_ensureIndexAndUnknownFamilycreates a compound(field, id)index for every field the search results can be sorted by in MCRITweb: familiesfamily_name,num_samples,num_library_samples,num_functions; samplesfamily_id,family,filename,version,bitness,sha256,statistics.num_functions; functionsfamily_id,sample_id,function_name,offset,num_instructions,num_blocks. As with the v1.6.2 indexes, the first start after upgrading builds them._get_sort_data). The order stays total and deterministic, and one index serves the descending order by being walked backwards; with(field asc, id asc)and a(field desc, id asc)sort no single index could.if last_element_key:, which is False for function 0, sample 0 and the unknown family 0. It isis not Nonenow. The details are in the commit message and the test.Measured
explain()on a real database, filterfunction_nameregex, sortnum_blocks/offset/function_name(functions),family(samples),num_samples(families), both directions:SORT → FETCH → IXSCAN(blocking in-memory sort of every filtered document)LIMIT → FETCH → IXSCAN(index order, stops at the limit)A mongo-backed test asserts the indexes exist and that the plan for a sorted search has no
SORTstage; another pages a listing to the end in both directions on the memory backend.ruff,tyand the full suite pass.