-
Notifications
You must be signed in to change notification settings - Fork 1.8k
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
[RFC] Query Planning and Rewriting #12390
Comments
Most existing query engines don't run optimized queries in shadow mode - They optimize the plan and execute it. I would consider requesting customers to opt-in when we launch the feature initially. Later, once we have tuned the system, we can make query planning the default and let customers turn it off. |
How Good are Query Optimizers, Really? (https://www.vldb.org/pvldb/vol9/p204-leis.pdf) might be relevant reading. |
Thanks @kkmr for sharing this. I went through this paper and this is mostly around cardinality estimation and join order both of which are not directly relevant for Opensearch. Few observations from this publication:
I am also leaning towards introducing query rewriting as feature which can be tuned or turned off using cluster setting. This will allow the front-loading of core value proposition, and the framework for tuning those settings/parameters can be worked upon in later milestones.
While I hope, this would be eventually possible, I feel that tuning for query planning will be continuous exercise for different types of customer workloads |
Recently, Opensearch customers have shown strong interest for more performant and resource efficient Join operation. One approach for such "Join" is making it first class citizen within Opensearch and building a query planner more aware of OpenSearch shard and Lucene index architecture. Both logical and physical plan to execute a query could be optimized over existing SQL plugin architecture for “Join“, which is based on executing OpenSearch queries, gathering all data and performing Join operation over them. It doesn’t deal or make use of intricacies of OpenSearch architecture but works more like an application sitting on top of OpenSearch. Very simple join operation across 2 indices in OpenSearch could look something like below:
Note: Extensive Query DSL grammar and query engine related changes will be covered in much more depth as part of separate RFC by @rishabhmaurya. This RFC is specifically focused on the Query Planning aspects around this Join operation. |
High level design for Query Planner within OpenSearch could look something like below: Query Planner can be a library or service available on all the coordinator nodes. It will take QueryDSL and IndexMetadata as primary input and generation Query Execution plan understood by OpenSearch Query Engine as output. There might be intermediate step similar to canMatch phase today for collecting additional metadata to allow better cardinality estimation and cost modelling. There can also be an option to collect this metadata asynchronously and keep it cached.
|
At a high level this looks good. I would like to battle-test this proposal against vector search because it has some native parts, maybe @navneet1v could take a look? |
love this idea. some thoughts
|
@penghuo thanks for providing feedback. I'm little hesitant in moving SQL support to core as it could be a significant overhead in maintenance in the core and I like the plugin model better for supporting a new query language frontend. |
Do look the cratedb implementation of joins (https://cratedb.com/docs/crate/reference/en/latest/concepts/joins.html). CrateDB is derived from elastic- so it uses Lucene as the storage. It also ports over postgres query engine code to Java (Postgres QP is reputed to be pretty good). So some of the ideas in the cratedb code might translate well to this effort. |
I took a high level look. In terms of vector search, as long as query is coming via Lucene Searcher interface vector search won't have any issue. @dblock is there any specific thing you want me to look at. Because after reading the doc, what I can see on high level is we are trying to rewrite the queries to optimized queries and not sure how vector queries can make use of it because vector search queries doesn't have complexities like Histogram aggs etc. Please correct me if my understanding is wrong. |
Thanks for chiming in! I wanted to make sure that the cost estimation will be able to (eventually) account for native parts of search (faiss, etc.). |
Thanks for putting it together, @jainankitk ! I would really like to see Query Cost Estimation Based implementation (although I 💯 agree this is hard problem). On the bright side, we could engage with Apache Lucene folks to sketch out a "fair" query cost estimation model per index (== OS shard). |
@dblock To know what will be the cost, I would like to know more details on how cost estimation is working. I see we have sprinkled this information little bit in the GH issue, but those might not work for every type of query. For vector search we cannot estimate just like this. There are other(algorithm related and query related) parameters that defines the scope of search. I think it will require more discussion. |
More importantly is that we build mechanisms (interfaces) for the knn plugin to participate in the cost estimation. |
@navneet1v - Thank you for providing your feedback from the knn perspective. While we are working on ironing out more details around cardinality/cost estimation and overall long term approach, I am wondering if there are specific knn queries that can benefit from Query Planning. For example - Join operation can significantly benefit from the order in which it is executed, are there similar rewriting optimizations that can be done for knn queries resulting in noticeable performance improvement? |
Problem statement
In traditional database systems, query planner and optimizer chooses the right indices and join order to ensure efficient execution of query. Opensearch does not have any such component causing degraded performance for queries not written well. The profiler can provide insights into the latency breakdown of each phase, but does not automatically optimize the queries for customers. The impact of manually rewriting the query has been confirmed both by github issues and during managed service customer engagements:
High Level Approach
There are primarily two approaches for solving this problem. One approach relies on cost estimation for planning the query similar to traditional database systems and other is more dynamic in nature by collecting feedback from the performance of rewritten queries and accordingly, enabling/disabling and tuning the parameters for query rewriting.
Query Cost Estimation Based
The key aspect of this approach is the query cost estimation component. Given any query, it is able to guess the query cost well allowing the query rewriter to compare multiple query plans. Accordingly, it can choose the most efficient form for query execution.
Pros:
Cons:
Rewritten Query Execution Based
This approach starts with the query rewriting in shadow mode. For every query, it checks if the query is rewrite eligible and samples such queries, executing them asynchronously (shadow mode) and comparing the execution time for the original vs rewritten query. Along with the execution time, every rewrite logic emits the value of tunable rewrite parameters for the query rewrites. Taking date histogram and doc values rewrite as example, we can expect following data after few executions:
Date Histogram Parameters
Rewrite efficiency:
Doc Values Rewrite Parameters
Rewrite efficiency:
Using the above data, query insights plugin will be able to help detect the right parameter values for each query rewrite type. And once it has sufficient confidence, it can operate in reverse shadow mode where the original query is run occasionally to detect any changes in workload for that particular type of rewrite.
Pros:
Cons:
Mitigations:
Related component
Search:Performance
Describe alternatives you've considered
No response
Additional context
No response
The text was updated successfully, but these errors were encountered: