Distinct can be removed due to unique index

In, theme.rb, there is
ChildTheme.where(parent_theme_id: theme_id).distinct.pluck(:child_theme_id), in which the distinct can be removed due to the unique index added in discourse/db/migrate/20170313192741_add_themes.rb: add_index :child_themes, [:parent_theme_id, :child_theme_id], unique: true.

This indicates that for two childthemes: ChildTheme1(id1, child_theme_id1, parent_them_id), and ChildTheme2(id2, child_theme_id2, parent_them_id), if child_theme_id1 == child_theme_id2, then id1 = id2, or else [:parent_theme_id, :child_theme_id] is not unique. As a result ChildTheme.where(parent_theme_id: theme_id).pluck(:child_theme_id) will return unique child_theme_id, so there is no need to add an distinct function call. We can remove it improve the performance.

2 Likes

Ok, you may want to submit a pull request with this change.

1 Like

Note perf wise this will make no difference, but it does improve clarity a bit so I also support a PR here

2 Likes

submitted https://github.com/discourse/discourse/pull/8472

3 Likes