Skip to content

Commit 486b47e

Browse files
DOCSP-28729 default meta time index (#3087) (#4255)
* Added info on default meta, time index and detailed use case for creating additional secondary indexes * Removed mention of default clustered index for time series collections, per SERVER-54965 * Removed mention of default clustered index for time series collections, per SERVER-54965 * Removed redundant 'internal indexes' heading since info is already presented earlier and the ref to adding secondary indexes is also present immediately prior * Updated limitations topic and expanded secondary index options in 6.0 and higher * Updated limitations topic and expanded secondary index options in 6.0 and higher--amend * Updated limitations topic and expanded secondary index options in 6.0 and higher--amend * Updated limitations topic and expanded secondary index options in 6.0 and higher--amend * Spelling and grammar check * Self review * internal PR feedback * Internal PR feedback * External PR feedback
1 parent 79821cd commit 486b47e

File tree

3 files changed

+105
-120
lines changed

3 files changed

+105
-120
lines changed

source/core/timeseries-collections.txt

Lines changed: 4 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -73,11 +73,12 @@ Benefits
7373
Compared to normal collections, storing time series data in time series
7474
collections improves query efficiency and reduces the disk usage for
7575
time series data and :term:`secondary indexes <secondary index>`.
76+
MongoDB 6.3 and later automatically creates a :ref:`compound index
77+
<index-type-compound>` on the time and metadata
78+
fields for new time series collections.
7679

7780
Time series collections use an underlying columnar storage format and
78-
store data in time-order with an automatically created :ref:`clustered
79-
index <clustered-collections>`. The columnar storage format provides the
80-
following benefits:
81+
store data in time-order. This format provides the following benefits:
8182

8283
- Reduced complexity for working with time series data
8384
- Improved query efficiency
@@ -116,26 +117,6 @@ optimized internal storage format and return results faster.
116117

117118
.. _manual-timeseries-internal-index:
118119

119-
Internal Index
120-
``````````````
121-
122-
When you create a time series collection, MongoDB automatically creates
123-
an internal :ref:`clustered index <clustered-collections>` on the time
124-
field. This index improves query efficiency and reduces disk
125-
usage. To learn more about the performance benefits of clustered
126-
indexes, see :ref:`Clustered Collections <clustered-collections>`.
127-
128-
The internal index is not listed when you run :dbcommand:`listIndexes`.
129-
130-
.. note::
131-
If you insert a document into a collection with a ``timeField``
132-
value before ``1970-01-01T00:00:00.000Z`` or after
133-
``2038-01-19T03:14:07.000Z``,
134-
MongoDB logs a warning and prevents some query optimizations from
135-
using the internal index. :ref:`Create a secondary index <timeseries-add-secondary-index>`
136-
on the ``timeField`` to regain query performance and resolve the log
137-
warning.
138-
139120
Get Started
140121
-----------
141122

source/core/timeseries/timeseries-limitations.txt

Lines changed: 15 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -100,33 +100,32 @@ To remove all documents from a collection, use the
100100
Time Series Secondary Indexes
101101
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
102102

103-
There is improved support for secondary indexes in MongoDB 6.0.
103+
MongoDB 6.0 and later features improved support for secondary indexes on
104+
time series collections. For a summary of these options, see :ref:`timeseries-add-secondary-index-mongodb-6.0`.
104105

105-
Time Series Secondary Indexes with MongoDB 6.0 and Later
106-
````````````````````````````````````````````````````````
106+
Time Series Secondary Indexes
107+
`````````````````````````````
108+
109+
Starting in MongoDB 6.3, time series collections support the ``expireAfterSeconds`` index property for :ref:`partial
110+
indexes <index-type-partial>` on the ``metaField``. For earlier versions
111+
of MongoDB, use the collection level :ref:`expireAfterSeconds
112+
<db.createCollection.expireAfterSeconds>` parameter.
107113

108114
Starting in MongoDB 6.0, you can add a :term:`secondary index <secondary
109115
index>` to any field.
110116

117+
These index types are partially supported:
118+
119+
- You can only create :ref:`multikey indexes <index-type-multikey>` on
120+
the ``metaField``.
121+
- You can only create :ref:`sparse indexes <index-type-sparse>` on the ``metaField``.
122+
111123
These index types aren't supported:
112124

113125
- :ref:`Text indexes <index-type-text>`
114126
- :ref:`2d indexes <2d-index>`
115127
- :ref:`Unique indexes <index-type-unique>`
116128

117-
You can only use the :ref:`multikey index <index-type-multikey>` type on
118-
the ``metaField``.
119-
120-
These index properties are partially supported. You can create:
121-
122-
- :ref:`Partial indexes <index-type-partial>` on every field except
123-
``metaField`` and ``timeField``.
124-
125-
- :ref:`Sparse indexes <index-type-sparse>` on the ``metaField``.
126-
127-
For improvements to time series secondary indexes available starting in
128-
MongoDB 6.0, see :ref:`timeseries-add-secondary-index-mongodb-6.0`.
129-
130129
.. include:: /includes/time-series-secondary-indexes-downgrade-FCV.rst
131130

132131
Time Series Secondary Indexes with MongoDB 5.0 and Earlier

source/core/timeseries/timeseries-secondary-index.txt

Lines changed: 86 additions & 81 deletions
Original file line numberDiff line numberDiff line change
@@ -18,26 +18,55 @@ Add Secondary Indexes to Time Series Collections
1818

1919
To improve query performance for :term:`time series collections <time
2020
series collection>`, add one or more :term:`secondary indexes <secondary
21-
index>` to support common time series query patterns. Specifically, we
22-
recommend that you create one or more :ref:`compound indexes
23-
<index-type-compound>` on the fields specified as the ``timeField`` and
24-
the ``metaField``. If the field value for the ``metaField`` field is a
25-
document, you can create secondary indexes on fields inside that
26-
document.
21+
index>` to support common time series query patterns. Starting in
22+
MongoDB 6.3, MongoDB automatically creates a :ref:`compound index
23+
<index-type-compound>` on the ``metaField`` and ``timeField`` fields for
24+
new collections.
2725

2826
.. note::
2927

3028
Not all index types are supported. For a list of unsupported index
3129
types, see :ref:`Limitations for Secondary Indexes on Time Series
3230
Collections <timeseries-limitations-secondary-indexes>`.
3331

34-
For example, this command creates a :ref:`compound index
35-
<index-type-compound>` on the ``metadata.sensorId`` and ``timestamp``
36-
fields:
32+
You may wish to create additional secondary indexes. Consider a weather
33+
data collection with the configuration:
3734

3835
.. code-block:: javascript
3936

40-
db.weather24h.createIndex( { "metadata.sensorId": 1, "timestamp": 1 } )
37+
db.createCollection(
38+
"weather",
39+
{
40+
timeseries: {
41+
timeField: "timestamp",
42+
metaField: "metadata"
43+
}})
44+
45+
In each weather data document, the ``metadata`` field value is a
46+
subdocument with fields for the weather sensor ID and type:
47+
48+
.. code-block:: javascript
49+
50+
{
51+
"timestamp": ISODate("2021-05-18T00:00:00.000Z"),
52+
"metadata": {
53+
"sensorId": 5578,
54+
"type": "temperature"
55+
},
56+
"temp": 12
57+
}
58+
59+
The default compound index for the collection indexes the entire
60+
``metadata`` subdocument, so the index is only used with
61+
:expression:`$eq` queries. By indexing specific ``metadata`` fields, you
62+
improve query performance for other query types.
63+
64+
For example, this :expression:`$in` query benefits from a
65+
secondary index on ``metadata.type``:
66+
67+
.. code-block:: javascript
68+
69+
{ metadata.type:{ $in: ["temperature", "pressure"] }}
4170

4271
.. see::
4372

@@ -48,9 +77,22 @@ fields:
4877
Use Secondary Indexes to Improve Sort Performance
4978
-------------------------------------------------
5079

51-
Time Series collections can use indexes to improve sort performance on
52-
the ``timeField`` and the ``metaField``.
53-
80+
Sort operations on time series collections can use secondary indexes
81+
on the ``timeField`` field. Under certain conditions, sort operations can also use compound secondary indexes on the ``metaField`` and
82+
``timeField`` fields.
83+
84+
The aggregation pipeline stages :pipeline:`$match` and
85+
:pipeline:`$sort` determine which indexes a time series collection can
86+
use. An index can be used in the following scenarios:
87+
88+
- Sort on ``{ <timeField>: ±1 }`` uses a secondary index on
89+
``<timeField>``
90+
- Sort on ``{ <metaField>: ±1, timeField: ±1 }`` uses the default
91+
compound index on ``{ <metaField>: ±1, timeField: ±1 }``
92+
- Sort on ``{ <timeField>: ±1 }`` uses a secondary index on
93+
``{ metaField: ±1, timeField: ±1 }`` when there is a point predicate
94+
on ``<metaField>``
95+
5496
For example, the following ``sensorData`` collection contains
5597
measurements from weather sensors:
5698

@@ -59,14 +101,15 @@ measurements from weather sensors:
59101
db.sensorData.insertMany( [ {
60102
"metadata": {
61103
"sensorId": 5578,
104+
"type": "omni",
62105
"location": {
63106
type: "Point",
64107
coordinates: [-77.40711, 39.03335]
65108
}
66109
},
67110
"timestamp": ISODate("2022-01-15T00:00:00.000Z"),
68111
"currentConditions": {
69-
"windDirecton": 127.0,
112+
"windDirection": 127.0,
70113
"tempF": 71.0,
71114
"windSpeed": 2.0,
72115
"cloudCover": null,
@@ -77,14 +120,15 @@ measurements from weather sensors:
77120
{
78121
"metadata": {
79122
"sensorId": 5578,
123+
"type": "omni",
80124
"location": {
81125
type: "Point",
82126
coordinates: [-77.40711, 39.03335]
83127
}
84128
},
85129
"timestamp": ISODate("2022-01-15T00:01:00.000Z"),
86130
"currentConditions": {
87-
"windDirecton": 128.0,
131+
"windDirection": 128.0,
88132
"tempF": 69.8,
89133
"windSpeed": 2.2,
90134
"cloudCover": null,
@@ -95,14 +139,15 @@ measurements from weather sensors:
95139
{
96140
"metadata": {
97141
"sensorId": 5579,
142+
"type": "omni",
98143
"location": {
99144
type: "Point",
100145
coordinates: [-80.19773, 25.77481]
101146
}
102147
},
103148
"timestamp": ISODate("2022-01-15T00:01:00.000Z"),
104149
"currentConditions": {
105-
"windDirecton": 115.0,
150+
"windDirection": 115.0,
106151
"tempF": 88.0,
107152
"windSpeed": 1.0,
108153
"cloudCover": null,
@@ -113,62 +158,7 @@ measurements from weather sensors:
113158
]
114159
)
115160

116-
Time Series collections :ref:automatically create an internal
117-
:ref:`clustered index <db.createCollection.clusteredIndex>`. The query
118-
planner uses this index to improve sort performance.
119-
120-
.. note::
121-
If you insert a document into a collection with a ``timeField``
122-
value before ``1970-01-01T00:00:00.000Z`` or after
123-
``2038-01-19T03:14:07.000Z``,
124-
MongoDB logs a warning and prevents some query optimizations from
125-
using the internal index. :ref:`Create a secondary index <timeseries-add-secondary-index>`
126-
on the ``timeField`` to regain query performance and resolve the log
127-
warning.
128-
129-
The following sort operation on the ``timestamp`` field uses the
130-
clustered index to improve performance:
131-
132-
.. code-block:: javascript
133-
134-
db.sensorData.find().sort( { "timestamp": 1 } )
135-
136-
To confirm that the sort operation used the clustered index, run the
137-
operation again with the ``.explain( "executionStats" )`` option:
138-
139-
.. code-block:: javascript
140-
141-
db.sensorData.find().sort( { "timestamp": 1 } ).explain( "executionStats" )
142-
143-
The ``winningPlan.queryPlan.inputStage.stage`` is ``COLLSCAN`` and an
144-
``_internalBoundedSort`` stage is present in the explain plan output.
145-
The ``interalBoundedSort`` field indicates that the clustered index was
146-
used. For more information on explain plan output, see :ref:`explain
147-
results <explain-results>`.
148-
149-
Secondary indexes on Time Series collections can improve
150-
performance for sort operations and increase the number of scenarios
151-
where indexes can be used.
152-
153-
Sort operations on Time Series collections can use secondary indexes
154-
on the ``timeField``. Under certain conditions, sort operations can
155-
also use compound secondary indexes on the ``metaField`` and
156-
``timeField``.
157-
158-
The Aggregation Pipeline Stages :pipeline:`$match` and
159-
:pipeline:`$sort` determine which indexes a Time Series collection can
160-
use. The following list describes scenarios where an index can be used:
161-
162-
- Sort on ``{ <timeField:> ±1 }`` uses the clustered index
163-
- Sort on ``{ <timeField>: ±1 }`` uses a secondary index on
164-
``<timeField>``
165-
- Sort on ``{ <metaField>: ±1, timeField: ±1 }`` uses a secondary
166-
index on ``{ <metaField>: ±1, timeField: ±1 }``
167-
- Sort on ``{ <timeField>: ±1 }`` uses a secondary index on
168-
``{ metaField: ±1, timeField: ±1 }`` when there is a point predicate
169-
on ``<metaField>``
170-
171-
Create a secondary index on the ``timestamp`` field:
161+
Create a secondary single-field index on the ``timestamp`` field:
172162

173163
.. code-block:: javascript
174164

@@ -195,13 +185,15 @@ operation again with the ``.explain( "executionStats" )`` option:
195185
] )
196186

197187

198-
"Last Point" Queries on Time Series Collections
188+
Last Point Queries on Time Series Collections
199189
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
200190

201-
A "last point" query fetches the latest measurement for each unique metadata
202-
value. For example, you may want to get the latest temperature reading from all
203-
sensors. Improve performance on last point queries by creating any of the
204-
following indexes:
191+
In time series data, a last point query returns the data point with the
192+
latest timestamp for a given field. For time series collections, a last
193+
point query fetches the latest measurement for each unique metadata
194+
value. For example, you may want to get the latest temperature reading
195+
from all sensors. Improve performance on last point queries by creating
196+
any of the following indexes:
205197

206198
.. code-block:: javascript
207199

@@ -283,19 +275,32 @@ a collection, use the :method:`db.collection.getIndexes()` method.
283275

284276
.. _timeseries-add-secondary-index-mongodb-6.0:
285277

286-
Time Series Secondary Indexes in MongoDB 6.0
287-
--------------------------------------------
278+
Time Series Secondary Indexes in MongoDB 6.0 and Later
279+
------------------------------------------------------
280+
281+
.. versionadded:: 6.3
282+
283+
Starting in MongoDB 6.3, you can create a partial :ref:`TTL
284+
index<index-feature-ttl>` for a time series collection. You can only
285+
filter on the ``metaField``.
286+
287+
If the collection doesn't use the ``expireAfterSeconds`` option to
288+
expire documents, creating a partial TTL index sets an
289+
expiration time for matching documents only. If the collection uses
290+
``expireAfterSeconds`` for all documents, a partial TTL index lets you expire matching documents sooner.
291+
292+
.. versionadded:: 6.0
288293

289294
Starting in MongoDB 6.0, you can:
290295

291296
- Add a :ref:`compound index <index-type-compound>` on the
292297
``timeField``, ``metaField``, or measurement fields.
293298

294299
- Use the :query:`$or`, :query:`$in`, and :query:`$geoWithin` operators
295-
with :doc:`partial indexes </core/index-partial>` on a time series
300+
with :ref:`partial indexes <index-type-partial>` on a time series
296301
collection.
297302

298-
- Add :doc:`partial </core/index-partial>` on the ``metaField``.
303+
- Add a partial filter expression on the ``metaField``.
299304

300305
- Add a :term:`secondary index <secondary index>` to any field or
301306
subfield.

0 commit comments

Comments
 (0)