Performance issue for multiple index queries when limit is given
g.V().has("name", "bob").limit(1) query is very fast, provided index is built for name.
However, g.V().has("name", "bob").has("age", 20).limit(1) query is slow, provided name and age have separate indices. Ideally this should not (often) happen because a small limit is given.
I've looked through several relevant PRs:
https://github.com/JanusGraph/janusgraph/pull/481 starts to stream the result from the index backend. It leverages scroll functionality provided by some external index backends to speed up retrieval (especially the first few results). To achieve so, this divides queries into two parts: first part fed into SubqueryIterator directly; the rest of queries go to QueryUtil.processIntersectingRetrievals first where actual results are fetched and collected, and then fed into SubqueryIterator as well. The SubqueryIterator then does an intersection of results of two parts and returns the iterator.
The above PR introduces a bug: it feeds QueryUtil.processIntersectingRetrievals with the limit. As pointed out by https://github.com/JanusGraph/janusgraph/issues/1125, since the total queries are divided into two parts now, we should not use the given limit to retrieve the rest of the queries and do intersection with the first query. Thus this PR https://github.com/JanusGraph/janusgraph/pull/1330 fixes the bug by passing through Infinity as the limit. However, this means when there are more than one index query, we always need to fetch all results (except the first query whose results are streamed). This means the streaming strategy, in this case, does harm to the overall performance, though it is still very useful when there is only one index query.
Although I haven't tested the behavior thoroughly, from looking at the code I would say the performance problem exists.
Maybe we can rewrite SubqueryIterator by moving the intersecting logic into it, instead of splitting total queries into two parts and handling them in two different places. Then we can leverage the streaming feature for all queries, and do intersection properly. The overall goal is that when a limit is given, a query consisting of multiple indexes should be reasonably fast.
@li-boxuan the problem here is that, in order to make sure we are not throwing away valid results, we HAVE to retrieve all results of all indices selected for intersection, except the one we chose for streaming. So, while you are right and we could rewrite the code such that it can stream all index results (as opposed to loading all results for non-streamed indices into memory in full), and do some sort of "streaming intersection" - this in itself is unlikely to help with performance (aside from preventing undue memory pressure).
What WOULD help with performance is if the query engine would have access to index stats when choosing which indices to use in the query execution plan, to start with. It would then see that your index on age field is not selective enough, and could decide not to use it at all - i.e. to use just name index, and then apply age filter on the streamed results from name index. There is even a comment to this extent in the code from the original author: https://github.com/JanusGraph/janusgraph/blob/677a57aa985a7ea47af70d0eaba30171d765c79d/janusgraph-core/src/main/java/org/janusgraph/graphdb/query/graph/GraphCentricQueryBuilder.java#L346
In the meantime, you can work around this issue by manually rewriting your query to prevent combining two "has" conditions and thus the selection of "bad" index for execution:
g.V().has("name", "bob").map(x -> x.get()).has("age", 20).limit(1)
Here map(x -> x.get()) is a "noop" step, but inserting it in between two has() steps prevents JG from trying to use the second index (on age)
Hope this makes sense, Dmitry
PS. I am by no means an expert in the latest DB engine techniques, so it is quite possible that more advanced techniques exist for better utilising some specially built index structures. For example, indices constructed in a special way could allow you to both answer such queries in a fastest possible way, AND at the same time scale well when the available indices/query conditions are not very selective and streaming is required to avoid memory issues.
@dk-github Thanks for the points. Totally agree with your proposed workaround and the possible optimization that can be done.
You also mentioned "streaming intersection" can reduce memory load but cannot help with performance boost, because:
in order to make sure we are not throwing away valid results, we HAVE to retrieve all results of all indices selected for intersection, except the one we chose for streaming.
Maybe I got something wrong, but I have a different view here. As the below snippet shows, the original author used a trick to avoid retrieving all results:
https://github.com/JanusGraph/janusgraph/blob/1978086c9be7df91fc7a7dedf3b4bbc74cb8d9de/janusgraph-core/src/main/java/org/janusgraph/graphdb/query/QueryUtil.java#L306-L331
Say we have 3 index queries, and the limit is 10, then the subLimit is 10 * 4 = 40. Then we execute the queries with 40 as the limit. If the size of results after the intersection exceeds 10, then we are good to return. Otherwise we make the subLimit as 40^1.5 and execute the queries with the new limit again. By doing so iteratively, we finally manage to retrieve the 10 results the user wants.
In theory, this strategy can work pretty well when the limit is a reasonable small number. Therefore, I don't think we have to retrieve all the results of index queries.
The drawback of the above strategy is that only the last round of queries really work; the first few rounds of trials are wasted. The good thing is, since we have scrolling ability now, for indexing backends that support scrolling, we can cache the results in memory and execute the next round of queries starting from our last scroll_id.
In short, I think for a gremlin query with multiple index queries and a limit, the original code before https://github.com/JanusGraph/janusgraph/pull/481 seems already to be able to handle reasonably well. After a few PRs, however, we lost the ability to do it in a timely manner.
I agree that progressively increasing the limit when loading results from non-streamed indices can be "on average" better than loading whole results straight away. However as you mentioned, it is still not "streaming" - it just progressively loads and throws away bigger chunks of the same work. And in cases where the we have to load pretty much all results before we reach original limit anyway, it performs way worse than just loading all data straight away (or exhausting the streams), while taking the same amount of memory at peak (and producing way more churn on the way).
And again, none of this would matter much if we only selected the indices which produce small results to start with.
So, while one could argue that we can reinstate the original logic because it is better in some cases, I think the ultimate solution is still to a) select only indexes which are likely to actually help reduce work, not increase it b) implement proper streaming intersection across all index results (or just fall back to index-less query execution plan), for the case where we are still getting huge result and risk dying with OOM if we try to load all of them in full.
On Sat, 7 Mar 2020 at 12:27, Boxuan Li [email protected] wrote:
@dk-github https://github.com/dk-github Thanks for the points. Totally agree with your proposed workaround and the possible optimization that can be done.
You also mentioned "streaming intersection" can reduce memory load but cannot help with performance boost, because:
in order to make sure we are not throwing away valid results, we HAVE to retrieve all results of all indices selected for intersection, except the one we chose for streaming.
Maybe I got something wrong, but I have a different view here. As the below snippet shows, the original author used a trick to avoid retrieving all results:
https://github.com/JanusGraph/janusgraph/blob/1978086c9be7df91fc7a7dedf3b4bbc74cb8d9de/janusgraph-core/src/main/java/org/janusgraph/graphdb/query/QueryUtil.java#L306-L331
Say we have 3 index queries, and the limit is 10, then the subLimit is 10
- 4 = 40. Then we execute the queries with 40 as the limit. If the size of results after the intersection exceeds 10, then we are good to return. Otherwise we make the subLimit as 40^1.5 and execute the queries with the new limit again. By doing so iteratively, we finally manage to retrieve the 10 results the user wants.
In theory, this strategy can work pretty well when the limit is a reasonable small number. Therefore, I don't think we have to retrieve all the results of index queries.
The drawback of the above strategy is that only the last round of queries really work; the first few rounds of trials are wasted. The good thing is, since we have scrolling ability now, for indexing backends that support scrolling, we can cache the results in memory and execute the next round of queries starting from our last scroll_id.
In short, I think for a gremlin query with multiple index queries and a limit, the original code before #481 https://github.com/JanusGraph/janusgraph/pull/481 seems already to be able to handle reasonably well. After a few PRs, however, we lost the ability to do it in a timely manner.
— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub https://github.com/JanusGraph/janusgraph/issues/2032?email_source=notifications&email_token=ABNBTWRKGWUT5H6F3OINYR3RGI4SPA5CNFSM4LDONPSKYY3PNVWWK3TUL52HS4DFVREXG43VMVBW63LNMVXHJKTDN5WW2ZLOORPWSZGOEODYDPQ#issuecomment-596083134, or unsubscribe https://github.com/notifications/unsubscribe-auth/ABNBTWXABSSMM4PIHIPTGPDRGI4SPANCNFSM4LDONPSA .
PS. it doesn't mean I am specifically against reinstating the original logic - I am just saying tha it is not the crux of the problem, the crux is throwing away the indices which do not help. For which we need index stats and/or ability to efficiently probe index result counts for a query.
So if your proposal is to just reinstate the original logic - you could submit a PR to that effect and see how it goes with the other members.
But if you are able to spend some time on addressing the root problem - that would be even better!
in cases where the we have to load pretty much all results before we reach original limit anyway, it performs way worse than just loading all data straight away (or exhausting the streams), while taking the same amount of memory at peak (and producing way more churn on the way).
Agree. Fortunately for indexing backends that support scroll, this shouldn't be a problem. But yeah, there are cases when progressively loading could lead to worse performance, e.g. using several composite indices, and unfortunately, the first few rounds of queries turn out to be useless.
An interesting thing to think about: when the user specifies a limit in the gremlin query, what is the intention? Would it be a reasonably good chance that the user expects the total number of possible results far exceeds his desired number? Under this circumstance, progressive loading might be the thing the user prefers to aggressive loading.
My idea with addressing the root cause is still very simple at this moment:
Say we have three index queries, Q1, Q2, Q3, and the limit is 10. Then the initial subLimit could be 40. Assume during the intersection, we find that Q1 and Q2 share 30 results, while Q3 only shares 5 results with both of them. Then the number of valid results we achieve in this round <= 5. In the next round, we don't execute queries against Q1 & Q2. Instead, we only fetch results from Q3, since Q3 is more likely to be the bottleneck, or in other words, more selective. In the extreme case, Q1 and Q2 are 'everything query', and Q3 is 'unique id query'. Then it makes sense that we should execute Q3 way more than Q1 and Q2.
There are more possible strategies, e.g. we prioritize queries that take less time; we prioritize queries that have already been cached. We can calculate a score based on multiple factors and then pick up the optimal query to execute.
I will try to make a PR and we can see how it goes :)
Sure, this may be one way to address both issues at once.
I am just saying that if you had index stats upfront, or could query each index for the number of results matching the respective condition upfront, you could make a right decision without much trial and error.
And in your case, to avoid re-querying from scratch with new limit, you could try implementing it such that you just continue reading from original index query results. If you remember the elements from each set you have seen but thrown away in previous attempts, you can re-iterate those items to add to your previous intersection approximation after you decide which index to load more data from.
Incidentally, I guess that would be what I called "proper streaming intersection" for the case where we don't know anything about the underlying index structure