# 고유 인덱스 존재로 인해 Distinct 제거 가능

**URL:** https://meta.discourse.org/t/distinct-can-be-removed-due-to-unique-index/135225
**Category:** Development
**Created:** [12월 6, 2019, 3:24오전 UTC](https://meta.discourse.org/t/distinct-can-be-removed-due-to-unique-index/135225 "2019-12-06T03:24:34Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![junwen\_yang](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/junwen_yang/32/163137_2.png) [@junwen\_yang](https://meta.discourse.org/u/junwen_yang)
#### Post date: [12월 6, 2019, 3:24오전 UTC](https://meta.discourse.org/t/distinct-can-be-removed-due-to-unique-index/135225/1 "2019-12-06T03:24:34Z")

</div>

theme.rb 파일에는 [ChildTheme.where(parent\_theme\_id: theme\_id).distinct.pluck(:child\_theme\_id)](https://github.com/discourse/discourse/blob/b1207289992c752d55abbbbf7215fa37a0a5b2b2/app/models/theme.rb#L122)가 있으며, discourse/db/migrate/20170313192741\_add\_themes.rb에서 추가된 고유 인덱스(`add_index :child_themes, [:parent_theme_id, :child_theme_id], unique: true`) 덕분에 distinct를 제거할 수 있습니다.

이는 두 개의 자식 테마, 즉 ChildTheme1(id1, child\_theme\_id1, parent\_theme\_id)와 ChildTheme2(id2, child\_theme\_id2, parent\_theme\_id)에 대해 child\_theme\_id1 == child\_theme\_id2인 경우, id1 = id2가 되어야 한다는 것을 의미합니다. 그렇지 않으면 [:parent\_theme\_id, :child\_theme\_id]가 유일하지 않게 됩니다. 따라서 `ChildTheme.where(parent_theme_id: theme_id).pluck(:child_theme_id)`는 고유한 child\_theme\_id 값을 반환하므로 distinct 함수 호출이 불필요합니다. 이를 제거하면 성능을 향상시킬 수 있습니다.

---

<div class="post-metadata">

### Author: ![codinghorror](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/codinghorror/32/110067_2.png) [@codinghorror](https://meta.discourse.org/u/codinghorror)
#### Post date: [12월 6, 2019, 9:26오전 UTC](https://meta.discourse.org/t/distinct-can-be-removed-due-to-unique-index/135225/2 "2019-12-06T09:26:12Z")

</div>

네, 이 변경 사항을 포함해 풀 리퀘스트를 제출해 보시는 게 좋겠어요.

---

<div class="post-metadata">

### Author: ![sam](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/sam/32/102149_2.png) [@sam](https://meta.discourse.org/u/sam)
#### Post date: [12월 6, 2019, 9:38오전 UTC](https://meta.discourse.org/t/distinct-can-be-removed-due-to-unique-index/135225/3 "2019-12-06T09:38:18Z")

</div>

성능 측면에서는 차이가 없지만, 가독성이 약간 향상되므로 이 PR을 지지합니다.

---

<div class="post-metadata">

### Author: ![junwen\_yang](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/junwen_yang/32/163137_2.png) [@junwen\_yang](https://meta.discourse.org/u/junwen_yang)
#### Post date: [12월 6, 2019, 8:00오후 UTC](https://meta.discourse.org/t/distinct-can-be-removed-due-to-unique-index/135225/4 "2019-12-06T20:00:02Z")

</div>

[Update theme.rb - Pull Request #8472 - discourse/discourse - GitHub](https://github.com/discourse/discourse/pull/8472) 제출됨
