@@ -36,8 +36,10 @@ Syntax
36
36
37
37
The :pipeline:`$lookup` stage has the following syntaxes:
38
38
39
- Equality Match
40
- ~~~~~~~~~~~~~~
39
+ .. _lookup-single-equality:
40
+
41
+ Equality Match with a Single Join Condition
42
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
41
43
42
44
To perform an equality match between a field from the input documents
43
45
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:
117
119
118
120
See the following examples:
119
121
120
- - :ref:`lookup-single-equality`
122
+ - :ref:`lookup-single-equality-example `
121
123
- :ref:`unwind-example`
122
124
- :ref:`lookup-mergeObjects`
123
125
@@ -219,7 +221,7 @@ The operation would correspond to the following pseudo-SQL statement:
219
221
See the following examples:
220
222
221
223
- :ref:`lookup-multiple-joins`
222
- - :ref:`lookup-uncorrelated-subuery `
224
+ - :ref:`lookup-uncorrelated-subquery `
223
225
224
226
Consideration
225
227
-------------
@@ -297,10 +299,60 @@ Alternatively, or to join multiple sharded collections, consider:
297
299
<https://docs.mongodb.com/datalake/reference/pipeline/lookup-stage/>`__
298
300
pipeline stage to lookup a sharded collection.
299
301
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
+
300
352
Examples
301
353
--------
302
354
303
- .. _lookup-single-equality:
355
+ .. _lookup-single-equality-example :
304
356
305
357
Perform a Single Equality Join with ``$lookup``
306
358
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
@@ -388,6 +440,12 @@ The operation would correspond to the following pseudo-SQL statement:
388
440
FROM inventory
389
441
WHERE sku= orders.item);
390
442
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
+
391
449
.. _unwind-example:
392
450
393
451
Use ``$lookup`` with an Array
@@ -617,7 +675,7 @@ collection:
617
675
- :query:`$expr`
618
676
- :doc:`/reference/aggregation-variables/`.
619
677
620
- .. _lookup-uncorrelated-subuery :
678
+ .. _lookup-uncorrelated-subquery :
621
679
622
680
Uncorrelated Subquery
623
681
~~~~~~~~~~~~~~~~~~~~~
0 commit comments