5

I have 1.5 million of documents in a collection with an indexed "name" field. Query like db.things.find({name: /^foo/i}) takes about of 5 secs which is very slow. Similar MySQL table with the same records performs SELECT * FROM things WHERE name LIKE 'foo%' in less then 10 ms.

The mongo's explain:

db.things.find({name: /^foo/i}).limit(10).explain()
{
    "cursor" : "BtreeCursor name_1 multi",
    "nscanned" : 325730,
    "nscannedObjects" : 10,
    "n" : 10,
    "millis" : 4758,
    "nYields" : 89,
    "nChunkSkips" : 0,
    "isMultiKey" : false,
    "indexOnly" : false,
    "indexBounds" : {
        "name" : [
            [
                "",
                {

                }
            ],
            [
                /^foo/i,
                /^foo/i
            ]
        ]
    }
}

So is regexp query that slow in mongo or am I doing it wrong?

1 Answer 1

4

case insensitive regex search will be slow as it cannot take advantage of the index effectively. If you only use the field for searching, you should consider storing text in all lowercase form and search with case-sensitive regex.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.