digoal
2016-11-08
postgresql , 并行計算 , tpc-h
postgresql 9.6首次推出支援聚合、全表掃描、hash join、nestloop的并行計算。
<a href="https://www.postgresql.org/docs/9.6/static/release-9-6.html">https://www.postgresql.org/docs/9.6/static/release-9-6.html</a>
那麼他對tpc-h有多少的性能提升呢?
并行度設為4,22條查詢有17條使用了并行執行計劃。
15條比單核執行更快,其中11條提升至少2倍,1條速度未變化,還有1條變慢。
i decided to try out parallel query, as implemented in postgresql 9.6devel, on the tpc-h queries.
i did the test on an ibm power7 server provided to the postgresql community by ibm.
i scaled the database to use 10gb of input data; the resulting database size was 22gb, of which 8gb was indexes.
i tried out each query just once without really tuning the database at all, except for increasing shared_buffers to 8gb.
then i tested them again after enabling parallel query by configuring max_parallel_degree = 4.
of the 22 queries, 17 switched to a parallel plan, while the plans for the other 5 were unchanged.
of the 17 queries where the plan changed, 15 got faster, 1 ran at the same speed, and 1 got slower.
11 of the queries ran at least twice as fast with parallelism as they did without parallelism.
here are the comparative results for the queries where the plan changed(parallel vs 單核執行):
linear scaling with a leader process and 4 workers would mean a 5.0x speedup, which we achieved in only one case.
however, for many users, that won't matter: if you have cpus that would otherwise be sitting idle, it's better to get some speedup than no speedup at all.
of course, i couldn't resist analyzing what went wrong here, especially for q21, which actually got slower.
q21變慢的原因,是work_mem的配置問題,以及目前hash join并行機制的問題。
to some degree, that's down to misconfiguration:
i ran this test with the default value of work_mem=4mb, but q21 chooses a plan that builds a hash table on the largest table in the database, which is about 9.5gb in this test.
therefore, it ends up doing a 1024-batch hash join, which is somewhat painful under the best of circumstances.
with work_mem=1gb, the regression disappears, and it's 6% faster with parallel query than without.
目前hash join,每一個并行的worker都需要一份hash table的拷貝,如果大表hash的話,會在大表基礎上放大n倍的cpu和記憶體的開銷。
小表hash這個問題可以緩解。
however, there's a deeper problem, which is that while postgresql 9.6 can perform a hash join in parallel, each process must build its own copy of the hash table.
that means we use n times the cpu and n times the memory, and we may induce i/o contention, locking contention, or memory pressure as well.
it would be better to have the ability to build a shared hash table, and enterprisedb is working on that as a feature, but it won't be ready in time for postgresql 9.6, which is already in feature freeze.
since q21 needs a giant hash table, this limitation really stings.
hash join可以提升的點,使用共享的hash table,而不是每個woker process都拷貝一份。
這個可能要等到postgresql 10.0加進來了。
in fact, there are a number of queries here where it seems like building a shared hash table would speed things up significantly: q3, q5, q7, q8, and q21.
an even more widespread problem is that, at present, the driving table for a parallel query must be accessed via a parallel sequential scan;
that's the only operation we have that can partition the input data.
另一個提升的點,bitmap scan,因為有幾個query的瓶頸是在bitmap scan哪裡,但是目前并行計算還不支援bitmap scan。
many of these queries - q4, q5, q6, q7, q14, q15, and q20 - would have been better off using a bitmap index scan on the driving table, but unfortunately that's not supported in postgresql 9.6.
we still come out ahead on these queries in terms of runtime because the system simply substitutes raw power for finesse:
with enough workers, we can scan the whole table quicker than a single process can scan the portion identified as relevant by the index.
however, it would clearly be nice to do better.
four queries - q2, q15, q16, q22 - were parallelized either not at all or only to a limited degree due to restrictions related to the handling of subqueries,
about which the current implementation of parallel query is not always smart.
three queries - q2, q13, and q15 - made no or limited use of parallelism because the optimal join strategy is a merge join, which can't be made parallel in a trivial way.
one query - q17 - managed to perform the same an expensive sort twice, once in the workers and then again in the leader.
this is because the gather operation reads tuples from the workers in an arbitrary and not necessarily predictable order;
so even if each worker's stream of tuples is sorted, the way those streams get merged together will probably destroy the sort ordering.
there are no doubt other issues here that i haven't found yet, but on the whole i find these results pretty encouraging.
parallel query basically works, and makes queries that someone thought were representative of real workloads significantly faster.
there's a lot of room for further improvement, but that's likely to be true of the first version of almost any large feature.
支援merge join。