Skip to content

Commit 4150317

Browse files
authored
DOCSP 32572 - adding performance and considerations to $lookup (#4771) (#5020)
* DOCSP 32572 - adding performance and considerations to $lookup (#4771) * DOCSP-32572 adding performance and considerations to lookup * DOCSP-32572 adding performance and considerations to lookup * DOCSP-32572 formatting errors * DOCSP-32572 performance and considerations for lookup * DOCSP-32572 performance and considerations for lookup * DOCSP-32572 performance and considerations for lookup * DOCSP-32572 performance and considerations for lookup * DOCSP-32572 performance and considerations for lookup * DOCSP-32572 performance and considerations for lookup * DOCSP-32572 performance and considerations for lookup * DOCSP-32572 performance and considerations for lookup * DOCSP-32572 performance and considerations for lookup * DOCSP-32572 performance and considerations for lookup * DOCSP-32572 fixing bullet spacing * DOCSP-32572 adding general strategies * DOCSP-32572 fixing spacing * DOCSP-32572 adding embedded data modeling reference * DOCSP-32572 adding embedded data modeling reference * DOCSP-32572 copy edits from Jeff * DOCSP-32572 copy edits from Jeff * DOCSP-32572 copy edits from Jeff * DOCSP-32572 copy edits from Jeff * DOCSP-32572 copy edits round 2 * DOCSP-32572 copy edits round 2 * DOCSP-32572 copy edits round 2 * DOCSP-32572 copy edits round 2 * DOCSP-32572 copy edits round 2 * DOCSP-32572 copy edits round 2 * DOCSP-32572 tech edit * DOCSP-32572 fixing line * DOCSP-32572 fixing line * DOCSP-32572 tech edit * DOCSP-32572-v4.4-backport build errors * DOCSP-32572-v4.4-backport build errors * DOCSP-32572-v4.4-backport build errors
1 parent a704380 commit 4150317

File tree

3 files changed

+68
-6
lines changed

3 files changed

+68
-6
lines changed

source/applications/indexes.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
.. _manual-indexing-strategies:
2+
13
===================
24
Indexing Strategies
35
===================

source/core/data-modeling-introduction.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,8 @@ revolves around the structure of documents and how the application
6060
represents relationships between data. MongoDB allows related data to
6161
be embedded within a single document.
6262

63+
.. _embedded-data-modeling:
64+
6365
Embedded Data
6466
~~~~~~~~~~~~~
6567

source/reference/operator/aggregation/lookup.txt

Lines changed: 64 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,10 @@ Syntax
3636

3737
The :pipeline:`$lookup` stage has the following syntaxes:
3838

39-
Equality Match
40-
~~~~~~~~~~~~~~
39+
.. _lookup-single-equality:
40+
41+
Equality Match with a Single Join Condition
42+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
4143

4244
To perform an equality match between a field from the input documents
4345
with a field from the documents of the "joined" collection, the
@@ -117,7 +119,7 @@ The operation would correspond to the following pseudo-SQL statement:
117119

118120
See the following examples:
119121

120-
- :ref:`lookup-single-equality`
122+
- :ref:`lookup-single-equality-example`
121123
- :ref:`unwind-example`
122124
- :ref:`lookup-mergeObjects`
123125

@@ -219,7 +221,7 @@ The operation would correspond to the following pseudo-SQL statement:
219221
See the following examples:
220222

221223
- :ref:`lookup-multiple-joins`
222-
- :ref:`lookup-uncorrelated-subuery`
224+
- :ref:`lookup-uncorrelated-subquery`
223225

224226
Consideration
225227
-------------
@@ -297,10 +299,60 @@ Alternatively, or to join multiple sharded collections, consider:
297299
<https://docs.mongodb.com/datalake/reference/pipeline/lookup-stage/>`__
298300
pipeline stage to lookup a sharded collection.
299301

302+
.. _lookup-performance-considerations:
303+
304+
Performance Considerations
305+
~~~~~~~~~~~~~~~~~~~~~~~~~~
306+
307+
``$lookup`` performance depends on the type of operation performed.
308+
Refer to the following table for performance considerations for
309+
different ``$lookup`` operations.
310+
311+
.. list-table::
312+
:header-rows: 1
313+
:widths: 20 80
314+
315+
* - ``$lookup`` Operation
316+
- Performance Considerations
317+
318+
* - :ref:`Equality Match with a Single Join
319+
<lookup-single-equality-example>`
320+
321+
- .. _equality-match-performance:
322+
323+
- ``$lookup`` operations that perform equality matches with a
324+
single join typically perform better when the source collection
325+
contains an index on the ``foreignField``.
326+
327+
* - :ref:`Uncorrelated Subqueries <lookup-uncorrelated-subquery>`
328+
329+
- .. _uncorrelated-subqueries-performance:
330+
331+
- ``$lookup`` operations that contain uncorrelated subqueries
332+
typically perform better when the inner pipeline can reference
333+
an index on the ``foreignField``.
334+
335+
- MongoDB only needs to run the ``$lookup`` subquery once before
336+
caching the query because there is no relationship between the
337+
source and foreign collections. The ``$lookup`` subquery is not
338+
based on any value in the source collection. This behavior
339+
improves performance for subsequent executions of this query.
340+
341+
For general performance strategies, see :ref:`Indexing Strategies
342+
<manual-indexing-strategies>` and :ref:`Query Optimization
343+
<read-operations-indexing>`.
344+
345+
.. important::
346+
347+
Excessive use of ``$lookup`` within a query may slow down
348+
performance. To avoid multiple ``$lookup`` stages, consider an
349+
:ref:`embedded data model <embedded-data-modeling>` to optimize query
350+
performance.
351+
300352
Examples
301353
--------
302354

303-
.. _lookup-single-equality:
355+
.. _lookup-single-equality-example:
304356

305357
Perform a Single Equality Join with ``$lookup``
306358
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
@@ -388,6 +440,12 @@ The operation would correspond to the following pseudo-SQL statement:
388440
FROM inventory
389441
WHERE sku= orders.item);
390442

443+
For more information, see
444+
:ref:`Equality Match Performance Considerations<equality-match-performance>`.
445+
446+
For more information, see
447+
:ref:`Equality Match Performance Considerations<equality-match-performance>`.
448+
391449
.. _unwind-example:
392450

393451
Use ``$lookup`` with an Array
@@ -617,7 +675,7 @@ collection:
617675
- :query:`$expr`
618676
- :doc:`/reference/aggregation-variables/`.
619677

620-
.. _lookup-uncorrelated-subuery:
678+
.. _lookup-uncorrelated-subquery:
621679

622680
Uncorrelated Subquery
623681
~~~~~~~~~~~~~~~~~~~~~

0 commit comments

Comments
 (0)