我们最终必须对数据库索引进行严格的微调,确保我们的查询不会做出非常愚蠢的事情,确保我们不会执行超过绝对必要的查询,等等。当我说“非常愚蠢的事情”,我的意思是1 + N 查询问题..。
# 1 query
Dog.find(:all).each do |dog|
# N queries
dog.owner.siblings.each do |sibling|
# N queries per above N query!
sibling.pets.each do |pet|
# Do something here
end
end
end
DataMapper 是处理上述问题的一种非常好的方法(这里是 没有1 + N 的问题) ,但是更好的方法是动动脑子,停止这样的查询。当您需要原始性能时,大多数 ORM 层不能轻松处理极其自定义的查询,所以您不妨手工编写它们。
We also did common sense things. We bought a beefy server for our growing database, and moved it off onto it's own dedicated box. We also had to do TONS of processing and data importing constantly. We moved our processing off onto 它的 own box as well. We also stopped loading our entire freaking stack just for our data import utilities. We tastefully loaded only what we absolutely needed (thus reducing memory overhead!).
Dog.find(:all).each do |dog|
#N queries
dog.owner.siblings.each do |sibling|
#N queries per above N query!!
sibling.pets.each do |pet|
#Do something here
end
end
end