Skip to content
Browse files

Improve peformance of finds using old friendly_id

Dropping down to manual SQL here like FriendlyId 3.x does gives us a
3.6x performance improvement.
  • Loading branch information...
1 parent a1df050 commit aa40f56c3a2f1d642fafbac44b16f3044c065e09 @norman committed
Showing with 13 additions and 3 deletions.
  1. +1 −1 README.md
  2. +12 −2 lib/friendly_id/history.rb
View
2 README.md
@@ -73,7 +73,7 @@ The current docs can always be found
## Benchmarks
The latest benchmarks for FriendlyId are maintained
-[here](https://gist.github.com/1129745).
+[here](http://bit.ly/friendly-id-benchmarks).
## Bugs
View
14 lib/friendly_id/history.rb
@@ -78,7 +78,7 @@ module FinderMethods
def find_one(id)
return super if id.unfriendly_id?
where(@klass.friendly_id_config.query_field => id).first or
- includes(:slugs).where("#{Slug.table_name}.slug" => id).first or
+ with_old_friendly_id(id) {|x| find_one_without_friendly_id(x)} or
find_one_without_friendly_id(id)
end
@@ -86,9 +86,19 @@ def find_one(id)
def exists?(id = nil)
return super if id.unfriendly_id?
exists_without_friendly_id?(@klass.friendly_id_config.query_field => id) or
- includes(:slugs).where("#{Slug.table_name}.slug" => id).exists_without_friendly_id? or
+ with_old_friendly_id(id) {|x| exists_without_friendly_id?(x)} or
exists_without_friendly_id?(id)
end
+
+ private
+
+ # Accepts a slug, and yields a corresponding sluggable_id into the block.
+ def with_old_friendly_id(slug, &block)
+ sql = "SELECT sluggable_id FROM #{Slug.quoted_table_name} WHERE sluggable_type = %s AND slug = %s"
+ sql = sql % [@klass.base_class.name, slug].map {|x| connection.quote(x)}
+ sluggable_id = connection.select_values(sql).first
+ yield sluggable_id if sluggable_id
+ end
end
end
end

0 comments on commit aa40f56

Please sign in to comment.
Something went wrong with that request. Please try again.