Skip to content

WRITING-904 updates to compound indexes per audit #2137

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

Closed
wants to merge 2 commits into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 25 additions & 23 deletions source/core/index-compound.txt
Original file line number Diff line number Diff line change
Expand Up @@ -110,39 +110,41 @@ following:
Prefixes
--------

Compound indexes support queries on any prefix of the index
fields. Index prefixes are the beginning subset of indexed fields. For
example, given the index ``{ a: 1, b: 1, c: 1 }``, both ``{ a: 1 }``
and ``{ a: 1, b: 1 }`` are prefixes of the index.
Compound indexes support queries on any prefix of the index fields.
Index prefixes are the beginning subsets of indexed fields. For example,
``{ a: 1 }`` and ``{ a: 1, b: 1 }`` are both prefixes of the index
``{ a: 1, b: 1, c: 1 }``.

If you have a collection that has a compound index on ``{ a: 1, b:
1 }``, as well as an index that consists of the prefix of that index,
i.e. ``{ a: 1 }``, assuming none of the index has a sparse or unique
constraints, then you can drop the ``{ a: 1 }`` index. MongoDB will be
able to use the compound index in all of situations that it would have
used the ``{ a: 1 }`` index.
If an index is an exact prefix of a second index, and if neither index has
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think you moved the wrong paragraph. That's probably my fault -- I probably commented on an outdated commit, which skewed the line. This needs to go down, so organizationally, we want:

Prefixes

def of prefixes of indexes

examples of queries supported by index

examples of queries not supported by index

Can get rid of other indexes that are prefixes.

a sparse or unique constraint, then you can remove the first index. For
example, if you have an index on ``{ a: 1 }`` and also have a compound
index on ``{ a: 1, b: 1 }``, then you can remove the ``{ a: 1 }`` index.
MongoDB will use the ``{ a: 1, b: 1 }`` index in all of the situations
that it would have used the ``{ a: 1 }`` index.

For example, given the following index:
Given the following compound index:

.. code-block:: javascript

{ "item": 1, "location": 1, "stock": 1 }

MongoDB **can** use this index to support queries that include:
MongoDB can use the index for queries on the following fields, which are
prefixes of the index:

- the ``item`` field,
- the ``item`` field *and* the ``location`` field,
- the ``item`` field *and* the ``location`` field *and* the
``stock`` field, or
- only the ``item`` *and* ``stock`` fields; however, this index
would be less efficient than an index on only ``item`` and
``stock``.
- ``item``
- ``item``, ``location``

MongoDB **cannot** use this index to support queries that include:
MongoDB **cannot** use the index for queries on the following fields, none
of which are prefixes of the index:

- only the ``location`` field,
- only the ``stock`` field, or
- only the ``location`` *and* ``stock`` fields.
- only on the ``location`` field
- only on the ``stock`` field
- only on the ``location`` and ``stock`` fields

MongoDB **can** use the index to support a query on ``item`` and
``stock``, even though they are not a prefix. However, the index would not
be as efficient in supporting the query as would be an index on only
``item`` and ``stock``.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should not be here. Belongs where it used to be. item is a prefix.


Index Intersection
------------------
Expand Down