# which is better: select distinct VS array\_unique

**URL:** <https://forums.percona.com/t/which-is-better-select-distinct-vs-array-unique/1285>\
**Category:** Other MySQL® Questions\
**Created:** [November 3, 2009, 10:02pm UTC](https://forums.percona.com/t/which-is-better-select-distinct-vs-array-unique/1285 "2009-11-03T22:02:59Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![qspy](https://avatars.discourse-cdn.com/v4/letter/q/a88e57/32.png) [@qspy](https://forums.percona.com/u/qspy)\
**Post date:** [November 3, 2009, 10:02pm UTC](https://forums.percona.com/t/which-is-better-select-distinct-vs-array-unique/1285/1 "2009-11-03T22:02:59Z")

</div>

got query like this:

SELECT DISTINCT p.topic\_id FROM phpbb\_posts p WHERE p.post\_id IN (1275, 3128, 3129, 10702, 16451, 37026, 44527, 65259, 68238, 68805, 122704, 254944, 323501, 384915, 387562, 593207, 615833, 639439, 660219, [etc], 9732302, 9734352, 9913929, 9959593, 10033198, 10033449, 7212999);±—±------------±------±------±--------------±--------±--------±-----±-----±-----------------------------+| id | select\_type | table | type | possible\_keys | key | key\_len | ref | rows | Extra |±—±------------±------±------±--------------±--------±--------±-----±-----±-----------------------------+| 1 | SIMPLE | p | range | PRIMARY | PRIMARY | 3 | NULL | 292 | Using where; Using temporary |±—±------------±------±------±--------------±--------±--------±-----±-----±-----------------------------+and then: while ($row = $db\_slave-\>sql\_fetchrow($result)) { $search\_ids = $row[‘topic\_id’]; }

How about this:

SELECT p.topic\_idFROM phpbb\_posts p WHERE p.post\_id IN (1275, 3128, 3129, 10702, 16451, 37026, 44527, 65259, 68238, 68805, 122704, 254944, 323501, 384915, 387562, 593207, 615833, 639439, 660219, [etc], 9732302, 9734352, 9913929, 9959593, 10033198, 10033449, 7212999);±—±------------±------±------±--------------±--------±--------±-----±-----±------------+| id | select\_type | table | type | possible\_keys | key | key\_len | ref | rows | Extra |±—±------------±------±------±--------------±--------±--------±-----±-----±------------+| 1 | SIMPLE | p | range | PRIMARY | PRIMARY | 3 | NULL | 292 | Using where |±—±------------±------±------±--------------±--------±--------±-----±-----±------------+and then: while ($row = $db\_slave-\>sql\_fetchrow($result)) { $search\_ids = $row[‘topic\_id’]; }$old\_array=$search\_ids; $new\_array=array\_unique($old\_array);

So we eliminating Using temporary (dinstinct first query), to make distinct via php. Do you predict any real worth performance gain?

.

---

<div class="post-metadata">

**Author:** ![gmouse](https://avatars.discourse-cdn.com/v4/letter/g/b9e5f3/32.png) [@gmouse](https://forums.percona.com/u/gmouse)\
**Post date:** [November 4, 2009, 1:21pm UTC](https://forums.percona.com/t/which-is-better-select-distinct-vs-array-unique/1285/2 "2009-11-04T13:21:15Z")

</div>

It is a trade-off. In this situation I would not care about the temp table since it can be kept in memory (no text fields).
