我正在运行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});
它使用你的名字索引,你可以在解释中看到。但是,它不仅仅是出于某种原因使用它。 – WiredPrairie