Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtp (Exim 4.80) (envelope-from ) id 1XeSuI-0001lx-6n for pgsql-performance@arkaria.postgresql.org; Wed, 15 Oct 2014 18:02:42 +0000 Received: from localhost ([127.0.0.1] helo=postgresql.org) by malur.postgresql.org with smtp (Exim 4.80) (envelope-from ) id 1XeSuH-0000zx-Jg for pgsql-performance@arkaria.postgresql.org; Wed, 15 Oct 2014 18:02:41 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.2:DHE_RSA_AES_256_CBC_SHA256:256) (Exim 4.80) (envelope-from ) id 1XeSuG-0000zq-FK for pgsql-performance@postgresql.org; Wed, 15 Oct 2014 18:02:40 +0000 Received: from smtp-out2.gransy.com ([77.78.126.49]) by magus.postgresql.org with esmtp (Exim 4.80) (envelope-from ) id 1XeSuA-0007ja-N7 for pgsql-performance@postgresql.org; Wed, 15 Oct 2014 18:02:39 +0000 Received: from smtp-out.gransy.com (localhost [127.0.0.1]) by smtp-out2.gransy.com (Postfix) with ESMTP id 247DC3F010B for ; Wed, 15 Oct 2014 20:02:35 +0200 (CEST) Received: from smtp.gransy.com (smtp.simplia.cz [77.78.126.20]) by smtp-out2.gransy.com (Postfix) with ESMTP id AFF1F3F0107 for ; Wed, 15 Oct 2014 20:02:34 +0200 (CEST) Received: from [192.168.1.163] (ip-78-45-139-138.net.upcbroadband.cz [78.45.139.138]) (Authenticated sender: tv@fuzzy.cz) by smtp.gransy.com (Postfix) with ESMTPSA id D5D3419A0523 for ; Wed, 15 Oct 2014 20:02:26 +0200 (CEST) Message-ID: <543EB6B6.4080005@fuzzy.cz> Date: Wed, 15 Oct 2014 20:02:30 +0200 From: Tomas Vondra User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:31.0) Gecko/20100101 Thunderbird/31.1.1 MIME-Version: 1.0 To: pgsql-performance@postgresql.org Subject: Re: Yet another abort-early plan disaster on 9.3 References: <541A2335.3060100@agliodbs.com> <5429D517.3090900@agliodbs.com> <542C4E60.3030505@agliodbs.com> <542DADEB.2080103@agliodbs.com> <543EACD2.50004@agliodbs.com> In-Reply-To: <543EACD2.50004@agliodbs.com> Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 7bit X-Pg-Spam-Score: -1.9 (-) List-Archive: List-Help: List-ID: List-Owner: List-Post: List-Subscribe: List-Unsubscribe: X-Mailing-List: pgsql-performance Precedence: bulk Sender: pgsql-performance-owner@postgresql.org On 15.10.2014 19:20, Josh Berkus wrote: > On 10/10/2014 04:16 AM, Greg Stark wrote: >> On Thu, Oct 2, 2014 at 8:56 PM, Josh Berkus wrote: >>> Yes, it's only intractable if you're wedded to the idea of a tiny, >>> fixed-size sample. If we're allowed to sample, say, 1% of the table, we >>> can get a MUCH more accurate n_distinct estimate using multiple >>> algorithms, of which HLL is one. While n_distinct will still have some >>> variance, it'll be over a much smaller range. >> >> I've gone looking for papers on this topic but from what I read this >> isn't so. To get any noticeable improvement you need to read 10-50% of >> the table and that's effectively the same as reading the entire table >> -- and it still had pretty poor results. All the research I could find >> went into how to analyze the whole table while using a reasonable >> amount of scratch space and how to do it incrementally. > > So, right now our estimation is off on large tables by -10X to > -10000X. First, the fact that it's *always* low is an indication > we're using the wrong algorithm. Second, we can most certainly do > better than a median of -1000X. A few days ago I posted an experimental patch with the adaptive estimator, described in [1]. Not perfect, but based on the testing I did I believe it's a superior algorithm to the one we use now. Would be nice to identify a few datasets where the current estimate is way off. [1] http://ftp.cse.buffalo.edu/users/azhang/disc/disc01/cd1/out/papers/pods/towardsestimatimosur.pdf > One interesting set of algorithms is block-based sampling. That is, > you read 5% of the physical table in random blocks, reading every row > in the block. The block size is determined by your storage block > size, so you're not actually reading any more physically than you are > logically; it really is just 5% of the table, especially on SSD. > > Then you apply algorithms which first estimate the correlation of > common values in the block (i.e. how likely is it that the table is > completely sorted?), and then estimates of how many values there > might be total based on the correlation estimate. I think we might also use a different approach - instead of sampling the data when ANALYZE kicks in, we might collect a requested sample of rows on the fly. Say we want 1% sample - whenever you insert a new row, you do [random() < 0.01] and if it happens to be true you keep a copy of the row aside. Then, when you need the sample, you simply read the sample and you're done - no random access to the main table, no problems with estimated being off due to block-level sampling, etc. Not sure how to track deletions/updates, though. Maybe rebuilding the sample if the number of deletions exceeds some threshold, but that contradicts the whole idea a bit. > I no longer have my ACM membership, so I can't link this, but > researchers were able to get +/- 3X accuracy for a TPCH workload > using this approach. A real database would be more variable, of > course, but even so we should be able to achieve +/- 50X, which > would be an order of magnitude better than we're doing now. If you know the title of the article, it's usually available elsewhere on the web - either at the university site, or elsewhere. I found these two articles about block-based sampling: http://ranger.uta.edu/~gdas/websitepages/preprints-papers/p287-chaudhuri.pdf https://www.stat.washington.edu/research/reports/1999/tr355.pdf Maybe there are more, but most of the other links were about how Oracle does this in 11g. Tomas -- Sent via pgsql-performance mailing list (pgsql-performance@postgresql.org) To make changes to your subscription: http://www.postgresql.org/mailpref/pgsql-performance