]> granicus.if.org Git - postgresql/commit
Speed-up build of MCV lists with many distinct values
authorTomas Vondra <tomas.vondra@postgresql.org>
Thu, 4 Jul 2019 21:02:02 +0000 (23:02 +0200)
committerTomas Vondra <tomas.vondra@postgresql.org>
Fri, 5 Jul 2019 14:00:12 +0000 (16:00 +0200)
commit57f459cf6c48e78aa321ac88418815fc65197517
treea7a182bc099ce43705cab21200915eb7f2e223fb
parent1970da1a0a7e9793d1676341fcb0c9a01f3db0dc
Speed-up build of MCV lists with many distinct values

When building multi-column MCV lists, we compute base frequency for each
item, i.e. a product of per-column frequencies for values from the item.
As a value may be in multiple groups, the code was scanning the whole
array of groups while adding items to the MCV list.  This works fine as
long as the number of distinct groups is small, but it's easy to trigger
trigger O(N^2) behavior, especially after increasing statistics target.

This commit precomputes frequencies for values in all columns, so that
when computing the base frequency it's enough to make a simple bsearch
lookup in the array.

Backpatch to 12, where multi-column MCV lists were introduced.

Discussion: https://postgr.es/m/20190618205920.qtlzcu73whfpfqne@development
src/backend/statistics/mcv.c