提高MongoDB查询的性能

时间:2013-07-02 18:25:13

标签: java mongodb optimization mongodb-java

我有一个4GB的mongodb数据库,其中包含ip到地理数据。数据如下所示:

{ "_id" : ObjectId("51d1c15d3a673a1a57f20251"), "IP_FROM" : NumberLong(1168612352), "IP_TO" : NumberLong(1168612607), "COUNTRY_CODE" : "US", "COUNTRY_NAME" : "UNITED STATES", "REGION" : "MICHIGAN", "CITY" : "LANSING", "LATITUDE" : 42.73328, "LONGITUDE" : -84.637765 }

我有查询数据库的java代码:

BasicDBObject query = new BasicDBObject("IP_TO", new BasicDBObject("$gte", ipNum));

DBCursor cursor = ipCollection.find(query).limit(1);
    try {
        while (cursor.hasNext()) {
            DBObject next = cursor.next();
            latitude = (Double) next.get("LATITUDE");
            longitude = (Double) next.get("LONGITUDE");
            System.out.println("LATITUTDE: " +latitude+" LONGITUDE: "+ longitude);
            break;
        }
    } finally {
        cursor.close();
    }

当我运行db.currentOp时,我得到:

"query" : {
            "IP_TO" : {
                "$gte" : NumberLong(1625798904)
            }
        }

这太长了。有没有办法加快这个查询?

1 个答案:

答案 0 :(得分:0)

IP_TO索引和IP_FROM可以提供帮助