Defect #32612

Distinct can be removed due to unique constraint in database

Added by jwjw yy about 1 year ago.

Status:NewStart date:
Priority:NormalDue date:
Assignee:-% Done:

0%

Category:-
Target version:-
Resolution: Affected version:

Description

Hi, we are a research team that aims to detect and fix performance and data constraints problems in application built upon ORM framework, such as Rails. Redmine provides us with a good platform.

There are two queries can be improved by removing the distinct keyword based on the unique constraint created in database.

@principals ||= Principal.active.joins(:members).where("#{Member.table_name}.project_id = ?", id).distinct

 @users ||= User.active.joins(:members).where("#{Member.table_name}.project_id = ?", 


constraint is redmine/db/migrate/20101104182107_add_unique_index_on_members.rb
add_index :members, [:user_id, :project_id], :unique => true

which means (user_id, project_id) will be unique. so this will make above two queries return distinct results without the `distinct` call.

Also available in: Atom PDF