Indexing Flashcards

1
Q

What is an index and what is its purpose?

A

An index is a B-tree of sorted field values that point to documents in the collection. Each node stores a field value that points to the actual document in the collection. Its purpose is to improve query performance

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

True or False

Without indexes, MongoDB needs to read all documents in a collection when data is queried

A

True

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

True or False

When indexes are used, MongoDB FETCHs the appropriate documents from the collection instead of performing an COLLSCAN

A

True

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

By default, what index is created when a collection is created?

Can it be deleted?

A

An _id index is created
No

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

What are the pros and cons of indexes?

A

Pros:
* Faster reads since the entire collection need not be traversed
* Faster reads since its values are sorted O(logn)

Cons:
* Slower writes since the values must also be added to the index (1 write for the actual document and 1 write for the index)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

What are the 4 different index types?

A
  1. Single-field index
  2. Single-field multikey index
  3. Compound index
  4. Compound multikey index
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

What is a single-field index?

A

A single-field index is an index with one field

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

What is a single-field multikey index?

A

A single-field multikey index is an index with one field that is an array

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

What is a compound index?

A

A compound index is an index with multiple fields

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

What is a compound multikey index?

A

A compound multikey index is an index with multiple fields and one of those fields is an array

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

True or False

When using a multikey index, each element in the array gets an index key

A

True

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

Code an example of creating a single-field index where the field is unique in mongosh

A
db.collection.createIndex({field: 1 or -1}, {unique: true})
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

Code an example of creating a single-field multikey index in mongosh

A
db.collection.createIndex({fieldArr: 1 or -1})
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

Code an example of getting the indexes in a collection

A
db.collection.getIndexes()
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

Code an example analyzing whether an index is being used for a read operation

A
db.collection.explain().find(...)
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

Describe the differences between COLLSCAN, FETCH, and IXSCAN

A
  • COLLSCAN implies a full collection scan (no indexes are used)
  • FETCH implies retrieving documents (not a full collection scan)
  • IXSCAN implies scanning index keys
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
17
Q

True or False

Multikey indexes can have multiple field arrays

A

False. Multikey indexes can only have 1 field array

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
18
Q

What are the queries that can be supported by the following compound index:

db.collection.createIndex({item: 1, location: 1, stock: 1})
A
find({item: value})
find({item: value, location: value})
find({item: value, location: value, stock: value})
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
19
Q

When creating indexes, what field order does MongoDB recommend?

A
  1. Equality fields
  2. Sort fields
  3. Range fields
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
20
Q

What is “covering a query by an index”?

A

An index covers a query when MongoDB does not need to fetch the data from memory since all the required data is already returned by the index. In most cases, we can use projections to return only the required fields and cover the query. Make sure those fields in the projection are in the index

21
Q

What is an index key?

A

An index key is the field names and their respective sorted values when creating an index. For example:

db.collection.createIndex({field1: 1, field2: 1})

Index key = {field1: 1, field2:1}

22
Q

Code an example of hiding an index

A
db.collection.hideIndex("indexName" or indexKey)
23
Q

What is the purpose of hiding an index?

A

The purpose of hiding an index is to analyze how an index affects the performance of a query

24
Q

True or False

When indexes are hidden, they will not be used for queries and they will not be updated for write operations

A

False. While hidden indexes are not used for queries, they are still updated for write operations

25
Q

Code an example of deleting an index in mongosh

A
db.collection.dropIndex("indexName" or indexKey)
26
Q

Code an example of deleting multiple indexes

A
db.collection.dropIndexes(["indexName1", "indexName2"])
27
Q

True or false

An index
covers
a query when all of the following apply:

all the fields in the query are part of an index, and

all the fields returned in the results are in the same index.

A

True

28
Q

True or False

An index can cover a query on fields within embedded documents.

A

True. For example:

{ _id: 1, user: { login: "tester" } }
// index = { "user.login": 1 }
db.userdata.find( { "user.login": "tester" }, { "user.login": 1, _id: 0 } ) // covered
29
Q

True or False

Because MongoDB can read indexes in both ascending and descending order, the direction of a single-key index does not matter

A

True

30
Q

Can index names be changed after creation?

A

No. If you wish to change the name of an index, drop it and create a new one with the new name

31
Q

Code an example of naming an index

A
db.collection.createIndex({field: value}, {name: "indexName"})
32
Q

Code an example of creating multiple indexes

A
db.collection.createIndexes([{field: value}, {field: value}])
33
Q

Create an index for the location subdocument in the following document:

{
      "name": "Bob",
      "gpa": 3.2,
      "location": { city: "Albany", state: "New York" }
}

So that the following query uses the index:

db.students.find( { location: { city: "Albany", state: "New York" } } )
A
db.students.createIndex(location: 1)
34
Q

True or False

When you create an index on an embedded document, only queries that specify the entire embedded document use the index. Queries on a specific field within the document do not use the index

A

True

35
Q

Create an index for the field in the embedded document:

{
      "location": { state: "New York" }
}
A
db.collection.createIndex({"location.state": 1})
36
Q

Code an example of the general structure of a compound index

A
db.collection.createIndex({field: value, field2: value2})
37
Q

How many fields can a compound index contain?

A

32

38
Q

What is the purpose of a multikey index?

A

The purpose of a multikey index is to improve the query performance on field arrays

39
Q

True or False

MongoDB can create multikey indexes over arrays that hold both scalar values (for example, strings and numbers) and embedded documents

A

True

40
Q

True or False

After you create the compound multikey index, if you attempt to insert a document where both fields are arrays, the insert fails

A

True

41
Q

True or False

Multikey idexes cannot cover queries on array fields

A

True

42
Q

True or False

When you create an index on a field of a document inside an array, MongoDB stores that index as a multikey index

A

True

43
Q

Code an example of unhiding an index

A
db.collection.unhideIndex("index name" or key)
44
Q

True or False

If you use the unique constraint on a compound index, then MongoDB will enforce uniqueness on the combination of the index key values

A

True

45
Q

True or False

MongoDB cannot do an index sort on the results of a range filter

A

True

46
Q

The following queries pertain to which part of the ESR rule?

db.cars.find( { price: { $gte: 15000} } )
db.cars.find( { age: { $lt: 10 } } )
db.cars.find( { priorAccidents: { $ne: null } } )
A

R = range

47
Q

Write an optimal compound index for the following query:

db.cars.find(
   {
       manufacturer: 'Ford',
       cost: { $gt: 15000 }
   } ).sort( { model: 1 } )
A
db.cars.createIndex({manufacturer: 1, model: 1, cost: 1})
48
Q

True or False

For the fastest processing, ensure that your indexes fit entirely in RAM so that the system can avoid reading the index from disk

A

True