プロジェクト

全般

プロフィール

Vote #78143

完了

Minor performance improvement - Replace count by exists?

Admin Redmine さんが約4年前に追加. 約4年前に更新.

ステータス:
Closed
優先度:
通常
担当者:
-
カテゴリ:
Performance_53
対象バージョン:
開始日:
2022/05/09
期日:
進捗率:

0%

予定工数:
category_id:
53
version_id:
119
issue_org_id:
24839
author_id:
88801
assigned_to_id:
1
comments:
5
status_id:
5
tracker_id:
3
plus1:
0
affected_version:
closed_on:
affected_version_id:
ステータス-->[Closed]

説明

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 %>

 
-<%= 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? %>
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" = ???;

journals

Thank you for the patch.

I found three more @count@ which can be replaced with @exists?@. This is an updated patch: attachment:use-exists-instead-of-count.diff

Although @.count > 0@ can be found in @app/views/imports/show.html.erb@ too, I have not changed because of cache efficiency (exactly the same query will be performed just after them).
--------------------------------------------------------------------------------

--------------------------------------------------------------------------------
Excluded @app/models/project.rb@ from the patch because of cache efficiency.
--------------------------------------------------------------------------------
Committed, thanks.
--------------------------------------------------------------------------------

--------------------------------------------------------------------------------


related_issues

relates,Closed,26728,count > 0 vs exists?

Admin Redmine さんが約4年前に更新

  • カテゴリPerformance_53 にセット
  • 対象バージョン3.4.0_119 にセット

他の形式にエクスポート: Atom PDF

いいね!0
いいね!0