Skip to content

Instantly share code, notes, and snippets.

@Timshel
Last active May 3, 2018 08:24
Show Gist options
  • Save Timshel/90fa50ffa6ad2756551b9ab7ee1f032c to your computer and use it in GitHub Desktop.
Save Timshel/90fa50ffa6ad2756551b9ab7ee1f032c to your computer and use it in GitHub Desktop.
Sort switch (tested on PG 9.6 and 10.3)
SET trace_sort TO on;
SET client_min_messages TO log;
explain analyze select id, sum(count) from sort_test group by id order by sum(count) desc limit 100

Output :

LOG:  begin tuple sort: nkeys = 1, workMem = 4096, randomAccess = f
LOG:  switching to bounded heapsort at 201 tuples: CPU 0.01s/1.30u sec elapsed 1.31 sec
LOG:  performsort starting: CPU 0.01s/1.30u sec elapsed 1.31 sec
LOG:  performsort done: CPU 0.01s/1.30u sec elapsed 1.31 sec
LOG:  internal sort ended, 29 KB used: CPU 0.01s/1.30u sec elapsed 1.31 sec

Limit  (cost=97148.11..97148.36 rows=100 width=12) (actual time=1312.064..1312.086 rows=100 loops=1)
  ->  Sort  (cost=97148.11..97149.36 rows=500 width=12) (actual time=1312.063..1312.079 rows=100 loops=1)
        Sort Key: (sum(count)) DESC
        Sort Method: top-N heapsort  Memory: 29kB
        ->  HashAggregate  (cost=97124.00..97129.00 rows=500 width=12) (actual time=1311.702..1311.774 rows=501 loops=1)
              Group Key: id
              ->  Seq Scan on sort_test  (cost=0.00..72124.00 rows=5000000 width=8) (actual time=0.014..333.964 rows=5000000 loops=1)
Planning time: 0.093 ms
Execution time: 1312.161 ms

Removing the limit in this specific case is faster (usually not the case whith more data) :

explain analyze select id, sum(count) from sort_test group by id order by sum(count) desc

Output :

LOG:  begin tuple sort: nkeys = 1, workMem = 4096, randomAccess = f
LOG:  performsort starting: CPU 0.01s/1.20u sec elapsed 1.21 sec
LOG:  performsort done: CPU 0.01s/1.20u sec elapsed 1.22 sec
LOG:  internal sort ended, 48 KB used: CPU 0.01s/1.20u sec elapsed 1.22 sec

Sort  (cost=97151.41..97152.66 rows=500 width=12) (actual time=1220.164..1220.188 rows=501 loops=1)
  Sort Key: (sum(count)) DESC
  Sort Method: quicksort  Memory: 48kB
  ->  HashAggregate  (cost=97124.00..97129.00 rows=500 width=12) (actual time=1219.804..1219.868 rows=501 loops=1)
        Group Key: id
        ->  Seq Scan on sort_test  (cost=0.00..72124.00 rows=5000000 width=8) (actual time=0.019..328.649 rows=5000000 loops=1)
Planning time: 0.049 ms
Execution time: 1220.270 ms

create table sort_test(id Integer, count INT );
insert into sort_test SELECT generate_series(1, 5000000) / 10000 AS id, (random() * 1000000)::INT AS descr;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment