Skip to content

Commit 09870ed

Browse files
authored
Docsp 32572 v5.0 backport (#5016)
* 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-v5.0-backport adding reference
1 parent fb81bde commit 09870ed

File tree

3 files changed

+99
-2
lines changed

3 files changed

+99
-2
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: 95 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,8 @@ Syntax
3636

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

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

@@ -119,7 +121,7 @@ The operation would correspond to the following pseudo-SQL statement:
119121

120122
See these examples:
121123

122-
- :ref:`lookup-single-equality`
124+
- :ref:`lookup-single-equality-example`
123125
- :ref:`unwind-example`
124126
- :ref:`lookup-mergeObjects`
125127

@@ -443,10 +445,93 @@ Alternatively, or to join multiple sharded collections, consider:
443445
<https://docs.mongodb.com/datalake/reference/pipeline/lookup-stage/>`__
444446
pipeline stage to lookup a sharded collection.
445447

448+
.. _lookup-performance-considerations:
449+
450+
Performance Considerations
451+
~~~~~~~~~~~~~~~~~~~~~~~~~~
452+
453+
``$lookup`` performance depends on the type of operation performed.
454+
Refer to the following table for performance considerations for
455+
different ``$lookup`` operations.
456+
457+
.. list-table::
458+
:header-rows: 1
459+
:widths: 20 80
460+
461+
* - ``$lookup`` Operation
462+
- Performance Considerations
463+
464+
* - :ref:`Equality Match with a Single Join
465+
<lookup-single-equality-example>`
466+
467+
- .. _equality-match-performance:
468+
469+
- ``$lookup`` operations that perform equality matches with a
470+
single join typically perform better when the source collection
471+
contains an index on the ``foreignField``.
472+
473+
* - :ref:`Uncorrelated Subqueries<lookup-uncorrelated-subquery>`
474+
475+
- .. _uncorrelated-subqueries-performance:
476+
477+
- ``$lookup`` operations that contain uncorrelated subqueries
478+
typically perform better when the inner pipeline can reference
479+
an index on the ``foreignField``.
480+
481+
- MongoDB only needs to run the ``$lookup`` subquery once before
482+
caching the query because there is no relationship between the
483+
source and foreign collections. The ``$lookup`` subquery is not
484+
based on any value in the source collection. This behavior
485+
improves performance for subsequent executions of this query.
486+
487+
488+
* - :ref:`Correlated Subqueries <lookup-concise-correlated-subquery>`
489+
490+
- .. _correlated-subqueries-performance:
491+
492+
- ``$lookup`` operations that contain correlated subqueries
493+
typically perform better when the following conditions apply:
494+
495+
- The source collection contains an index on the
496+
``localField``.
497+
498+
- The foreign collection contains an index on the
499+
``foreignField``.
500+
501+
- The foreign collection contains an index that references the
502+
inner pipline.
503+
504+
- If your pipeline passes a large number of documents to the
505+
``$lookup`` query, the following strategies may improve
506+
performance:
507+
508+
- Reduce the number of documents that MongoDB passes to the
509+
``$lookup`` query. For example, set a stricter filter
510+
during the ``$match`` stage.
511+
512+
- Run the inner pipeline of the ``$lookup`` subquery as a
513+
separate query and use ``$out`` to create a temporary
514+
collection. Then, run an :ref:`equality match with a single
515+
join <lookup-single-equality>`.
516+
517+
- Reconsider the data's schema to ensure it is optimal for the
518+
use case.
519+
520+
For general performance strategies, see :ref:`Indexing Strategies
521+
<manual-indexing-strategies>` and :ref:`Query Optimization
522+
<read-operations-indexing>`.
523+
524+
.. important::
525+
526+
Excessive use of ``$lookup`` within a query may slow down
527+
performance. To avoid multiple ``$lookup`` stages, consider an
528+
:ref:`embedded data model <embedded-data-modeling>` to optimize query
529+
performance.
530+
446531
Examples
447532
--------
448533

449-
.. _lookup-single-equality:
534+
.. _lookup-single-equality-example:
450535

451536
Perform a Single Equality Join with ``$lookup``
452537
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
@@ -536,6 +621,9 @@ The operation corresponds to this pseudo-SQL statement:
536621
WHERE sku = orders.item
537622
);
538623

624+
For more information, see
625+
:ref:`Equality Match Performance Considerations<equality-match-performance>`.
626+
539627
.. _unwind-example:
540628

541629
Use ``$lookup`` with an Array
@@ -909,6 +997,9 @@ The operation corresponds to this pseudo-SQL statement:
909997
WHERE year = 2018
910998
);
911999

1000+
For more information, see
1001+
:ref:`Uncorrelated Subquery Performance Considerations <uncorrelated-subqueries-performance>`.
1002+
9121003
.. _lookup-concise-correlated-subquery:
9131004

9141005
Perform a Concise Correlated Subquery with ``$lookup``
@@ -1074,3 +1165,5 @@ The previous examples correspond to this pseudo-SQL statement:
10741165
AND restaurants.beverages = orders.drink
10751166
);
10761167

1168+
For more information, see
1169+
:ref:`Correlated Subquery Performance Considerations <correlated-subqueries-performance>`.

0 commit comments

Comments
 (0)