Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

speedup unique impressions #24

Merged
merged 1 commit into from

2 participants

@lacco

When using the :unique option, then a impressions.count == 0is used to validate uniqueness. This requires a full table scan every time (without appropriate indexes):

SELECT COUNT(*) FROM `impressions`

Using !impressions.exists? results in a much more efficient query (but even requires a full table scan when there is no existing impression):

SELECT 1 FROM `impressions` LIMIT 1

@johnmcaliley johnmcaliley merged commit 7de8bd9 into from
@johnmcaliley

Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Jan 19, 2012
  1. @lacco
This page is out of date. Refresh to see the latest.
Showing with 2 additions and 2 deletions.
  1. +2 −2 app/controllers/impressionist_controller.rb
View
4 app/controllers/impressionist_controller.rb
@@ -48,11 +48,11 @@ def bypass
end
def unique_instance?(impressionable, unique_opts)
- return unique_opts.blank? || impressionable.impressions.where(unique_query(unique_opts)).size == 0
+ return unique_opts.blank? || !impressionable.impressions.where(unique_query(unique_opts)).exists?
end
def unique?(unique_opts)
- return unique_opts.blank? || Impression.where(unique_query(unique_opts)).size == 0
+ return unique_opts.blank? || !Impression.where(unique_query(unique_opts)).exists?
end
# creates the query to check for uniqueness
Something went wrong with that request. Please try again.