Skip to content
engineering workbench.by StudyPort Academy
Learning path
Advanced18 min

Indexes and query performance

Understand the idea

A query can return the right document and still read every other document to find it.

The idea

The filter is { topic: 'indexes' } against 300 documents, one of which matches. The first run has no . explain reports COLLSCAN and totalDocsExamined 300. The second run creates an index on topic, then runs the same . The plan uses IXSCAN and examines one key. findOne with a projection of title and no _id returns a document whose only key is title. Projection avoids over-fetching. It is not the thing that stopped the scan.

What changes

Same filter, two plans
RunPlanWhat was examined
No indexCOLLSCAN300 documents
Index on topicIXSCAN1 key
Projection of titleDoes not choose the planThe fields after the match

The command

Unindexed filter

3s timeout · one stateless run. Node and Express use Piston when that instance accepts the job (public EMKC execute is whitelist-only; set PISTON_URL or PISTON_TOKEN). Otherwise a local Node process. MongoDB uses a fresh in-memory server per run, then it is stopped.

Indexed filter and projection

3s timeout · one stateless run. Node and Express use Piston when that instance accepts the job (public EMKC execute is whitelist-only; set PISTON_URL or PISTON_TOKEN). Otherwise a local Node process. MongoDB uses a fresh in-memory server per run, then it is stopped.

The mistake

Check the result

The unindexed run prints COLLSCAN 300. The indexed run prints IXSCAN 1, then title. Both runs insert their own 300 documents into a new database.

Guided challenge

Explain the indexes topic filter

The explain runs with topic missing, so no document matches. Change the filter to topic indexes so you see COLLSCAN 300 on the seeded collection.

Explain the indexes topic filter

3s timeout · one stateless run. Node and Express use Piston when that instance accepts the job (public EMKC execute is whitelist-only; set PISTON_URL or PISTON_TOKEN). Otherwise a local Node process. MongoDB uses a fresh in-memory server per run, then it is stopped.

EXPECTED RESULT

COLLSCAN 300

Hints 0/2

    Use all hints or make 3 failed checks (0/3).

    Free exploration

    Change this example and run it again. Each run is a new process. Nothing is saved.

    Your variation

    3s timeout · one stateless run. Node and Express use Piston when that instance accepts the job (public EMKC execute is whitelist-only; set PISTON_URL or PISTON_TOKEN). Otherwise a local Node process. MongoDB uses a fresh in-memory server per run, then it is stopped.

    Quiz

    A quick check for understanding. Retry as often as you like; your learning path stays open.

    01What does a collection scan examine?
    02What does projection change?

    Ready to call this one understood?

    Mark this lesson complete to earn 200 XP. You can always revisit it.

    Sign in to save progress
    Next lesson