|
36 | 36 |
|
37 | 37 | The :pipeline:`$lookup` stage has the following syntaxes:
|
38 | 38 |
|
| 39 | +.. _lookup-single-equality: |
| 40 | + |
39 | 41 | Equality Match with a Single Join Condition
|
40 | 42 | ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
41 | 43 |
|
@@ -119,7 +121,7 @@ The operation would correspond to the following pseudo-SQL statement:
|
119 | 121 |
|
120 | 122 | See these examples:
|
121 | 123 |
|
122 |
| -- :ref:`lookup-single-equality` |
| 124 | +- :ref:`lookup-single-equality-example` |
123 | 125 | - :ref:`unwind-example`
|
124 | 126 | - :ref:`lookup-mergeObjects`
|
125 | 127 |
|
@@ -443,10 +445,93 @@ Alternatively, or to join multiple sharded collections, consider:
|
443 | 445 | <https://docs.mongodb.com/datalake/reference/pipeline/lookup-stage/>`__
|
444 | 446 | pipeline stage to lookup a sharded collection.
|
445 | 447 |
|
| 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 | + |
446 | 531 | Examples
|
447 | 532 | --------
|
448 | 533 |
|
449 |
| -.. _lookup-single-equality: |
| 534 | +.. _lookup-single-equality-example: |
450 | 535 |
|
451 | 536 | Perform a Single Equality Join with ``$lookup``
|
452 | 537 | ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
@@ -536,6 +621,9 @@ The operation corresponds to this pseudo-SQL statement:
|
536 | 621 | WHERE sku = orders.item
|
537 | 622 | );
|
538 | 623 |
|
| 624 | +For more information, see |
| 625 | +:ref:`Equality Match Performance Considerations<equality-match-performance>`. |
| 626 | + |
539 | 627 | .. _unwind-example:
|
540 | 628 |
|
541 | 629 | Use ``$lookup`` with an Array
|
@@ -909,6 +997,9 @@ The operation corresponds to this pseudo-SQL statement:
|
909 | 997 | WHERE year = 2018
|
910 | 998 | );
|
911 | 999 |
|
| 1000 | +For more information, see |
| 1001 | +:ref:`Uncorrelated Subquery Performance Considerations <uncorrelated-subqueries-performance>`. |
| 1002 | + |
912 | 1003 | .. _lookup-concise-correlated-subquery:
|
913 | 1004 |
|
914 | 1005 | Perform a Concise Correlated Subquery with ``$lookup``
|
@@ -1074,3 +1165,5 @@ The previous examples correspond to this pseudo-SQL statement:
|
1074 | 1165 | AND restaurants.beverages = orders.drink
|
1075 | 1166 | );
|
1076 | 1167 |
|
| 1168 | +For more information, see |
| 1169 | +:ref:`Correlated Subquery Performance Considerations <correlated-subqueries-performance>`. |
0 commit comments