4

How to improve the performance of the simple search query mongo

 3 years ago
source link: https://www.codesd.com/item/how-to-improve-the-performance-of-the-simple-search-query-mongo.html
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

How to improve the performance of the simple search query mongo

advertisements

This is explanation of a simple search in mongodb, its taking more than 2.4 secs and more to retrieve data.If i add index(search params) it takes more than 5 secs.

Query

    db.CX_EMPLOYEES.find({ "$or" : [{ "AML_FULLNAME" : /RAJ/ },
{ "AML_FULLALIAS" : /RAJ/ }] })

Explain

 {
        "cursor" : "BasicCursor",
        "isMultiKey" : false,
        "n" : 79,
        "nscannedObjects" : 504570,
        "nscanned" : 504570,
        "nscannedObjectsAllPlans" : 504570,
        "nscannedAllPlans" : 504570,
        "scanAndOrder" : false,
        "indexOnly" : false,
        "nYields" : 0,
        "nChunkSkips" : 0,
        "millis" : 2423,
        "indexBounds" : {},
        "server" : "SERVER:27017"
    }


Scheduled for the 2.6 version of MongoDb is a full text search feature. It's available as a development preview in current builds if enabled.

Given the nature of your query, it's likely to be the only option that might be effective using only MongoDb. As you're trying to do a "string contains" search according to the regular expressions you provided, the performance of doing a search to match strings on multiple fields will be terrible given the size of your collection. While it's a simple query in concept, translation to an efficient query is very difficult. Mongo needs to scan every document for a match. Breaking the words apart doesn't help, as Mongo still needs to scan every document.

If you can anchor the regular expression which means it would change to a "string starts with" rather than "string contains", the performance should be reasonable if you normalize the strings so that all character case is ignored, and realizing that matches will be exact still. For example, a is not á and would need to be specially handled.

Mongo's support for this type of query really are limited for production use. You may find that the full text search feature isn't suitable either. If this query is important, I'd suggest considering alternative search mechanisms. Possibly looking at something like Elastic Search for example.

Tags mongodb

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK