Solution for speeding up a slow SELECT DISTINCT query in Postgres

前端 未结 3 1364
隐瞒了意图╮
隐瞒了意图╮ 2020-12-30 20:23

The query is basically:

SELECT DISTINCT \"my_table\".\"foo\" from \"my_table\" WHERE...

Pretending that I\'m 100% certain the DISTINC

3条回答
  •  自闭症患者
    2020-12-30 20:50

    You can try increasing the work_mem setting, depending on the size of Your dataset It can cause switching the query plan to hash aggregates, which are usually faster.

    But before setting it too high globally, first read up on it. You can easily blow up Your server, because the max_connections setting acts as a multiplier to this number.

    This means that if you were to set work_mem = 128MB and you set max_connections = 100 (the default), you should have more than 12.8GB of RAM. You're essentially telling the server that it can use that much for performing queries (not even considering any other memory use by Postgres or otherwise).

提交回复
热议问题