@@ -426,173 +426,177 @@ in a very efficient query.
426
426
427
427
.. tabs-drivers::
428
428
429
- tabs:
430
- - id: shell
431
- content: |
432
- .. _analyze-compare-performance:
429
+ .. tab:: Shell
430
+ :tabid: shell
433
431
434
- Compare Performance of Indexes
435
- ------------------------------
432
+ .. _analyze-compare-performance:
436
433
437
- To manually compare the performance of a query using more
438
- than one index, you can use the :method:`~cursor.hint()`
439
- method in conjunction with the :method:`~cursor.explain()`
440
- method.
434
+ **Compare Performance of Indexes**
441
435
442
- Consider the following query:
436
+ To manually compare the performance of a query using more
437
+ than one index, you can use the :method:`~cursor.hint()`
438
+ method in conjunction with the :method:`~cursor.explain()`
439
+ method.
443
440
444
- .. code-block:: javascript
441
+ Consider the following query:
445
442
446
- db.inventory.find( {
447
- quantity: {
448
- $gte: 100, $lte: 300
449
- },
450
- type: "food"
451
- } )
443
+ .. code-block:: javascript
452
444
453
- The query returns the following documents:
445
+ db.inventory.find( {
446
+ quantity: {
447
+ $gte: 100, $lte: 300
448
+ },
449
+ type: "food"
450
+ } )
454
451
455
- .. code-block:: javascript
452
+ The query returns the following documents:
456
453
457
- { "_id" : 2, "item" : "f2", "type" : "food", "quantity" : 100 }
458
- { "_id" : 5, "item" : "f3", "type" : "food", "quantity" : 300 }
459
454
460
- To support the query, add a :doc:`compound index
461
- </core/index-compound>`. With :doc:`compound indexes
462
- </core/index-compound>`, the order of the fields matter.
455
+ .. code-block:: javascript
463
456
464
- For example, add the following two compound indexes. The
465
- first index orders by ``quantity`` field first, and then the
466
- ``type`` field. The second index orders by ``type`` first,
467
- and then the ``quantity`` field.
457
+ { "_id" : 2, "item" : "f2", "type" : "food", "quantity" : 100 }
458
+ { "_id" : 5, "item" : "f3", "type" : "food", "quantity" : 300 }
468
459
469
- .. code-block:: javascript
460
+ To support the query, add a :ref:`compound index
461
+ <index-type-compound>`. With compound indexes, the order
462
+ of the fields matter.
470
463
471
- db.inventory.createIndex( { quantity: 1, type: 1 } )
472
- db.inventory.createIndex( { type: 1, quantity: 1 } )
464
+ For example, add the following two compound indexes. The
465
+ first index orders by ``quantity`` field first, and then the
466
+ ``type`` field. The second index orders by ``type`` first,
467
+ and then the ``quantity`` field.
473
468
474
- Evaluate the effect of the first index on the query:
469
+ .. code-block:: javascript
475
470
476
- .. code-block:: javascript
471
+ db.inventory.createIndex( { quantity: 1, type: 1 } )
472
+ db.inventory.createIndex( { type: 1, quantity: 1 } )
477
473
478
- db.inventory.find(
479
- { quantity: { $gte: 100, $lte: 300 }, type: "food" }
480
- ).hint({ quantity: 1, type: 1 }).explain("executionStats")
474
+ Evaluate the effect of the first index on the query:
481
475
482
- The :method:`~cursor.explain()` method returns the following
483
- output:
476
+ .. code-block:: javascript
484
477
485
- .. code-block:: javascript
486
- :emphasize-lines: 22,24
478
+ db.inventory.find(
479
+ { quantity: { $gte: 100, $lte: 300 }, type: "food" }
480
+ ).hint({ quantity: 1, type: 1 }).explain("executionStats")
487
481
488
- {
489
- queryPlanner: {
490
- ...
491
- winningPlan: {
492
- queryPlan: {
493
- stage: 'FETCH',
494
- inputStage: {
495
- stage: 'IXSCAN',
496
- keyPattern: {
497
- quantity: 1,
498
- type: 1
499
- },
500
- ...
501
- }
482
+ The :method:`~cursor.explain()` method returns the following
483
+ output:
484
+
485
+ .. code-block:: javascript
486
+ :emphasize-lines: 22,24
487
+
488
+ {
489
+ queryPlanner: {
490
+ ...
491
+ winningPlan: {
492
+ queryPlan: {
493
+ stage: 'FETCH',
494
+ inputStage: {
495
+ stage: 'IXSCAN',
496
+ keyPattern: {
497
+ quantity: 1,
498
+ type: 1
499
+ },
500
+ ...
502
501
}
503
502
}
504
- },
505
- rejectedPlans: [ ]
506
- },
507
- executionStats: {
508
- executionSuccess: true,
509
- nReturned: 2,
510
- executionTimeMillis: 0,
511
- totalKeysExamined: 5,
512
- totalDocsExamined: 2,
513
- executionStages: {
514
- ...
515
503
}
516
504
},
505
+ rejectedPlans: [ ]
506
+ },
507
+ executionStats: {
508
+ executionSuccess: true,
509
+ nReturned: 2,
510
+ executionTimeMillis: 0,
511
+ totalKeysExamined: 5,
512
+ totalDocsExamined: 2,
513
+ executionStages: {
517
514
...
518
- }
515
+ }
516
+ },
517
+ ...
518
+ }
519
519
520
- MongoDB scanned ``5`` index keys
521
- (:data:`executionStats.totalKeysExamined
522
- <explain.executionStats.totalKeysExamined>`) to return ``2``
523
- matching documents (:data:`executionStats.nReturned
524
- <explain.executionStats.nReturned>`).
520
+ MongoDB scanned ``5`` index keys
521
+ (:data:`executionStats.totalKeysExamined
522
+ <explain.executionStats.totalKeysExamined>`) to return ``2``
523
+ matching documents (:data:`executionStats.nReturned
524
+ <explain.executionStats.nReturned>`).
525
525
526
- Evaluate the effect of the second index on the query:
526
+ Evaluate the effect of the second index on the query:
527
527
528
- .. code-block:: javascript
528
+ .. code-block:: javascript
529
529
530
- db.inventory.find(
531
- { quantity: { $gte: 100, $lte: 300 }, type: "food" }
532
- ).hint({ type: 1, quantity: 1 }).explain("executionStats")
530
+ db.inventory.find(
531
+ { quantity: { $gte: 100, $lte: 300 }, type: "food" }
532
+ ).hint({ type: 1, quantity: 1 }).explain("executionStats")
533
533
534
- The :method:`~cursor.explain()` method returns the following
535
- output:
534
+ The :method:`~cursor.explain()` method returns the following
535
+ output:
536
536
537
- .. code-block:: javascript
538
- :emphasize-lines: 19,21
537
+ .. code-block:: javascript
538
+ :emphasize-lines: 19,21
539
539
540
- {
541
- queryPlanner: {
542
- ...
543
- queryPlan: {
544
- winningPlan: {
545
- stage: 'FETCH',
546
- inputStage: {
547
- stage: 'IXSCAN',
548
- keyPattern: {
549
- type: 1,
550
- quantity: 1
551
- },
552
- ...
553
- }
540
+ {
541
+ queryPlanner: {
542
+ ...
543
+ queryPlan: {
544
+ winningPlan: {
545
+ stage: 'FETCH',
546
+ inputStage: {
547
+ stage: 'IXSCAN',
548
+ keyPattern: {
549
+ type: 1,
550
+ quantity: 1
551
+ },
552
+ ...
554
553
}
555
- },
556
- rejectedPlans: [ ]
557
- },
558
- executionStats: {
559
- executionSuccess: true,
560
- nReturned: 2,
561
- executionTimeMillis: 0,
562
- totalKeysExamined: 2,
563
- totalDocsExamined: 2,
564
- executionStages: {
565
- ...
566
554
}
567
555
},
568
- ...
569
- }
570
-
571
- MongoDB scanned ``2`` index keys
572
- (:data:`executionStats.totalKeysExamined
573
- <explain.executionStats.totalKeysExamined>`) to return ``2``
574
- matching documents (:data:`executionStats.nReturned
575
- <explain.executionStats.nReturned>`).
576
-
577
- The second compound index, ``{ type: 1, quantity: 1 }``, is
578
- therefore the more efficient index for supporting the example
579
- query, as the MongoDB server only needs to scan ``2``
580
- :data:`index keys <explain.executionStats.totalKeysExamined>`
581
- to find all matching documents using this index, compared to
582
- ``5`` when when using the compound index
583
- ``{ quantity: 1, type: 1 }``.
556
+ rejectedPlans: [ ]
557
+ },
558
+ executionStats: {
559
+ executionSuccess: true,
560
+ nReturned: 2,
561
+ executionTimeMillis: 0,
562
+ totalKeysExamined: 2,
563
+ totalDocsExamined: 2,
564
+ executionStages: {
565
+ ...
566
+ }
567
+ },
568
+ ...
569
+ }
570
+
571
+ MongoDB scanned ``2`` index keys
572
+ (:data:`executionStats.totalKeysExamined
573
+ <explain.executionStats.totalKeysExamined>`) to return ``2``
574
+ matching documents (:data:`executionStats.nReturned
575
+ <explain.executionStats.nReturned>`).
576
+
577
+ The second compound index, ``{ type: 1, quantity: 1 }``, is
578
+ therefore the more efficient index for supporting the example
579
+ query, as the MongoDB server only needs to scan ``2``
580
+ :data:`index keys <explain.executionStats.totalKeysExamined>`
581
+ to find all matching documents using this index, compared to
582
+ ``5`` when when using the compound index
583
+ ``{ quantity: 1, type: 1 }``.
584
+
585
+ .. seealso::
586
+
587
+ - :doc:`/core/query-optimization`
588
+ - :doc:`/core/query-plans`
589
+ - :doc:`/tutorial/optimize-query-performance-with-indexes-and-projections`
590
+ - :doc:`/applications/indexes`
591
+
592
+
593
+ .. tab:: Compass
594
+ :tabid: compass
584
595
585
- .. seealso::
596
+ .. seealso::
586
597
587
- - :doc:`/core/query-optimization`
588
- - :doc:`/core/query-plans`
589
- - :doc:`/tutorial/optimize-query-performance-with-indexes-and-projections`
590
- - :doc:`/applications/indexes`
598
+ - :ref:`Query Optimization <read-operations-indexing>`
599
+ - :ref:`MongoDB Compass Documentation <compass-index>`
600
+ - :compass:`Compass Query Plan Documentation </query-plan/>`
591
601
592
- - id: compass
593
- content: |
594
- .. seealso::
595
602
596
- - :ref:`Query Optimization <read-operations-indexing>`
597
- - :ref:`MongoDB Compass Documentation <compass-index>`
598
- - :compass:`Compass Query Plan Documentation </query-plan/>`
0 commit comments