Skip to content
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

Compound index selection in query planner is broken #38

Closed
apkar opened this issue Jan 9, 2019 · 1 comment · Fixed by #78
Closed

Compound index selection in query planner is broken #38

apkar opened this issue Jan 9, 2019 · 1 comment · Fixed by #78
Assignees
Labels
bug Something isn't working In progress Actively working on the issue
Milestone

Comments

@apkar
Copy link
Contributor

apkar commented Jan 9, 2019

Document Layer allows users to create compound indexes and updates them properly. But, the query planner is not yet using them. Query planner uses them just like Simple indexes. For example, if we have a compound index on fields a and b, query planner treats the index as a Simple Index on a. So, if there is a query with predicate a == "foo" and b == "bar" - query planner would scan the index with bounds foo, foo0, and runs FilterPlan on it to look for bar. Instead, it should scan with bounds foo:bar, foo:bar0.

@apkar apkar added the enhancement New feature or request label Jan 10, 2019
@apkar apkar added this to the 1.7 milestone Jan 25, 2019
@apkar apkar self-assigned this Feb 12, 2019
@apkar
Copy link
Contributor Author

apkar commented Feb 12, 2019

In [2]: coll = db['test_comp_ind']

In [3]: coll.drop_indexes()

In [4]: coll.create_index([('a', pymongo.ASCENDING), ('b', pymongo.ASCENDING), ('c', pymongo.ASCENDING)])
Out[4]: u'a_1_b_1_c_1'

In [5]: coll.find({'a': 'aaa', 'b': 'bbb'}).explain()
Out[5]:
{u'explanation': {u'source_plan': {u'projection': u'{}',
   u'source_plan': {u'filter': u'ANY(ExtPath((b\\x00) matching EQUALS(\'"bbb"\'))',
    u'source_plan': {u'bounds': {u'begin': u'(aaa\\x00', u'end': u'(aaa\\x00'},
     u'index name': u'a_1_b_1_c_1',
     u'type': u'index scan'},
    u'type': u'filter'},
   u'type': u'projection'},
  u'type': u'non-isolated'}}

In [6]: coll.find({'a': 'aaa', 'b': 'bbb', 'c': 'ccc'}).explain()
Out[6]:
{u'explanation': {u'source_plan': {u'projection': u'{}',
   u'source_plan': {u'filter': u'AND(ANY(ExtPath((c\\x00) matching EQUALS(\'"ccc"\')), ANY(ExtPath((b\\x00) matching EQUALS(\'"bbb"\')))',
    u'source_plan': {u'bounds': {u'begin': u'(aaa\\x00', u'end': u'(aaa\\x00'},
     u'index name': u'a_1_b_1_c_1',
     u'type': u'index scan'},
    u'type': u'filter'},
   u'type': u'projection'},
  u'type': u'non-isolated'}}

In [7]:

Both queries should use the index a_1_b_1_c_1. Bug is in prefix matching, its always matching only the first key.

@apkar apkar changed the title Use compound indexes from Query Planner Compound index selection in query planner is broken Feb 12, 2019
@apkar apkar added bug Something isn't working and removed enhancement New feature or request labels Feb 12, 2019
@apkar apkar added the In progress Actively working on the issue label Feb 14, 2019
@apkar apkar closed this as completed in #78 Feb 14, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working In progress Actively working on the issue
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant