opensearch-project / sql

Query your data using familiar SQL or intuitive Piped Processing Language (PPL)
https://opensearch.org/docs/latest/search-plugins/sql/index/
Apache License 2.0
121 stars 140 forks source link

[FEATURE] Improve query performance for IN clause with long value list #3117

Open dai-chen opened 4 weeks ago

dai-chen commented 4 weeks ago

Is your feature request related to a problem?

Currently, long IN value lists are handled recursively and translated into deeply nested term queries, which can lead to performance issues. This approach is inefficient for handling large sets of values in SQL translations.

What solution would you like?

The solution needs further exploration, but the key steps should include:

  1. Optimizing the recursion in the Analyzer and FilterQueryBuilder.
  2. Replacing the deeply nested Term queries with the OpenSearch Terms query which seems better suited for handling large collections of values.

What alternatives have you considered?

N/A

Do you have any additional context?

Example:

... WHERE src_ip IN ('192.168.0.1', '192.168.0.2', '192.168.0.3' ...)

{
  ...
   "query":{
      "bool":{
         "should":[
            {
               "term":{
                  "src_ip":{
                     "value":"192.168.0.1",
                     "boost":1.0
                  }
               }
            },
            {
               "bool":{
                  "should":[
                     {
                        "term":{
                           "src_ip":{
                              "value":"192.168.0.2",
                              "boost":1.0
                           }
                        }
                     },
                     {
                        "bool":{
                           "should":[
                              {
                                 "term":{
                                    "src_ip":{
                                       "value":"192.168.0.3",
                                       "boost":1.0
                                    }
                                 }
                              },
                              {
                                 "bool":{
                                    "should":[
                                       {
                                          "term":{
                                             "src_ip":{
                                                "value":"192.168.0.4",
                                                "boost":1.0
                                             }
                                          }
                                       },
                                       {
                                          ...