Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1pVbjc-0000T0-EW for pgsql-performance@arkaria.postgresql.org; Fri, 24 Feb 2023 17:19:52 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1pVbjb-0007sb-6s for pgsql-performance@arkaria.postgresql.org; Fri, 24 Feb 2023 17:19:51 +0000 Received: from makus.postgresql.org ([2001:4800:3e1:1::229]) by malur.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1pVbja-0007sR-QL for pgsql-performance@lists.postgresql.org; Fri, 24 Feb 2023 17:19:50 +0000 Received: from mail-io1-xd2e.google.com ([2607:f8b0:4864:20::d2e]) by makus.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.92) (envelope-from ) id 1pVbjW-0004Wq-MI for pgsql-performance@postgresql.org; Fri, 24 Feb 2023 17:19:49 +0000 Received: by mail-io1-xd2e.google.com with SMTP id e17so3236525iot.1 for ; Fri, 24 Feb 2023 09:19:46 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=telsasoft-com.20210112.gappssmtp.com; s=20210112; h=user-agent:in-reply-to:content-disposition:mime-version:references :message-id:subject:cc:to:from:date:from:to:cc:subject:date :message-id:reply-to; bh=BmTHrwrNAiR3ckJOO/H2BukKdT2lBKS5z2uo+efefw4=; b=vh5GT6a25K9tduVf++TVBDfwi2nzPaOqVQgDES0p4rCluscjXx9/Ixka88YLXyMwiX JFfaNKOZ9Fa3Ic73ZkmNp1EBTt541lZDFvdJMJJq57sJKo+UqE92bFtBv7wWsgyw5V3B 45w5jrCgjyE5CgnsZys0W2Eq2pP4XK28LEipbgEPHV1uAcluBzvwtZbGeY1ePBlg29+V 4yxO3vqqVE2wpIIv9Gu/UAI2+GeRKxFfveoZ/uV4q0Ite2ik7GRyByHjdZ1wuHnX9S2z RfHjjbS8YZWVC2Z4ldF2FWVAtBbB2KqhMv7rKc9ACzLODHUFr4/MIdSVXLB76BAtsW3n 5O+A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=user-agent:in-reply-to:content-disposition:mime-version:references :message-id:subject:cc:to:from:date:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=BmTHrwrNAiR3ckJOO/H2BukKdT2lBKS5z2uo+efefw4=; b=7U94BuSNkejzce6WjIu+zERuyJ6QhXyiU0j6IxAUOiIKj3HOQAPAPFKLRbFTKCPT/C 0jIAwI7r0fVx6jlSxqadKm1h7FfwZgnmQ5GQeP6xqyHKT7VPdbC5RGTavvrj0GzTK+30 SzsED8x+/JWBJqlVNVkHHoE2U5+a81e3KLCp7O54TbSB8UrvCN4GMZCSsDSkp7lz2kwz Ux7Xs9ZEgRk1b1hSg6A70UJVg/5SIqlGXS9mlaxwMklClBQKEK4bHWXK8w0ZNpijoWL7 4C81PGPU6rJ5LPdF650FrTwfwAEyeQZaAPG/lIdfrZhAkL5QZoId/fkZudFunPqRHJfL j1Cg== X-Gm-Message-State: AO0yUKU8UKfQoGcG9YNdoFdHlh+uV9bT/gDUWw2EsYt4tBTHgRyY5HQC Pm7kVCjocXCx0Yxu69ItLCrrWw== X-Google-Smtp-Source: AK7set9C4upPvUl7p2a2wyhCQ+SxgiFjFSFHN69DfDDzluSzOhuBjdyJsCVnpUypr8q8Vk8gaDUeaw== X-Received: by 2002:a05:6602:15ce:b0:74c:9b9a:2d17 with SMTP id f14-20020a05660215ce00b0074c9b9a2d17mr439136iow.0.1677259185807; Fri, 24 Feb 2023 09:19:45 -0800 (PST) Received: from pryzbyj.telsasoft (charmander.telsasoft.com. [50.244.222.1]) by smtp.gmail.com with ESMTPSA id s10-20020a6bdc0a000000b0074c8a021d4csm3388244ioc.44.2023.02.24.09.19.45 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 24 Feb 2023 09:19:45 -0800 (PST) Received: by pryzbyj.telsasoft (Postfix, from userid 1000) id 639D3800BEC; Fri, 24 Feb 2023 11:19:44 -0600 (CST) Date: Fri, 24 Feb 2023 11:19:44 -0600 From: Justin Pryzby To: Mickael van der Beek Cc: pgsql-performance@postgresql.org Subject: Re: BRIN index worse than sequential scan for large search set Message-ID: <20230224171944.GB1653@telsasoft.com> References: MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: User-Agent: Mutt/1.9.4 (2018-02-28) List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk On Fri, Feb 24, 2023 at 05:40:55PM +0100, Mickael van der Beek wrote: > Hello everyone, > > I'm playing around with BRIN indexes so as to get a feel for the feature. > During my tests, I was unable to make BRIN indexes perform better than a > sequential scan for queries searching for large value sets (20K values in > the example down below). > And now let's query 20K random rows from our 20M total rows: I didn't try your test, but I think *random* is the problem/explanation. > By default, this query will not use the BRIN index and simply run a 1.5s > long sequential scan (hitting 700 MB) and a 2.47s hash join for a total > 8.7s query time: > https://explain.dalibo.com/plan/46c3191g8a6c1bc7 > If we force the use of the BRIN index using (`SET LOCAL enable_seqscan = > OFF;`) the same query will now take 50s with 2.5s spent on the bitmap index > scan (hitting 470 MB of data) and a whopping 42s on the bitmap heap scan > (hitting 20 GB of data!): > https://explain.dalibo.com/plan/7f73bg9172a8b226 That means the planner's cost model correctly preferred a seq scan. > So I had the following two questions: > > 1. Why is the BRIN index systematically worse than a sequential scan, > even when the table is x1000 larger than the search set, physically > pre-sorted, dense (fillfactor at 100%) and the search rows are themselves > sorted? The table may be dense, but the tuples aren't. You're asking to return 1/1000th of the tuples, across the entire table. Suppose there are ~100 tuples per page, and you need to read about every 10th page. It makes sense that it's slow to read a large amount of data nonsequentially. That's why random_page_cost is several times higher than seq_page_cost. I would expect brin to win if the pages to be accessed were dense rather than distributed across the whole table. > 2. Since we only select the "idx" column, why does the BRIN index not > simply return the searched value if included in one of it's ranges? > Hitting the actual row data stored in the table seems to be unnessary no? Because it's necessary to check if the tuple is visible to the current transaction. It might be from an uncommited/aborted transaction. -- Justin