2013-08-27 49 views
0

我正在运行Rails应用程序并使用mongoid。我在我的Rails应用中创建了一个帐号为Mongoid::Document
账号文件中有大量记录,我加了复合索引来加快查询速度。但应用程序的性能很差。
我试着做查询的解释,但indexOnly属性显示为false的值。如何使用MongoDB查询和索引更快获得结果

任何人都可以请让我知道什么是创建复合索引的最佳方式。
如何检查我写的查询是否正确使用索引?

这里是我的查询

first_record = Account.where(column_1: 5, column_2: "xxxx", column_3: "xxxxxx", column_4: "xxxxxxxxxxx", column_5: "xxxxxxxxx")

first_record.explain() => {"cursor"=>"BtreeCursor provider_5_params_idx", "isMultiKey"=>false, "n"=>10320, "nscannedObjects"=>10320, "nscanned"=>10320, "nscannedObjectsAllPlans"=>10320, "nscannedAllPlans"=>10320, "scanAndOrder"=>false, "indexOnly"=>false, "nYields"=>317, "nChunkSkips"=>0, "millis"=>222464, "indexBounds"=>{"column_1"=>[[5, 5]], "column_2"=>[["xxxx", "xxxx"]], "column_3"=>[["xxxxx", "xxxxx"]], "column_4"=>[["pxxxx", "xxxxxxx"]], "column_5"=>[["xxxxxx", "xxxxxx"]]}, "allPlans"=>[{"cursor"=>"BtreeCursor provider_5_params_idx", "n"=>10320, "nscannedObjects"=>10320, "nscanned"=>10320, "indexBounds"=>{"column_1"=>[[5, 5]], "column_2"=>[["xxxx", "xxxx"]], "column_3"=>[["xxxx", "xxxx"]], "column_4"=>[["xxxx", "xxxxx"]], "column_5"=>[["xxxxx", "xxxxx"]]}}], "oldPlan"=>{"cursor"=>"BtreeCursor provider_5_params_idx", "indexBounds"=>{"column_1"=>[[5, 5]], "column_2"=>[["xxxx", "xxxx"]], "column_3"=>[["xxxxx", "xxxxx"]], "column_4"=>[["xxxx", "xxxxx"]], "column_5"=>[["xxxxx", "xxxxx"]]}}, "server"=>"xxxxxxxxx"}

而且我已经创建的索引使用以下方法。
db.account.ensureIndex({column_1:1,column_2:1,column_3:1,column_4:1,column_5:1}, {name:"provider_5_params_idx",background:true});

+0

它使用你的名字索引,你可以在解释中看到。但是,它不仅仅是出于某种原因使用它。 – WiredPrairie

回答

0

这是一个错误,你索引column_1两次?

与计划类似,10320对象被扫描的事实是索引不起作用。

+0

是的,我在评论中写了错误的索引名称,但现在我编辑了文本。 – AnitaB

相关问题