2016-11-29 9 views
1

私はこの巨大な文書を持っているとしましょう。インデックスmongodbを正しく作成するには?

このうち2人はこのオブジェクト配列を持っています。

{ 
    status: "A", 
    group: "public", 
    "created.dt": .... 
} 

{ 
    status: "A", 
    group: "private", 
    "created.dt": .... 
} 

私はインデックスを作成し、次のように確認してください。

db.collection.ensureIndex({"created.dt":-1}); 

db.collection.ensureIndex({"created.dt":-1, "status":1}); 
db.collection.ensureIndex({"created.dt":-1, "group":1}); 

db.collection.ensureIndex({"created.dt":-1, "status":1, "group":1}); 

問合せ:

db.collection.find(
{ 
    "status": { 
    $in: ["A", "I"] 
    }, 
    "asset_group": "public" 
}, 
{ 
    sort: { 
    'created.dt':1 
    } 
} 
).count(); 

は、それが間違っていますか?

このインデックスはまだ遅いです。 は、次のクエリのために適切なindex.thankあなた

+0

あなたの質問が表示されないと誰も気付くことができません(例:検索、並べ替えなど) –

+0

@FirdausRamlan大変、私はその投稿を編集しました。 –

答えて

5

私を助けてください:

db.collection.find(
{ 
    "status": { 
    $in: ["A", "I"] 
    }, 
    "asset_group": "public" 
}, 
{ 
    sort: { 
    'created.dt':1 
    } 
} 
).count(); 

最良の指標はこのようになります。あなたがいるので

db.collection.ensureIndex({"status":1, "asset_group":1, "created.dt":1}); 

または

db.collection.ensureIndex({"asset_group":1, "status":1, "created.dt":-1}); 

で質問しています

statusasset_group - これらの値は、インデックスの接頭辞

とソートにcreated.dtフィールドに切り替えることができます - のでcreated.atは、インデックスの接頭辞の最後の値であることがshuold。注:ソートでは、インデックスは逆順に移動できます。

他のクエリでは、他のインデックスが適している可能性があります。 compound indexesについてさらに読む

+0

よろしくお願いします!あなたに説明をありがとう... –

関連する問題