Sergiu Velescu
Sergiu Velescu

Reputation: 514

Create an index for specific query

could you please help me to solve following problem:

I have a query which scans a collection to find the result, I would like to create an index (or maybe something else) in order to improve the execution speed.

Here is the SQL equivalent of the query

active=true 
AND 
exclude_from_search=false 
AND 
(
    full_name_lc like '%buttor% 
    OR 
    user_name_lc like '%button%' 
    OR 
    first_name_lc like '%button%' 
    OR last_name_lc like '%button%'
) 
AND 
group !='Star'

Here is the MongoDB query:

db.user.find({ 
    "active":true,
    "exclude_from_search":false,
    $or:[
        {"full_name_lc":{$regex:"button"}},
        {"user_name_lc":{$regex:"button"}},
        {"first_name_lc":{$regex:"button"}},
        {"last_name_lc":{$regex:"button"}}
    ],
    "group":{$ne:"Star"}
})

Thanks you in advance.

Upvotes: 0

Views: 44

Answers (2)

Sergiu Velescu
Sergiu Velescu

Reputation: 514

This is a well known problem - in case you use wild-cast search on both sides (... like '%some_string%') you have no other choose than FULL TABLE SCAN (this is a well known problem - you can not create an index to be used in this cases, you will have to rethink the logic)

Upvotes: 0

Wizard
Wizard

Reputation: 4431

Perhaps making a compound index is enough.

db.user.ensureIndex({active : 1, exclude_from_search : 1, group : 1}, {name : "aeg"});

db.user.find({ 
    "active":true,
    "exclude_from_search":false,
    $or:[
        {"full_name_lc":{$regex:"button"}},
        {"user_name_lc":{$regex:"button"}},
        {"first_name_lc":{$regex:"button"}},
        {"last_name_lc":{$regex:"button"}}
    ],
    "group":{$ne:"Star"}
}).explain();

{
    "cursor" : "BtreeCursor aeg",
    "isMultiKey" : false,
    "n" : 0,
    "nscannedObjects" : 0,
    "nscanned" : 0,
    "nscannedObjectsAllPlans" : 0,
    "nscannedAllPlans" : 0,
    "scanAndOrder" : false,
    "indexOnly" : false,
    "nYields" : 0,
    "nChunkSkips" : 0,
    "millis" : 0,
    "indexBounds" : {
            "active" : [
                    [
                            true,
                            true
                    ]
            ],
            "exclude_from_search" : [
                    [
                            false,
                            false
                    ]
            ],
            "group" : [
                    [
                            {
                                    "$minElement" : 1
                            },
                            "Star"
                    ],
                    [
                            "Star",
                            {
                                    "$maxElement" : 1
                            }
                    ]
            ]
    },
    "server" : "xxx",
    "filterSet" : false
}

Upvotes: 1

Related Questions