Project

General

Profile

Actions

Patch #38319

closed

Optimize IssueQuery#sql_for_assigned_to_role_field for PostgreSQL performance

Added by Vincent Robert over 1 year ago. Updated about 1 year ago.

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

0%

Estimated time:

Description

In our installation, with plenty of issues and projects, we met a performance issue with the assigned_to_role filter on issues.
The SQL query was very slow when using this filter.
So, here is a patch to make this request a lot faster:

diff --git a/app/models/issue_query.rb b/app/models/issue_query.rb
index eb7684cc2..ec3d5ec62 100644
--- a/app/models/issue_query.rb
+++ b/app/models/issue_query.rb
@@ -575,8 +575,9 @@ class IssueQuery < Query
         end
       sw = operator == "!" ? 'NOT' : ''
       nl = operator == "!" ? "#{Issue.table_name}.assigned_to_id IS NULL OR" : ''
-      "(#{nl} #{Issue.table_name}.assigned_to_id #{sw} IN (SELECT DISTINCT #{Member.table_name}.user_id FROM #{Member.table_name}, #{MemberRole.table_name}" +
-        " WHERE #{Member.table_name}.project_id = #{Issue.table_name}.project_id AND #{Member.table_name}.id = #{MemberRole.table_name}.member_id AND #{role_cond}))" 
+      "(#{nl} (#{Issue.table_name}.assigned_to_id, #{Issue.table_name}.project_id) #{sw} IN (" +
+        " SELECT DISTINCT #{Member.table_name}.user_id, #{Member.table_name}.project_id FROM #{Member.table_name}, #{MemberRole.table_name}" +
+        " WHERE #{Member.table_name}.id = #{MemberRole.table_name}.member_id AND #{role_cond}))" 
     end
   end

Thank you for considering this change.


Files

issue_query.diff (1.01 KB) issue_query.diff Vincent Robert, 2023-03-02 08:14

Related issues

Related to Redmine - Defect #39443: Invalid statement query error on MSSQL when role filter is used in issues queryClosedMarius BĂLTEANU

Actions
Actions

Also available in: Atom PDF