Optimize index select algorithm to reduce number of index queries
Sometimes an additional composite index query is picked up even if a mixed index query itself is sufficient. See the following example:
graph = JanusGraphFactory.open('conf/janusgraph-cql-es.properties')
mgmt = graph.openManagement()
name = mgmt.makePropertyKey('name').dataType(String.class).make()
nickname = mgmt.makePropertyKey('nickname').dataType(String.class).make()
mgmt.buildIndex('composite', Vertex.class).addKey(name).buildCompositeIndex()
mgmt.buildIndex('mixed',Vertex.class).addKey(name,Mapping.STRING.asParameter()).addKey(nickname,Mapping.STRING.asParameter()).buildMixedIndex('search')
mgmt.commit()
g = graph.traversal()
g.V().has('name', 'Robert').has('nickname', 'Bob').profile()
The result shows that it creates a JointIndexQuery consisting of two subqueries, one using Composite Index and the other one using Mixed Index:
==>Traversal Metrics
Step Count Traversers Time (ms) % Dur
=============================================================================================================
JanusGraphStep([],[name.eq(Robert), nickname.eq... 135.230 100.00
\_condition=(name = Robert AND nickname = Bob)
\_orders=[]
\_isFitted=true
\_isOrdered=true
\_query=[(name = Robert AND nickname = Bob)]:mixed
\_index=mixed
\_index_impl=search
optimization 0.023
optimization 35.363
backend-query 0 89.864
\_query=mixed:[(name = Robert AND nickname = Bob)]:mixed
backend-query 0 1.583
\_query=composite:multiKSQ[1]@2147483647
>TOTAL - - 135.230 -
Given that in this case, the mixed index already covers all conditions, I believe there is no need to fetch the composite index. Plus, a 'multiple indices' query is much more expensive than a 'single index' query.
This is because Composite Index always has a very high score in GraphCentricQueryBuilder. If we have two composite indices, one for name and one for nickname (meaning that composite indices can cover all conditions), then two composite indices might be better than a single mixed index. However, in the case that one single mixed index can cover all conditions, it does not make sense to involve a composite index.
In general, if an index A is necessary (in the sense that some condition can only be covered by it), and it is a superset of some other index B (regardless of whether it's composite or mixed), then B can be removed from the JointIndexQuery. This strategy can solve the problem mentioned beforehand but can fail to deal with situations that are more complicated.
To make it clearer, let's assume the conditions given by the user are condition 1, condition 2 & condition 3. Consider three index candidates:
- composite index A that covers condition 1
- mixed index B that covers condition 1 & 2
- mixed index C that covers condition 2 & 3
According to the current scoring scheme, composite index A will firstly be picked up (composite index is preferred). The next index query that gets selected can be B or C. C is better in the sense that it covers two additional conditions, while B only covers one, but let's just assume B is better in some other way (e.g. support sorting). Then B is selected after A. Finally we still have to pick up index C because we yet to cover condition 3.
So finally, all three indices are selected. However, we can see that the real optimal index combination shall be composite index A + mixed index C. Although B shall be removed, it is not a subset of any other index. Now we can see this problem is indeed the famous Set Cover Problem. To be more precise, it is the weighted set cover problem.
Since Set Cover Problem is a NP-complete problem, I think we can do the following:
- if the number of index candidates is reasonably small, say, 10, then we use the brute force algorithm with exponential complexity to select the optimal set of queries
otherwise,
- we use an approximation algorithm, e.g. https://courses.cs.washington.edu/courses/cse525/13sp/scribe/lec4.pdf. This can be done in the second stage, and for now, we can keep the current algorithm.
@li-boxuan as discussed on a similar issue elsewhere, I may be missing some context here but I think the long-term solution to all these various "index selection" problems is to establish an API for requesting/refreshing/passing index stats from backends into the query builder. It then can evaluate exact or approximate number of hits for each condition/index and decide correctly in O(1) time what is the best index combination, as well as what is the best retrieval strategy (load one upfront, filter out, load a few upfront and intersect, do "streaming intersection" with limit as discussed elsewhere, etc).
I agree that this is a big and risky chunk of work, but arguably the ROI for total effort of doing small improvements to existing code vs the achieved improvement (if any) is not so big, and the effort can be comparable.
As before, this is not to say that we shouldn't consider small improvements to existing logic - just that if you are diving deep into this part of code anyway, and have the resources, then you might consider tackling the long-term solution as well.
@dk-github totally agree with your long-term blueprint. I am also thinking about that. At this stage, I tend to believe this incremental improvement (tackling index cover) is also a necessary part of the whole picture. So yes, it is an incremental improvement, but not like a workaround optimisation. I might change my mind on this particular issue in the future though.
That being said, your words are very valuable and inspirational. Everyone could think about that and maybe some day we can come up with a roadmap or milestone.
UPDATE: I actually don't think
decide correctly in O(1) time what is the best index combination
is possible, but I do believe we can give a better score estimate for each individual index by
establish an API for requesting/refreshing/passing index stats from backends into the query builder.
I didn't check #2099 enough, so my thoughts might be completely wrong but I am thinking, if choosing the best index is so costly right now, maybe we should cache index selection decisions per queries or some stats? In many applications the queries are pre-determined. If so, there will be not so many different queries. To remove repeated calculations of the index selection algorithms, we might simply cache decisions for specific queries / stats. Thus, the calculation will be executed only once per possible query. After that we will retrieve selected indexes from cache. Of course, if we are going to use cache, we should consider the next:
- When an index is created / changed / removed, we might need to invalidate our cache (partially or fully).
- In some cases the amount of different queries might be too big, so we should have an option to disable this cache or / and configure its max size.