-
Notifications
You must be signed in to change notification settings - Fork 626
Run one instead of n queries #2982
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
Coverage ReportAffected SDKs
Test Logs
NotesHTML coverage reports can be produced locally with Head commit (b7c5b76f) is created by Prow via merging commits: 90efb98 58a179c. |
Binary Size ReportAffected SDKs
Test Logs
NotesHead commit (b7c5b76f) is created by Prow via merging commits: 90efb98 58a179c. |
eff0bd7
to
4110173
Compare
/test device-check-changed |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I want to see how you feel about an alternative: using SQL 'UNION' instead.
There are some advantages with UNION:
- It is less sql text manipulating, the old code can be modified to simply concat the sql with UNION.
- This would also be mentally close to IndexedDb and LevelDb.
- My playing with sqlite query plan suggests UNION might be more efficient..
It looks like SQLite is not doing all optimization with connected OR filters, it will simply do a filter on index_id
.
The query plan for using OR
sqlite> select * from index_entries_test where (index_id=1 and index_value >= 'x' and index_value <'z') or (index_id = 1 and index_value > 'a' and index_value <'c');
QUERY PLAN
`--SEARCH TABLE index_entries_test USING COVERING INDEX sqlite_autoindex_index_entries_test_1 (index_id=?)
The query plan for using UNION:
sqlite> select * from index_entries_test where (index_id = 1 and index_value > 'a' and index_value <'c')
...> union
...> select * from index_entries_test where (index_id=1 and index_value >= 'x' and index_value <'z')
...> order by index_value
...> limit 10
...> ;
QUERY PLAN
`--MERGE (UNION)
|--LEFT
| `--SEARCH TABLE index_entries_test USING COVERING INDEX sqlite_autoindex_index_entries_test_1 (index_id=? AND index_value>? AND index_value<?)
`--RIGHT
`--SEARCH TABLE index_entries_test USING COVERING INDEX sqlite_autoindex_index_entries_test_1 (index_id=? AND index_value>? AND index_value<?)
@Nullable Bound upperBound = target.getUpperBound(fieldIndex); | ||
|
||
if (Logger.isDebugEnabled()) { | ||
Logger.warn( | ||
Logger.debug( |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The if check above is not neccessary.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It is meant to reduce computation complexity as it avoids stringifying the protos.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Got it.
33b3666
to
8c8993c
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for the great suggestion. Updated to use UNION, which is also a bit simpler.
@Nullable Bound upperBound = target.getUpperBound(fieldIndex); | ||
|
||
if (Logger.isDebugEnabled()) { | ||
Logger.warn( | ||
Logger.debug( |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It is meant to reduce computation complexity as it avoids stringifying the protos.
@Nullable Bound upperBound = target.getUpperBound(fieldIndex); | ||
|
||
if (Logger.isDebugEnabled()) { | ||
Logger.warn( | ||
Logger.debug( |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Got it.
Instead of running the same query a bunch of times (e.g. for IN queries), this PR now runs a query with multiple OR clauses. This allows SQLite to dedupe results.
I think this should never hit a statement limit since our query complexity is quite limited.