Skip to content

Commit dc27fb3

Browse files
authored
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
1 parent 2bec27d commit dc27fb3

File tree

2 files changed

+98
-2
lines changed

2 files changed

+98
-2
lines changed

source/core/data-modeling-introduction.txt

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

70+
.. _embedded-data-modeling:
71+
7072
Embedded Data
7173
~~~~~~~~~~~~~
7274

source/reference/operator/aggregation/lookup.txt

Lines changed: 96 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,8 @@ Syntax
4545

4646
The :pipeline:`$lookup` stage has the following syntaxes:
4747

48+
.. _lookup-single-equality:
49+
4850
Equality Match with a Single Join Condition
4951
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
5052

@@ -132,7 +134,7 @@ The operation would correspond to the following pseudo-SQL statement:
132134

133135
See these examples:
134136

135-
- :ref:`lookup-single-equality`
137+
- :ref:`lookup-single-equality-example`
136138
- :ref:`unwind-example`
137139
- :ref:`lookup-mergeObjects`
138140

@@ -509,10 +511,93 @@ in the ``from`` parameter of :pipeline:`$lookup` stages.
509511

510512
For more information, see :ref:`agg-lookup-optimization-sbe`.
511513

514+
.. _lookup-performance-considerations:
515+
516+
Performance Considerations
517+
~~~~~~~~~~~~~~~~~~~~~~~~~~
518+
519+
``$lookup`` performance depends on the type of operation performed.
520+
Refer to the following table for performance considerations for
521+
different ``$lookup`` operations.
522+
523+
.. list-table::
524+
:header-rows: 1
525+
:widths: 20 80
526+
527+
* - ``$lookup`` Operation
528+
- Performance Considerations
529+
530+
* - :ref:`Equality Match with a Single Join
531+
<lookup-single-equality-example>`
532+
533+
- .. _equality-match-performance:
534+
535+
- ``$lookup`` operations that perform equality matches with a
536+
single join typically perform better when the source collection
537+
contains an index on the ``foreignField``.
538+
539+
* - :ref:`Uncorrelated Subqueries<lookup-uncorrelated-subquery>`
540+
541+
- .. _uncorrelated-subqueries-performance:
542+
543+
- ``$lookup`` operations that contain uncorrelated subqueries
544+
typically perform better when the inner pipeline can reference
545+
an index on the ``foreignField``.
546+
547+
- MongoDB only needs to run the ``$lookup`` subquery once before
548+
caching the query because there is no relationship between the
549+
source and foreign collections. The ``$lookup`` subquery is not
550+
based on any value in the source collection. This behavior
551+
improves performance for subsequent executions of this query.
552+
553+
554+
* - :ref:`Correlated Subqueries <lookup-concise-correlated-subquery>`
555+
556+
- .. _correlated-subqueries-performance:
557+
558+
- ``$lookup`` operations that contain correlated subqueries
559+
typically perform better when the following conditions apply:
560+
561+
- The source collection contains an index on the
562+
``localField``.
563+
564+
- The foreign collection contains an index on the
565+
``foreignField``.
566+
567+
- The foreign collection contains an index that references the
568+
inner pipline.
569+
570+
- If your pipeline passes a large number of documents to the
571+
``$lookup`` query, the following strategies may improve
572+
performance:
573+
574+
- Reduce the number of documents that MongoDB passes to the
575+
``$lookup`` query. For example, set a stricter filter
576+
during the ``$match`` stage.
577+
578+
- Run the inner pipeline of the ``$lookup`` subquery as a
579+
separate query and use ``$out`` to create a temporary
580+
collection. Then, run an :ref:`equality match with a single
581+
join <lookup-single-equality>`.
582+
583+
- Reconsider the data's schema to ensure it is optimal for the
584+
use case.
585+
586+
For general performance strategies, see :ref:`Indexing Strategies
587+
<manual-indexing-strategies>` and :ref:`Query Optimization
588+
<read-operations-indexing>`.
589+
590+
.. important::
591+
592+
Excessive use of ``$lookup`` within a query may slow down
593+
performance. To avoid multiple ``$lookup`` stages, consider an
594+
:ref:`embedded data model <embedded-data-modeling>` to optimize query
595+
performance.
596+
512597
Examples
513598
--------
514599

515-
.. _lookup-single-equality:
600+
.. _lookup-single-equality-example:
516601

517602
Perform a Single Equality Join with ``$lookup``
518603
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
@@ -602,6 +687,9 @@ The operation corresponds to this pseudo-SQL statement:
602687
WHERE sku = orders.item
603688
);
604689

690+
For more information, see
691+
:ref:`Equality Match Performance Considerations<equality-match-performance>`.
692+
605693
.. _unwind-example:
606694

607695
Use ``$lookup`` with an Array
@@ -975,6 +1063,9 @@ The operation corresponds to this pseudo-SQL statement:
9751063
WHERE year = 2018
9761064
);
9771065

1066+
For more information, see
1067+
:ref:`Uncorrelated Subquery Performance Considerations <uncorrelated-subqueries-performance>`.
1068+
9781069
.. _lookup-concise-correlated-subquery:
9791070

9801071
Perform a Concise Correlated Subquery with ``$lookup``
@@ -1139,3 +1230,6 @@ The previous examples correspond to this pseudo-SQL statement:
11391230
WHERE restaurants.name = orders.restaurant_name
11401231
AND restaurants.beverages = orders.drink
11411232
);
1233+
1234+
For more information, see
1235+
:ref:`Correlated Subquery Performance Considerations <correlated-subqueries-performance>`.

0 commit comments

Comments
 (0)