In my ruby-on-rails app, I have nested comments that can be nested an arbitrary length.
I tried different ways of storing this:
Using self joins:
belongs_to :parent, :class_name => 'Comment', :foreign_key => 'parent_id'
has_many :children, :class_name => 'Comment', :foreign_key => "parent_id"
Using ancestry gem
etc
The problem, though, is that no matter what I use, there will always be an linear number of SQL statements. (1 statement to grab all the root comments, and then 1 statement for each root’s children, and then 1 statement for all the children of that, etc)
Is there a more efficient way to accomplish this?
Postgres 9.1, but hopefully backwards compatible solutions are preferred.
You could stick with your
parent_idpointer column and usefind_by_sqland aWITH RECURSIVEquery and let the database do all the work in one shot. Something like this:where
rootswould be a Ruby array holding theids of the root nodes that you’re interested in. That will give you all the nodes in the subtrees of interest as Comment instances. I’ve used queries like this in the past and WITH RECURSIVE was well over twice as fast as your iterative technique even with shallow trees, I’d guess that deeper trees would see even better speed ups.The
parent_idstructure you’re using is very convenient for most things and meshes quite well with how ActiveRecord wants to work. Also, sticking with your current structure means that you can leave the rest of your application alone.WITH RECURSIVE is available in PostgreSQL 8.4 and higher.