We have a query that takes 1min to execute in postgres 10.6 and the same executes in 4 sec in Oracle database. The query is doing 'select distinct'. If I add a 'group by' clause, performance in postgres improves significantly and fetches results in 2 sec (better than oracle). But unfortunately, we cannot modify the query. Could you please suggest a way to improve performance in Postgres without modifying the query.
Original condition: time taken 1min
Sort Method: external merge Disk: 90656kB
After removing distinct from query: time taken 2sec
Sort Method: top-N heapsort Memory: 201kB
After increasing work_mem to 180MB; it takes 20sec
Sort Method: quicksort Memory: 172409kB
SELECT * FROM pg_stat_statements ORDER BY total_time DESC limit 1;
-[ RECORD 1 ]-------+-----------------------------------------------------------------------------------------------------------------------------------------
userid | 174862
dbid | 174861
queryid | 1469376470
query | <query is too long. It selects around 300 columns>