Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

Improve performance of self_and_descendants #122

Merged
merged 2 commits into from

2 participants

@marktorrance

This modified version of self_and_descendants is semantically equivalent and passes all tests, but it uses only the lft field of the records it queries, which means it can benefit from an index on that field if one exists on your table.

lib/awesome_nested_set/awesome_nested_set.rb
@@ -336,7 +336,8 @@ def level
# Returns a set of itself and all of its nested children
def self_and_descendants
nested_set_scope.where([
- "#{self.class.quoted_table_name}.#{quoted_left_column_name} >= ? AND #{self.class.quoted_table_name}.#{quoted_right_column_name} <= ?", left, right
+ "#{self.class.quoted_table_name}.#{quoted_left_column_name} >= ? AND #{self.class.quoted_table_name}.#{quoted_left_column_name} <= ?", left, right
@parndt Owner
parndt added a note

Shouldn't it be < not <= on the second half?

I guess it doesn't matter, since lft is always less than rgt for any given node. But I can change that if you like.

@parndt Owner
parndt added a note

I was just thinking that it would be a more correct version?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
@parndt parndt merged commit 18fe58a into from
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Mar 16, 2012
  1. Respond to parndt suggestion: < instead of <=

    Mark Torrance authored
This page is out of date. Refresh to see the latest.
Showing with 2 additions and 1 deletion.
  1. +2 −1  lib/awesome_nested_set/awesome_nested_set.rb
View
3  lib/awesome_nested_set/awesome_nested_set.rb
@@ -336,7 +336,8 @@ def level
# Returns a set of itself and all of its nested children
def self_and_descendants
nested_set_scope.where([
- "#{self.class.quoted_table_name}.#{quoted_left_column_name} >= ? AND #{self.class.quoted_table_name}.#{quoted_right_column_name} <= ?", left, right
+ "#{self.class.quoted_table_name}.#{quoted_left_column_name} >= ? AND #{self.class.quoted_table_name}.#{quoted_left_column_name} < ?", left, right
+ # using _left_ for both sides here lets us benefit from an index on that column if one exists
])
end
Something went wrong with that request. Please try again.