Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1j0R0D-0004Lk-7C for pgsql-hackers@arkaria.postgresql.org; Sat, 08 Feb 2020 14:22:33 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1j0R0A-0005Q9-S2 for pgsql-hackers@arkaria.postgresql.org; Sat, 08 Feb 2020 14:22:30 +0000 Received: from makus.postgresql.org ([2001:4800:3e1:1::229]) by malur.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1j0R0A-0005Q1-Af for pgsql-hackers@lists.postgresql.org; Sat, 08 Feb 2020 14:22:30 +0000 Received: from mail-wr1-x442.google.com ([2a00:1450:4864:20::442]) by makus.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.92) (envelope-from ) id 1j0R02-0003vv-SB for pgsql-hackers@postgresql.org; Sat, 08 Feb 2020 14:22:28 +0000 Received: by mail-wr1-x442.google.com with SMTP id y11so2197375wrt.6 for ; Sat, 08 Feb 2020 06:22:22 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=2ndquadrant-com.20150623.gappssmtp.com; s=20150623; h=date:from:to:cc:subject:message-id:references:mime-version :content-disposition:in-reply-to; bh=Yp4nfjZ70ziKGzZnmTK4GDpLbjbxE/3YOtNJtQAzaVY=; b=1jTrk0lbVpJQcPrTDD8PqlYeZoa/rWZ7gN/QYAMoZjGQaLKhaQc2bqbiqH8aSIc0Jd nz2nMF/h6BNK4NlG+LZyxzcoTn5DzmbDASvrTXlAhD6m1QScz+kVm0iKM5oRF2MNI7ri Q/dcgRJ/uYfqOnxj4MgYm8btUi7ZtMTiPVidAC/VW1yTKvAU0MShBvb52hi1iW/Iep35 SZGf/M3Q458zrrR3ltEmmaw7RWa5AjX2ZIlTug6tzWxQkNVZjUc8PjVzSqRwJeKWIW76 if8P9Z3WdWfSEXAh93z2AnkLtB2Nw1wnFs61QSji6aGvkQskV64dyQm55TpPcB/Me+lX 63Lg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:date:from:to:cc:subject:message-id:references :mime-version:content-disposition:in-reply-to; bh=Yp4nfjZ70ziKGzZnmTK4GDpLbjbxE/3YOtNJtQAzaVY=; b=J0EfHPUYR8tI11SevTon2+apicui9pH8u/o+00VY7G3UiEy+loZNBrfmJdIa+hFBca jF6cLEdDrQf48Ieb/x9YvTYV/k16m3pelE9rPlyKu6iSDiIW67503KLsPV5xIEBVmskM vMMmHYtdbK138fqQIMb/xSxSYP++CvCQ7rQa9MPaJLoF9Pwrcp5hjWRt5+kPe+pLmsRW 39cIWmdGUA2ZJb6h8VCuNXJBmnXJAbQ2Nwu7A67Bx3NFk0IVGqPXzpP/WU42KSdIVU0s 3AQFlMYdv0BZTn74lNoRexpbWvJLQvLQyeC9XEIOWWk1ifbhqClBmdbJFIXQorjecmpn bVIA== X-Gm-Message-State: APjAAAVloS6tQJJgC1OnEGLYIBYuTHplkq76yAz2EDHsylC2qkednbcq +hXVwUDGFrpL/Nmkkdbc5560Fw== X-Google-Smtp-Source: APXvYqxm2AD83GK9V4ssSt4guoCWr/PzbxsVQi5xAfRVq8L+L7ABJCsRF/pdKUCKyY5YQk0boHFk3Q== X-Received: by 2002:adf:f10b:: with SMTP id r11mr5632858wro.307.1581171741431; Sat, 08 Feb 2020 06:22:21 -0800 (PST) Received: from localhost (ip-86-49-253-92.net.upcbroadband.cz. [86.49.253.92]) by smtp.gmail.com with ESMTPSA id t13sm7777295wrw.19.2020.02.08.06.22.18 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 08 Feb 2020 06:22:20 -0800 (PST) Date: Sat, 8 Feb 2020 15:22:17 +0100 From: Tomas Vondra To: Dmitry Dolgov <9erthalion6@gmail.com> Cc: Kyotaro Horiguchi , florisvannee@optiver.com, pg@bowt.ie, jesper.pedersen@redhat.com, david.rowley@2ndquadrant.com, alvherre@2ndquadrant.com, thomas.munro@gmail.com, jtc331@gmail.com, rafia.pghackers@gmail.com, jeff.janes@gmail.com, bhushan.uparkar@gmail.com, pgsql-hackers@postgresql.org, a.korotkov@postgrespro.ru Subject: Re: Index Skip Scan Message-ID: <20200208142217.fhsiymg3szfnniih@development> References: <20200205163730.eqvoxh6eb3do52r5@localhost> <20200206.102450.1514793516830965023.horikyota.ntt@gmail.com> <20200206105707.fuidmdsi6oapieoz@localhost> <20200206.212220.642002310227620136.horikyota.ntt@gmail.com> <20200207162543.zlj2tvcwy7jku6vu@localhost> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii; format=flowed Content-Disposition: inline In-Reply-To: <20200207162543.zlj2tvcwy7jku6vu@localhost> List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk Hi, I've done some testing and benchmarking of the v31 patch, looking for regressions, costing issues etc. Essentially, I've ran a bunch of SELECT DISTINCT queries on data sets of various size, number of distinct values etc. The results are fairly large, so I've uploaded them to github https://github.com/tvondra/skip-scan-test There are four benchmark groups, depending on how the data are generated and availability of extended stats and if the columns are independent: 1) skipscan - just indexes, columns are independent 2) skipscan-with-stats - indexes and extended stats, independent columns 3) skipscan-correlated - just indexes, correlated columns 4) skipscan-correlated-with-stats - indexes and extended stats, correlated columns The github repository contains *.ods spreadsheet comparing duration with the regular query plan (no skip scan) and skip scan. In general, there are pretty massive speedups, often by about two orders of magnitude. There are a couple of regressions, where the plan with skipscan enables is ~10x slower. But this seems to happen only in high-cardinality cases where we misestimate the number of groups. Consider a table with two independent columns CREATE TABLE t (a text, b text); INSERT INTO t SELECT md5((10000*random())::int::text), md5((10000*random())::int::text) FROM generate_series(1,1000000) s(i); CREATE INDEX ON t(a,b); ANALYZE; which then behaves like this: test=# select * from (select distinct a,b from t) foo offset 10000000; Time: 3138.222 ms (00:03.138) test=# set enable_indexskipscan = off; Time: 0.312 ms test=# select * from (select distinct a,b from t) foo offset 10000000; Time: 199.749 ms So in this case the skip scan is ~15x slower than the usual plan (index only scan + unique). The reason why this happens is pretty simple - to estimate the number of groups we multiply the ndistinct estimates for the two columns (which both have n_distinct = 10000), but then we cap the estimate to 10% of the table. But when the columns are independent with high cardinalities that under-estimates the actual value, making the cost for skip scan much lower than it should be. I don't think this is an issue the skipscan patch needs to fix, though. Firstly, the regressed cases are a tiny minority. Secondly, we already have a way to improve the root cause - creating extended stats with ndistinct coefficients generally makes the problem go away. One interesting observation however is that this regression only happened with text columns but not with int or bigint. My assumption is that this is due to text comparisons being much more expensive. Not sure if there is something we could do to deal with this - reduce the number of comparisons or something? regards -- Tomas Vondra http://www.2ndQuadrant.com PostgreSQL Development, 24x7 Support, Remote DBA, Training & Services