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 1j1ElL-0003As-2j for pgsql-hackers@arkaria.postgresql.org; Mon, 10 Feb 2020 19:30:31 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1j1ElI-0000La-Mc for pgsql-hackers@arkaria.postgresql.org; Mon, 10 Feb 2020 19:30:28 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from <9erthalion6@gmail.com>) id 1j1ElI-0000LQ-C6 for pgsql-hackers@lists.postgresql.org; Mon, 10 Feb 2020 19:30:28 +0000 Received: from mail-wm1-x342.google.com ([2a00:1450:4864:20::342]) by magus.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.92) (envelope-from <9erthalion6@gmail.com>) id 1j1ElA-000600-Mm for pgsql-hackers@postgresql.org; Mon, 10 Feb 2020 19:30:27 +0000 Received: by mail-wm1-x342.google.com with SMTP id a9so516840wmj.3 for ; Mon, 10 Feb 2020 11:30:20 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=date:from:to:cc:subject:message-id:references:mime-version :content-disposition:in-reply-to; bh=95DsJopll+O1rfSbWofSB0XI3j3wERhy/WaL2TiVrMA=; b=if+8Vv9ENtTtlg55TCwabN7NIYuf+mL9CaKSgeCPFtBWGt473YkkrIc2Yfo+np0kTG tamrS32ulE/Q4PkSTQQX3osfCKuR/YHFFV7yo8UhJ0FXJ9civ8zrkCyg3bxz4i3iIlBd FcikwPCYcU0pIVAHeHO8rXCA+UrkE8kbwVdVimEVRjOLSEiFzIjOZ3mcnhUII6QajzTT M/OHi4mGXg92PMLUwER83yslEyQszkP6Q+wUv3RRXK8JOGJp7zIifXolRsuXtsGmuFy+ NSy37TmpukqNTHg/CvQjGT/2BRFKantVjMXjCPRJOIv0rAgcWGzkItHHnPh+WTZmIBjC f95Q== 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=95DsJopll+O1rfSbWofSB0XI3j3wERhy/WaL2TiVrMA=; b=R9XmSajH7cRKlo0XPeruPN1A84iKbczSwrLscN3kqJZi8ueFOcgUUIvHxH6MgAeLqJ MikbF9BSg9WkvAy+Vyu7xzweWVcdJ6nqSMF1mrWmE7+HmVjjpx8ob2u3r5JOBQmOB7yH tke00xFro8l8wxejxutHY+HISxpdpU1Ye5pkgfLVn8Fy9btep/PYoE087BYxuXQwyXip SwIAFLLino8meChSJUvGgXAuzNeHbDVJyT8ptEh1HB943F8aKHWmkK8SxyMI8OrNSWi1 P9Pf+rGVSyjqaRAoZAzlnn/vEzZT0f6DL8IEAzn5OsBSUGFY5tv2fsXOo16EOtNBr/nu oPNA== X-Gm-Message-State: APjAAAUUIKNf+xQ5Sw3fdC7IfMI/aOHL6xf8yTDdKK4VIyjGC1Ijnor3 ImeOqai5xmjBZJDOGHDBHv8= X-Google-Smtp-Source: APXvYqxCQWdqA595TvrUc3A0nXatkE+ctERHQl1FldG2cI5FVrF5zhye6jVO/XBwqorQMBFrg+Svdg== X-Received: by 2002:a1c:b7c4:: with SMTP id h187mr521145wmf.105.1581363018443; Mon, 10 Feb 2020 11:30:18 -0800 (PST) Received: from localhost (dslb-094-222-004-201.094.222.pools.vodafone-ip.de. [94.222.4.201]) by smtp.gmail.com with ESMTPSA id y7sm1160082wmd.1.2020.02.10.11.30.16 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Mon, 10 Feb 2020 11:30:17 -0800 (PST) Date: Mon, 10 Feb 2020 20:30:56 +0100 From: Dmitry Dolgov <9erthalion6@gmail.com> To: James Coleman Cc: Tomas Vondra , Kyotaro Horiguchi , Floris Van Nee , Peter Geoghegan , Jesper Pedersen , David Rowley , Alvaro Herrera , Thomas Munro , Rafia Sabih , Jeff Janes , Bhushan Uparkar , pgsql-hackers , Alexander Korotkov Subject: Re: Index Skip Scan Message-ID: <20200210193056.l4slz4bspqv44gqu@localhost> References: <20200205163730.eqvoxh6eb3do52r5@localhost> <20200206.102450.1514793516830965023.horikyota.ntt@gmail.com> <20200206105707.fuidmdsi6oapieoz@localhost> <20200206.212220.642002310227620136.horikyota.ntt@gmail.com> <20200207162543.zlj2tvcwy7jku6vu@localhost> <20200208142217.fhsiymg3szfnniih@development> <20200208152440.l2sllrla7utvgexr@localhost> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk > On Sat, Feb 08, 2020 at 01:31:02PM -0500, James Coleman wrote: > On Sat, Feb 8, 2020 at 10:24 AM Dmitry Dolgov <9erthalion6@gmail.com> wrote: > > > > > On Sat, Feb 08, 2020 at 03:22:17PM +0100, Tomas Vondra wrote: > > > 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. > > > > The current implementation checks if we can find the next value on the > > same page to do a shortcut instead of tree traversal and improve such > > kind of situations, but I can easily imagine that it's still not enough > > in some extreme situations. > > This is almost certainly rehashing already covered ground, but since I > doubt it's been discussed recently, would you be able to summarize > that choice (not to always get the next tuple by scanning from the top > of the tree again) and the performance/complexity tradeoffs? Yeah, this part of discussion happened already some time ago. The idea [1] is to protect ourselves at least partially from incorrect ndistinct estimations. Simply doing jumping over an index means that even if the next key we're searching for is on the same page as previous, we still end up doing a search from the root of the tree, which is of course less efficient than just check right on the page before jumping further. Performance tradeoff in this case is simple, we make regular use case slightly slower, but can perform better in the worst case scenarios. Complexity tradeoff was never discussed, but I guess everyone assumed it's relatively straightforward to check the current page and return if something was found before jumping. [1]: https://www.postgresql.org/message-id/CA%2BTgmoY7QTHhzLWZupNSyyqFRBfMgYocg3R-6g%3DDRgT4-KBGqg%40mail.gmail.com