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 1mCWn2-0007j2-GG for pgsql-performance@arkaria.postgresql.org; Sun, 08 Aug 2021 00:35:44 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1mCWmz-0003un-6C for pgsql-performance@arkaria.postgresql.org; Sun, 08 Aug 2021 00:35:41 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1mCWmy-0003uf-Qo for pgsql-performance@lists.postgresql.org; Sun, 08 Aug 2021 00:35:40 +0000 Received: from mail-io1-xd32.google.com ([2607:f8b0:4864:20::d32]) by magus.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.92) (envelope-from ) id 1mCWmq-0004fL-Tx for pgsql-performance@postgresql.org; Sun, 08 Aug 2021 00:35:40 +0000 Received: by mail-io1-xd32.google.com with SMTP id 188so18846258ioa.8 for ; Sat, 07 Aug 2021 17:35:32 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=telsasoft-com.20150623.gappssmtp.com; s=20150623; h=date:from:to:cc:subject:message-id:references:mime-version :content-disposition:content-transfer-encoding:in-reply-to :user-agent; bh=8jMJVafzliFg3rQbMulbL7draYacSnMUlA8t+X+iXIo=; b=KujAj2JP5WPLqgkeVa86wRrwy4OAQ+yIp0NPgpepDpL8uuJBmckUj9QCRd08ugzMc8 TsZGt7AwyF3MMkszoakhVfNo6B5BRKaRKTXnnSmwdZ5+3Bu/SNUna8dwoq/OSUfGKNPT WNTk2XQ3hJpNLVuBhQVhcGw1oHJGXj5eNf8gITNEkUwHo7oqMTX+i7cwdcnImkB9zhvx EDiQeKV6Abf6cVuBLUAv7dTYtzBXALTgVLGwBx/Z50/LGcS+hSZegjLeGLissaqV4lHl JOaPJ+ykGmjUECirg15QDAlZLzOLxOID8OwQTNGuF5ooGRKkGErGoJqs3SyQd2Xai59X rQvg== 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:content-transfer-encoding :in-reply-to:user-agent; bh=8jMJVafzliFg3rQbMulbL7draYacSnMUlA8t+X+iXIo=; b=R0gOn9XWfG46dwdZnG5E0e6bM8w0cZCd9CDX+c5AQ9HSszuJJCzOymx0TnyIZPUYB9 b6weXl9WoEpy39PXplFd88dI/4H5Toq6DA/T41OlRD718sh60caY/oLeg//1CDez0arY BMZu/vkNlPitJagoE91OJIUb8bFQlPLmHNukVLYB9J59wFPxI0cYKTVZvxg3/IWItiuH EeBcDZXoADMx/11sEHmzHEIkUwKJlmu9nMUWmghtL1Lux0zuyesXIR+U3xZ/WDyJVG2L pTc8PMBXW96VmVq8NwjyoFvTbvQVQNOi3M8mZqb6aIQ9sH+dTXugdC8+d+FABwb8EI0v 5ufA== X-Gm-Message-State: AOAM531fEKUvLPurAtXRPRQEMRhspLTG1xWtpFWO/BgMZcix1VlKfOfU A84HeJmD3qosWx7VaiSUzBZ1oz650XLQ9A== X-Google-Smtp-Source: ABdhPJxx7JkdDrDCHaATOhLasecAPVIOhnk0/6oZ5MICxPw99d2FP3cltrqG/RiTPDlSYJBxL3MsfA== X-Received: by 2002:a6b:6d16:: with SMTP id a22mr27609iod.209.1628382930442; Sat, 07 Aug 2021 17:35:30 -0700 (PDT) Received: from pryzbyj.telsasoft (charmander.telsasoft.com. [50.244.222.1]) by smtp.gmail.com with ESMTPSA id i6sm7425235ilq.4.2021.08.07.17.35.29 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Sat, 07 Aug 2021 17:35:30 -0700 (PDT) Received: by pryzbyj.telsasoft (Postfix, from userid 1000) id 0AE9E800BC8; Sat, 7 Aug 2021 19:35:28 -0500 (CDT) Date: Sat, 7 Aug 2021 19:35:28 -0500 From: Justin Pryzby To: Alex Cc: pgsql-performance@postgresql.org Subject: Re: Slow query because lexeme index not used Message-ID: <20210808003528.GZ10479@telsasoft.com> References: <1557921338.669347.1628364925994.ref@mail.yahoo.com> <1557921338.669347.1628364925994@mail.yahoo.com> MIME-Version: 1.0 Content-Type: text/plain; charset=iso-8859-1 Content-Disposition: inline Content-Transfer-Encoding: 8bit In-Reply-To: <1557921338.669347.1628364925994@mail.yahoo.com> 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 Sat, Aug 07, 2021 at 07:35:25PM +0000, Alex wrote: > Table "product" has a GIN index on "lexeme" column (tsvector) that is not used. > > Query that doesn't use lexeme idx:  https://explain.dalibo.com/plan/BlB#plan, ~8s, ~60.000 blocks needed > > Query forced to use lexeme idx: https://explain.dalibo.com/plan/i52, ~800ms (10x less), ~15.000 blocks needed (x4 less) Could you show the table stats for product.id ? In particular its "correlation". I guess the correlation is ~1, and the 10,659 index scans on product.id are considered to be cheaper than scannning the lexeme index - since there are no correlation stats for tsvector. How large is shared_buffers ? Does the query plan improve if you increase work_mem ? Maybe you could encourage scanning in order of product_property.product. You could CLUSTER product_property_default on an index on "product" and then ANALYZE. Or you could write the query with a temp table: CREATE TEMP TABLE product_ids AS SELECT product FROM product_property WHERE "meaning" = 'B' AND "first" = 1.7179869184E10 GROUP BY 1 -- or DISTINCT, because the table is only used for EXISTS ORDER BY 1; -- to scan product in order of id ANALYZE product_ids; The index scans on product.id should be faster when you use EXISTS(SELECT 1 FROM product_ids ...), even though it didn't use the lexeme index. Maybe it would help to create stats on "first" and "meaning"; the rowcount is underestimated by 3x, which means it did several times more index scans into "product" than planned. | Bitmap Heap Scan on product_property_default product_property_default (cost=2,748.6..8,823.4 rows=6,318 width=4) (actual time=43.945..211.621 rows=21,061 loops=1) CREATE STATISTICS first_meaning ON first,meaning FROM product_property; ANALYZE product_property; > Table metdata: >          relname          | relpages | reltuples | relallvisible | relkind | relnatts | relhassubclass | reloptions | pg_table_size > --------------------------+----------+-----------+---------------+---------+----------+----------------+------------+--------------- >  product_property_default |     8992 |    622969 |          8992 | r       |       16 | f              |            |      73719808 >  product                  |    49686 |    413840 |         49686 | r       |       14 | f              |            |     493314048 > > Table stats: >    frac_mcv    |        tablename         | attname | inherited | null_frac | n_distinct  | n_mcv | n_hist | correlation > ---------------+--------------------------+---------+-----------+-----------+-------------+-------+--------+------------- >                | product                  | lexeme  | f         |         0 |          -1 |       |        | >     0.99773335 | product_property_default | meaning | f         |         0 |          63 |    39 |     24 |  0.19444875 >      0.6416333 | product_property_default | first   | f         |         0 |        2193 |   100 |    101 | -0.09763639 >  0.00023333334 | product_property_default | product | f         |         0 | -0.15221785 |     1 |    101 |  0.08643274 > > > Using windows docker with wsl2.Both cases are run with cold cache.All database memory is limited to 1GB by using .wslconfig file with memory=1GB, also the docker container is limited to 1GB. > My requirement is to optimize disk access with this limited memory