From 160111a5c512c97075c53fcfde8293bdf762e9a9 Mon Sep 17 00:00:00 2001 From: Bob Grabar Date: Fri, 23 Jan 2015 13:05:27 -0500 Subject: [PATCH 1/2] WRITING-904 updates to compound indexes per audit --- source/core/index-compound.txt | 50 ++++++++++++++++++---------------- 1 file changed, 26 insertions(+), 24 deletions(-) diff --git a/source/core/index-compound.txt b/source/core/index-compound.txt index cc6b7f6187d..75853f06721 100644 --- a/source/core/index-compound.txt +++ b/source/core/index-compound.txt @@ -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. - -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: + +- ``item`` +- ``item``, ``location``, in that order + +MongoDB **cannot** use the index for queries on the following fields, none +of 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``. +- only on the ``location`` field +- only on the ``stock`` field +- only on the ``location`` and ``stock`` fields, no matter the order -MongoDB **cannot** use this index to support queries that include: +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``. -- only the ``location`` field, -- only the ``stock`` field, or -- only the ``location`` *and* ``stock`` fields. +If an index is an exact prefix of another index, you usually can remove +it. For example, if you have an index on ``{ a: 1 }`` and also have a +compound index on ``{ a: 1, b: 1 }``, and if neither index has a sparse or +unique constraint, 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. Index Intersection ------------------ From 3ff79ec65261083da26411d1b8bc631ef16c3d5a Mon Sep 17 00:00:00 2001 From: Bob Grabar Date: Fri, 23 Jan 2015 15:54:18 -0500 Subject: [PATCH 2/2] review edits --- source/core/index-compound.txt | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/source/core/index-compound.txt b/source/core/index-compound.txt index 75853f06721..c7378979fc3 100644 --- a/source/core/index-compound.txt +++ b/source/core/index-compound.txt @@ -115,6 +115,13 @@ 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 an index is an exact prefix of a second index, and if neither index has +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. + Given the following compound index: .. code-block:: javascript @@ -125,27 +132,20 @@ MongoDB can use the index for queries on the following fields, which are prefixes of the index: - ``item`` -- ``item``, ``location``, in that order +- ``item``, ``location`` MongoDB **cannot** use the index for queries on the following fields, none of which are prefixes of the index: - only on the ``location`` field - only on the ``stock`` field -- only on the ``location`` and ``stock`` fields, no matter the order +- 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``. -If an index is an exact prefix of another index, you usually can remove -it. For example, if you have an index on ``{ a: 1 }`` and also have a -compound index on ``{ a: 1, b: 1 }``, and if neither index has a sparse or -unique constraint, 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. - Index Intersection ------------------