Ruby on rails MongoDB对system.namespaces集合的冗余查询?
因此,我使用MongoDB(使用Mongoid Ruby Gem)已经有一段时间了,随着我们的应用程序的发展,我注意到随着数据的增长,请求的时间越来越长,下面是我的应用程序的典型请求的样子,但仅对于DB类的请求,它大约需要500毫秒 这里没什么特别的,只是一些控制器的东西:Ruby on rails MongoDB对system.namespaces集合的冗余查询?,ruby-on-rails,ruby,mongodb,mongoid,Ruby On Rails,Ruby,Mongodb,Mongoid,因此,我使用MongoDB(使用Mongoid Ruby Gem)已经有一段时间了,随着我们的应用程序的发展,我注意到随着数据的增长,请求的时间越来越长,下面是我的应用程序的典型请求的样子,但仅对于DB类的请求,它大约需要500毫秒 这里没什么特别的,只是一些控制器的东西: Started GET "/cities/san-francisco?date_range=past_week" for 127.0.0.1 at 2011-11-15 11:13:04 -0800 Processing
Started GET "/cities/san-francisco?date_range=past_week" for 127.0.0.1 at 2011-11-15 11:13:04 -0800
Processing by CitiesController#show as HTML
Parameters: {"date_range"=>"past_week", "id"=>"san-francisco"}
然后运行查询,但我不明白的是,对于运行的每个查询,它都会执行一个MONGODB dashboard_开发['system.namespaces']。在实际运行之前查找({})
!为什么?
MONGODB dashboard_development['system.namespaces'].find({})
MONGODB dashboard_development['users'].find({:_id=>BSON::ObjectId('4e80e0090f6d2e306f000001')})
MONGODB dashboard_development['system.namespaces'].find({})
MONGODB dashboard_development['cities'].find({:slug=>"san-francisco"})
MONGODB dashboard_development['system.namespaces'].find({})
MONGODB dashboard_development['accounts'].find({:_id=>BSON::ObjectId('4e80e0090f6d2e306f000002')})
MONGODB dashboard_development['system.namespaces'].find({})
MONGODB dashboard_development['neighborhoods'].find({"city_id"=>BSON::ObjectId('4e80e00a0f6d2e306f000005')})
然后渲染视图,它们也非常慢。。。但这是一个单独的问题,我将在另一个时间解决
Rendered cities/_title_and_scope.html.erb (109.3ms)
Rendered application/_dropdown.html.erb (0.1ms)
Rendered application/_date_range_selector.html.erb (6.2ms)
Rendered cities/show.html.erb within layouts/application (122.7ms)
Rendered application/_user_dropdown.html.erb (0.9ms)
Rendered application/_main_navigation.html.erb (5.8ms)
因此,减去浏览量,请求耗时约500毫秒,这对于一个真正简单的查询来说太长了,此外,应用程序将增长,时间也将增长。此外,此示例比通常的请求速度更快,有时需要1000毫秒或更长时间
Completed 200 OK in 628ms (Views: 144.9ms)
另外,我想问什么字段最适合索引?也许这是我的问题,因为我根本没有真正使用它们。如果您能帮助理解这一点,我们将不胜感激。谢谢 您需要使用索引——否则,您的mongo查询将执行最恰当的全表扫描。它将集合的全部json文档加载到内存中,然后对每个文档进行评估,以确定是否应将其包含在响应中
字符串、日期、数字都可以用作索引——诀窍是,在每个属性上都有一个索引,您可以对其执行“where”操作
您可以在mongo配置中关闭表扫描,以帮助查找表扫描并销毁它们