Mongo Query - number of limits versus speed (and indexing!)

Suppose I have a million records in db with 10 fields / ("columns") in db. It seems to me that the more columns I search, the faster the query is executed - for example:

db.items.find( { 
    $and: [ 
        { field1: x }, 
        { field2: y },
        { field3: z}
    ] 
} )

faster than:

db.items.find( { 
    $and: [ 
        { field1: x }, 
        { field2: y }
    ] 
} )

While I would like to say, “Great, that makes complete sense to me” - that’s not so. I just know that this is happening in my particular case and I wonder if it really is. If so, ideally, I would like to know why.

In addition, when creating indexes with multiple fields, it helps them in any order. For example, let's say I add a composite index :

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

- ? , ? , 90% 1, 1% 3. - ?

+4
2

, , 90% 1, 1% 3. , explain .

. . . , .

. { field1: 1, field2: 1, field3: 1 }
db.items.find( { field2: x, field3: y }), wount ,
db.items.find( { field1: x, field3: y }) field1.

, :
db.items.find( { field1: x, field2: y }) , db.items.find( { field2: y, field1: x }) .

, , . , , { field1: 1}, { field2: 1}, { field3: 1}, .

. .

+2

... MongoDB , , , , , .

, , . , , , , .

, , :

{ city: 1, building: 1, room: 1 }

{ city: 1, building: 1 }

, , () "{city: 1, building: 1, room: 1".

. , , , , , ( , ). B-Tree, . : http://visualgo.net/bst.html

, - , - , X.

(, ) MongoDB , - explain() , , , (COLLSCAN).

db.items.find( { 
    $and: [ 
        { field1: x }, 
        { field2: y }
    ] 
})
.explain()
+1

Source: https://habr.com/ru/post/1628569/


All Articles