2014-11-12 2 views
0

이 특정 컬렉션에는 300,000 개의 문서가 있습니다. 각 문서는 택시로 간주됩니다. 각 문서에는 TaxiStation 번호와 라이센스 번호가 들어 있습니다.Mongodb에 대한 쿼리를 최적화하는 방법

내 목표는 TaxiStation 당 TaxiLicense 당 여행 수를 계산하는 것입니다.
예 :
TaxiStation A 라이센스 X에는 5 번의 여행이있었습니다.
TaxiStation A 면허 Y가 9 번 여행했습니다. 등등.

검색어를 어떻게 최적화 할 수 있습니까? 완료하는 데 30 분 이상의 시간이 필요합니다!

List /*of*/ taxistationOfCollection, taxiLicenseOfTaxistation; 
     //Here I get all the distinct TaxiStation numbers in the collection 
     taxistationOfCollection = coll.distinct("TaxiStation"); 

     BasicDBObject query, tripquery; 
     int tripcount; 

     //Now I have to loop through each Taxi Station 
     for(int i = 0; i<taxistationOfCollection.size(); i++) 
     { 
      query = new BasicDBObject("TaxiStation", taxistationOfCollection.get(i)); 
      //Here, I make a list of each distinct Taxi License in the current Taxi station 
      taxiLicenseOfTaxistation = coll.distinct("TaxiLicense", query); 

      //Now I make a loop to process each Taxi License within the current Taxi station 
      for(int k = 0; k<taxiLicenseOfTaxistation.size();k++) 
      { 
       tripcount=0; 
       if(taxiLicenseOfTaxistation.get(k) !=null) 
       { 
        //I'm looking for each Taxi Station with this Taxi License 
        tripquery= new BasicDBObject("TaxiStation", taxistationOfCollection.get(i)).append("TaxiLicense", taxiLicenseOfTaxistation.get(k)); 
        DBCursor cursor = coll.find(tripquery); 

        try { 
         while(cursor.hasNext()) { 
          //Increasing my counter everytime I find a match 
          tripcount++; 
          cursor.next(); 
         } 
        } finally { 
         //Finally printing the results 
         System.out.println("Station: " + taxistationOfCollection.get(i) + " License:" + taxiLicenseOfTaxistation.get(k) 
           + " Trips: " + tripcount); 
        } 



       } 
      } 
     } 

샘플 문서 :

{ 
    "_id" : ObjectId("53df46ed9b2ed78fb7ca4f23"), 
    "Version" : "2", 
    "Display" : [], 
    "Generated" : "2014-08-04,16:40:05", 
    "GetOff" : "2014-08-04,16:40:05", 
    "GetOffCellInfo" : "46001,43027,11237298", 
    "Undisplay" : [], 
    "TaxiStation" : "0000", 
    "GetOn" : "2014-08-04,16:40:03", 
    "GetOnCellInfo" : "46001,43027,11237298", 
    "TaxiLicense" : "000000", 
    "TUID" : "26921876-3bd5-432e-a014-df0fb26c0e6c", 
    "IMSI" : "460018571356892", 
    "MCU" : "CM8001MA121225V1", 
    "System_ID" : "000", 
    "MeterGetOffTime" : "", 
    "MeterGetOnTime" : "", 
    "Setup" : [], 
    "MeterSID" : "", 
    "MeterWaitTime" : "", 
    "OS" : "4.2", 
    "PackageVersion" : "201407300888", 
    "PublishVersion" : "201312060943", 
    "SWVersion" : "rel_touchbox_20101010", 
    "MeterMile" : 0, 
    "MeterCharged" : 0, 
    "GetOnLongitude" : 0, 
    "GetOnLatitude" : 0, 
    "GetOffLongitude" : 0, 
    "TripLength" : 2, 
    "GetOffLatitude" : 0, 
    "Clicks" : 0, 
    "updateTime" : "2014-08-04 16:40:10" 
} 
+0

샘플 문서를 게시 할 수 있습니까? 쿼리를 통해 다시 그 문제를 해결하기가 어렵습니다. – Trudbert

+0

@ 트루 버트 그래! 나는 find (tripquery)를 어떻게 사용하는지에 관해서는 비효율적이라는 것을 알고 있지만, 나는 그 문제를 어떻게 해결할지는 모르겠다. – krikara

답변

2

Aggregation 당신이 찾고있는 아마. 집계 연산을 사용하면 전체 코드가 데이터베이스에서 실행되며 몇 줄에서 수행 할 수 있습니다. 데이터베이스는 인덱스와 다른 것들을 최대한 활용할 수있는 모든 작업을 처리하므로 성능 또한 훨씬 좋아야합니다.

내가 게시 한 내용에서이 메시지는 simple $group operation으로 귀결됩니다.

DBObject taxigroup = new BasicDBObject("$group", 
           new BasicDBObject("_id", 
            new BasicDBObject("station","$TaxiStation") 
            .append("Licence","$TaxiLicense")) 
           .append("count", new BasicDBObject("$sum",1))); 
AggregationOutput aggout = taxistationOfCollection.aggregate(
                 Arrays.asList(taxigroup)); 

코드가 있습니다 :

db.taxistationOfCollection.aggregate([ 
         {$group: 
          { _id: 
            {station: "$TaxiStation", 
            licence: "$TaxiLicense"}, 
           count : {$sum : 1} 
          } 
         ]) 

이 당신에게 자바의 경우

{_id : {station: stationid, licence: licence_number}, count: number_of_documents} 

그것과 같을 것이다 형태의 문서를 제공합니다 : 쉘에서이 같을 것이다 스 니펫은 테스트되지 않습니다.

+0

몇 가지 이유로 마지막 부분에서 오류가 발생합니다. 집계 목록 (목록 )은 유형 목록 – krikara

+0

에 대해 정의되지 않았습니다. 사용중인 드라이버 버전은 무엇입니까? 관련 문서 http://api.mongodb.org/java/current/com/mongodb/DBCollection.html#aggregate(java.util.List) – Trudbert

+0

여기에 같은 명명 충돌이있을 수 있습니다 : http : // stackoverflow .com/questions/9914873/play-2-0-java-cant-pass-list-into-template? – Trudbert

관련 문제