Project

General

Profile

Actions

Patch #24839

closed

Minor performance improvement - Replace count by exists?

Added by Vincent Robert over 7 years ago. Updated over 7 years ago.

Status:
Closed
Priority:
Normal
Category:
Performance
Target version:
Start date:
Due date:
% Done:

0%

Estimated time:

Description

When checking to see if a query returns any records, we should use the ActiveRecord exists? method instead of count.

Here is an example in the versions/show.html.erb file:

diff --git a/app/views/versions/show.html.erb b/app/views/versions/show.html.erb
index 82bd572..fc22a9f 100644
--- a/app/views/versions/show.html.erb
+++ b/app/views/versions/show.html.erb
@@ -32,7 +32,7 @@
 <% end %>

 <div id="status_by">
-<%= render_issue_status_by(@version, params[:status_by]) if @version.fixed_issues.count > 0 %>
+<%= render_issue_status_by(@version, params[:status_by]) if @version.fixed_issues.exists? %>
 </div>
 </div>

If we compare the generated queries with PostgreSQL: 

SELECT  1 AS one FROM "issues" WHERE "issues"."fixed_version_id" = ??? LIMIT 1;
should be faster than
SELECT COUNT(*) FROM "issues" WHERE "issues"."fixed_version_id" = ???;

Files


Related issues

Related to Redmine - Patch #26728: count > 0 vs exists?Closed

Actions
Actions

Also available in: Atom PDF