Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use empty? instead of count for validate uniqueness #2122

Conversation

numbata
Copy link
Contributor

@numbata numbata commented Jan 19, 2024

Using empty provides a more efficient way to check for records that violate the uniqueness constraint. Unlike count, which counts all matching rows, empty stops as soon as it finds a match, thereby reducing the load on the database. This change is particularly beneficial for tables with a large number of rows, where performance can be an issue.

Using `empty?` instead of `count` for performance reasons. The `empty?` checks for
the existence of any record and is generally faster than `count`.
@numbata numbata force-pushed the validate_uniqueness_by_finding_first_row branch from 03e510f to c457c1e Compare January 19, 2024 09:33
@jeremyevans
Copy link
Owner

Thanks for the patch. This makes sense to me. I'll review and test later today.

db = Sequel.mock(:fetch=>proc{|sql| sql =~ /'a{51}'|'uniq'/ ? {:v=>0} : {:v=>1}})
fetch_proc = proc do |sql|
if sql =~ /'a{51}'|'uniq'/
sql =~ /1 AS one/ ? {:v=>nil} : {:v=>0}
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is not a style change. The code was unwrapped because new check was added here.

@jeremyevans jeremyevans merged commit c457c1e into jeremyevans:master Jan 19, 2024
16 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants