月亮 – >行星 – >明星 – > galaxy – >宇宙
Moon belongs_to Planet,Planet belongs_to Star等等:
class Moon < ActiveRecord::Base belongs_to :planet,inverse_of: :moons has_one :star,through: :planet has_one :galaxy,through: :star has_one :universe,through: :galaxyend
class Planet < ActiveRecord::Base belongs_to :star,inverse_of: :planets has_many :moons,inverse_of: :planet,dependent: :destroyend
问题
我试图让Rails将这些对象的层次结构加载到内存中.我正在努力实现两件事:
>使用一个有效的查询加载一切
>能够使用诸如moon.galaxy之类的调用而不是moon.planet.star.galaxy之类的调用而无需额外调用数据库.
我尝试了两种方法.第一个(m1)呼叫包含在月亮上,所有关系变平.这导致查询效率非常低,但我可以调用m1.galaxy.第二个(m2)调用包括关系层次.这会产生有效的查询,但我可以调用m2.galaxy而无需访问数据库.
这样做的正确方法是什么?
例
m1 – 低效查询,m2 – 高效查询
irb(main):152:0* m1 = Moon.includes(:planet,:star,:galaxy).where(galaxIEs: {name: 'The Milky Way'}).first sql (1.9ms) SELECT "moons"."ID" AS t0_r0,"moons"."name" AS t0_r1,"moons"."planet_ID" AS t0_r2,"moons"."created_at" AS t0_r3,"moons"."updated_at" AS t0_r4,"planets"."ID" AS t1_r0,"planets"."name" AS t1_r1,"planets"."star_ID" AS t1_r2,"planets"."created_at" AS t1_r3,"planets"."updated_at" AS t1_r4,"stars"."ID" AS t2_r0,"stars"."name" AS t2_r1,"stars"."galaxy_ID" AS t2_r2,"stars"."created_at" AS t2_r3,"stars"."updated_at" AS t2_r4,"galaxIEs"."ID" AS t3_r0,"galaxIEs"."name" AS t3_r1,"galaxIEs"."universe_ID" AS t3_r2,"galaxIEs"."created_at" AS t3_r3,"galaxIEs"."updated_at" AS t3_r4 FROM "moons" left OUTER JOIN "planets" ON "planets"."ID" = "moons"."planet_ID" left OUTER JOIN "planets" "planets_moons_join" ON "planets_moons_join"."ID" = "moons"."planet_ID" left OUTER JOIN "stars" ON "stars"."ID" = "planets_moons_join"."star_ID" left OUTER JOIN "planets" "planets_moons_join_2" ON "planets_moons_join_2"."ID" = "moons"."planet_ID" left OUTER JOIN "stars" "stars_moons_join" ON "stars_moons_join"."ID" = "planets_moons_join_2"."star_ID" left OUTER JOIN "galaxIEs" ON "galaxIEs"."ID" = "stars_moons_join"."galaxy_ID" WHERE "galaxIEs"."name" = ORDER BY "moons"."ID" ASC liMIT 1 [["name","The Milky Way"]]=> #<Moon ID: 1,name: "The Moon",planet_ID: 1,created_at: "2015-10-28 10:02:04",updated_at: "2015-10-28 10:02:04">irb(main):153:0> m2 = Moon.includes(planet: {star: :galaxy}).where(galaxIEs: {name: 'The Milky Way'}).first sql (0.5ms) SELECT "moons"."ID" AS t0_r0,"galaxIEs"."updated_at" AS t3_r4 FROM "moons" left OUTER JOIN "planets" ON "planets"."ID" = "moons"."planet_ID" left OUTER JOIN "stars" ON "stars"."ID" = "planets"."star_ID" left OUTER JOIN "galaxIEs" ON "galaxIEs"."ID" = "stars"."galaxy_ID" WHERE "galaxIEs"."name" = ORDER BY "moons"."ID" ASC liMIT 1 [["name",updated_at: "2015-10-28 10:02:04">
m1 – 我可以在内存中调用moon.galaxy.name,m2 – moon.galaxy.name调用DB
irb(main):154:0> m1.galaxy.name=> "The Milky Way"irb(main):155:0> m2.galaxy.name galaxy Load (0.5ms) SELECT "galaxIEs".* FROM "galaxIEs" INNER JOIN "stars" ON "galaxIEs"."ID" = "stars"."galaxy_ID" INNER JOIN "planets" ON "stars"."ID" = "planets"."star_ID" WHERE "planets"."ID" = liMIT 1 [["ID",1]]=> "The Milky Way"
m1 – moon.planet.star.galaxy.name调用DB,m2 – 我可以在内存中调用moon.planet.star.galaxy.name
irb(main):156:0> m1.planet.star.galaxy.name Star Load (3.3ms) SELECT "stars".* FROM "stars" WHERE "stars"."ID" = liMIT 1 [["ID",1]] galaxy Load (0.3ms) SELECT "galaxIEs".* FROM "galaxIEs" WHERE "galaxIEs"."ID" = liMIT 1 [["ID",1]]=> "The Milky Way"irb(main):157:0> m2.planet.star.galaxy.name=> "The Milky Way"
查询的差异
解决方法 嗯,我认为你的第二种方式(m2)是正确的. Bay还考虑了eager_load,因为包含可能会触发几个SQL查询而不是每个关联一个(行星,启动等).我认为你在这些不正确的联想星球,星系和宇宙中的问题.
class Moon < ActiveRecord::Base belongs_to :planet,through: :galaxyend
只需删除它们然后再试一次.
请注意你不能这样使用has_one(example in documentation)
总结以上是内存溢出为你收集整理的什么是正确的使用方式包括(Rails 4,ActiveRecord,PostgreSQL)全部内容,希望文章能够帮你解决什么是正确的使用方式包括(Rails 4,ActiveRecord,PostgreSQL)所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)