|
45 | 45 |
|
46 | 46 | The :pipeline:`$lookup` stage has the following syntaxes: |
47 | 47 |
|
| 48 | +.. _lookup-single-equality: |
| 49 | + |
48 | 50 | Equality Match with a Single Join Condition |
49 | 51 | ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
50 | 52 |
|
@@ -132,7 +134,7 @@ The operation would correspond to the following pseudo-SQL statement: |
132 | 134 |
|
133 | 135 | See these examples: |
134 | 136 |
|
135 | | -- :ref:`lookup-single-equality` |
| 137 | +- :ref:`lookup-single-equality-example` |
136 | 138 | - :ref:`unwind-example` |
137 | 139 | - :ref:`lookup-mergeObjects` |
138 | 140 |
|
@@ -509,10 +511,93 @@ in the ``from`` parameter of :pipeline:`$lookup` stages. |
509 | 511 |
|
510 | 512 | For more information, see :ref:`agg-lookup-optimization-sbe`. |
511 | 513 |
|
| 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 | + |
512 | 597 | Examples |
513 | 598 | -------- |
514 | 599 |
|
515 | | -.. _lookup-single-equality: |
| 600 | +.. _lookup-single-equality-example: |
516 | 601 |
|
517 | 602 | Perform a Single Equality Join with ``$lookup`` |
518 | 603 | ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
@@ -602,6 +687,9 @@ The operation corresponds to this pseudo-SQL statement: |
602 | 687 | WHERE sku = orders.item |
603 | 688 | ); |
604 | 689 |
|
| 690 | +For more information, see |
| 691 | +:ref:`Equality Match Performance Considerations<equality-match-performance>`. |
| 692 | + |
605 | 693 | .. _unwind-example: |
606 | 694 |
|
607 | 695 | Use ``$lookup`` with an Array |
@@ -975,6 +1063,9 @@ The operation corresponds to this pseudo-SQL statement: |
975 | 1063 | WHERE year = 2018 |
976 | 1064 | ); |
977 | 1065 |
|
| 1066 | +For more information, see |
| 1067 | +:ref:`Uncorrelated Subquery Performance Considerations <uncorrelated-subqueries-performance>`. |
| 1068 | + |
978 | 1069 | .. _lookup-concise-correlated-subquery: |
979 | 1070 |
|
980 | 1071 | Perform a Concise Correlated Subquery with ``$lookup`` |
@@ -1139,3 +1230,6 @@ The previous examples correspond to this pseudo-SQL statement: |
1139 | 1230 | WHERE restaurants.name = orders.restaurant_name |
1140 | 1231 | AND restaurants.beverages = orders.drink |
1141 | 1232 | ); |
| 1233 | + |
| 1234 | +For more information, see |
| 1235 | +:ref:`Correlated Subquery Performance Considerations <correlated-subqueries-performance>`. |
0 commit comments