Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.89) (envelope-from ) id 1efwak-0001Qk-Kf for pgsql-general@arkaria.postgresql.org; Sun, 28 Jan 2018 23:42:31 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.84_2) (envelope-from ) id 1efwaj-0007xk-FZ for pgsql-general@arkaria.postgresql.org; Sun, 28 Jan 2018 23:42:29 +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_SHA384:256) (Exim 4.84_2) (envelope-from ) id 1efwaj-0007xa-4C for pgsql-general@lists.postgresql.org; Sun, 28 Jan 2018 23:42:29 +0000 Received: from mail-pg0-x234.google.com ([2607:f8b0:400e:c05::234]) by magus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1efwad-0008Gt-U4 for pgsql-general@postgresql.org; Sun, 28 Jan 2018 23:42:27 +0000 Received: by mail-pg0-x234.google.com with SMTP id w17so3032663pgv.6 for ; Sun, 28 Jan 2018 15:42:23 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=lucee-org.20150623.gappssmtp.com; s=20150623; h=to:from:subject:message-id:date:user-agent:mime-version :content-language; bh=oF01iiPFrK3RGitZ6/5DHiuRgG2kPPQB810DKmtwhas=; b=S7weoFmEB3sJLkQ5mcPtBf+m2BwbOF653RsWINvzRRmI+8vpHhk3vj8BVayKYz4WcO /uUxIzXEXlLp4XnMO7wzCqKWS/VPG9EVQ+5VOuWiAJ42on4aalc4a1p12k5BuSumch7U NTyMvml3IQOOYg7qumKhnhNIwY1uRNSEJx2+432yY4G5eJ/6XHdhHaFbYYr1ESvbYzsi QZgG8H7WLjmzmceeznDPHEbgzHv7h1Rc8gYgTawS3z/OVcJR1lPOM4cB7f8iCqHn9vf0 ryf0mxd/7ynspyDrrzROH6x8NM/x8UgHuzQ1M7ub7GZlB7M4FvjYLMkLlssiB2WLXLRp WOfQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:to:from:subject:message-id:date:user-agent :mime-version:content-language; bh=oF01iiPFrK3RGitZ6/5DHiuRgG2kPPQB810DKmtwhas=; b=d1JQJkxnD55yGPYeffQgfEWEG+eYv4SQS2t2wB9Rn0LS3ozaBGO+k89Ho+uGi9ZMho 2D3tSX+Wr/HrN9Dq+pT8oXBssmX8UHP1wXyQAVeFTRS0fkYhzF5Ee2c6omxa4KmNdSwo 8kg/B7DjAs1zIafycUno5uMpSVR4MXTSkUXZQ8sV1YoSVVJO705iykquzN9XtBbed9SH 7w6/zIYBdvlwuE7jylb4FpegVqpMUflWWJRk1Nx2GQz9enIfcn7inDkJHjZxHc3/cCH/ seEgJT2BkDjOkfcH85v3jlIXCAaB9Y3OrJ8JhyzNPbAdefcfvW54RcNE2iKO8vx6twf+ eRRw== X-Gm-Message-State: AKwxytdsMOuFdgERJ7nLr94SfJCJtge4WSU0k3W39tBXyuQILlVTsSEH yi/Y6vpI1SPL/TL6UoCttToUEYjt X-Google-Smtp-Source: AH8x2262U9IAoBAWHpmkOspEvXxzLLD06swmPWwC3T+vvQiLfhGC+DSipOCXd189I/ATmlwnNnNJyw== X-Received: by 2002:a17:902:904b:: with SMTP id w11-v6mr20257395plz.41.1517182940480; Sun, 28 Jan 2018 15:42:20 -0800 (PST) Received: from [192.168.1.15] (45-19-167-161.lightspeed.irvnca.sbcglobal.net. [45.19.167.161]) by smtp.googlemail.com with ESMTPSA id z15sm16183783pgr.62.2018.01.28.15.42.18 for (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Sun, 28 Jan 2018 15:42:19 -0800 (PST) To: pgsql-general From: "Igal @ Lucee.org" Subject: How to Optimize pg_trgm Performance Message-ID: <0676f27e-118b-892b-f44d-c60ab13eb37c@lucee.org> Date: Sun, 28 Jan 2018 15:42:17 -0800 User-Agent: Mozilla/5.0 (Windows NT 10.0; WOW64; rv:52.0) Gecko/20100101 Thunderbird/52.5.2 MIME-Version: 1.0 Content-Type: multipart/alternative; boundary="------------07C66F5E3E563E920EA018A7" Content-Language: en-US List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk This is a multi-part message in MIME format. --------------07C66F5E3E563E920EA018A7 Content-Type: text/plain; charset=utf-8; format=flowed Content-Transfer-Encoding: 8bit I want to use pg_trgm for auto-suggest functionality.  I created a Materialized View with the information that I need, with the relevant columns being (keywords text, rank int).  keywords is the column from which I build the tri-grams, and rank is some popularity factor so that popular results will show up higher than less popular results given the same tri-gram distance. I want to return results in the order of [distance], [distance_word], [rank].  The input comes from the user and is not known in advance.  My query is as follows:     SELECT title         ,id         ,(input <-> keywords) AS distance         ,(input <<-> keywords) AS distance_word         ,rank     FROM  (VALUES (cast('red pill' AS text))) consts(input)         ,mv_autosuggest     ORDER BY 3, 4, 5     LIMIT 20; This gives me pretty good results, but it takes too long and is not likely to scale well. I have created two indexes but neither seem to be used: CREATE INDEX mv_autosuggest_keywords_tgrm_gist ON staging.mv_autosuggest USING gist (keywords gist_trgm_ops); CREATE INDEX mv_autosuggest_keywords_tgrm_gin ON staging.mv_autosuggest USING gin (keywords gin_trgm_ops); This is the result of explain analyze: QUERY PLAN | -------------------------------------------------------------------------------------------------------------------------------------------| Limit  (cost=356.41..356.46 rows=20 width=51) (actual time=163.132..163.135 rows=20 loops=1)                                               |   ->  Sort  (cost=356.41..372.96 rows=6619 width=51) (actual time=163.130..163.131 rows=20 loops=1)                                        |         Sort Key: (('red pill'::text <-> mv_autosuggest.keywords)), (('red pill'::text <<-> mv_autosuggest.keywords)), mv_autosuggest.rank |         Sort Method: top-N heapsort  Memory: 28kB |         ->  Seq Scan on mv_autosuggest  (cost=0.00..180.29 rows=6619 width=51) (actual time=0.263..161.289 rows=6619 loops=1)              | Planning time: 0.139 ms | Execution time: 163.174 ms | How can I improve the performance here? Thank you, Igal Sapir Lucee Core Developer Lucee.org --------------07C66F5E3E563E920EA018A7 Content-Type: text/html; charset=utf-8 Content-Transfer-Encoding: 8bit

I want to use pg_trgm for auto-suggest functionality.  I created a Materialized View with the information that I need, with the relevant columns being (keywords text, rank int).  keywords is the column from which I build the tri-grams, and rank is some popularity factor so that popular results will show up higher than less popular results given the same tri-gram distance.

I want to return results in the order of [distance], [distance_word], [rank].  The input comes from the user and is not known in advance.  My query is as follows:

    SELECT title
        ,id
        ,(input <-> keywords) AS distance
        ,(input <<-> keywords) AS distance_word
        ,rank
    FROM  (VALUES (cast('red pill' AS text))) consts(input)
        ,mv_autosuggest
    ORDER BY 3, 4, 5
    LIMIT 20;

This gives me pretty good results, but it takes too long and is not likely to scale well.

I have created two indexes but neither seem to be used:

CREATE INDEX mv_autosuggest_keywords_tgrm_gist ON staging.mv_autosuggest USING gist (keywords gist_trgm_ops);

CREATE INDEX mv_autosuggest_keywords_tgrm_gin ON staging.mv_autosuggest USING gin (keywords gin_trgm_ops);

This is the result of explain analyze:

QUERY PLAN                                                                                                                                 |
-------------------------------------------------------------------------------------------------------------------------------------------|
Limit  (cost=356.41..356.46 rows=20 width=51) (actual time=163.132..163.135 rows=20 loops=1)                                               |
  ->  Sort  (cost=356.41..372.96 rows=6619 width=51) (actual time=163.130..163.131 rows=20 loops=1)                                        |
        Sort Key: (('red pill'::text <-> mv_autosuggest.keywords)), (('red pill'::text <<-> mv_autosuggest.keywords)), mv_autosuggest.rank |
        Sort Method: top-N heapsort  Memory: 28kB                                                                                          |
        ->  Seq Scan on mv_autosuggest  (cost=0.00..180.29 rows=6619 width=51) (actual time=0.263..161.289 rows=6619 loops=1)              |
Planning time: 0.139 ms                                                                                                                    |
Execution time: 163.174 ms                                                                                                                 |

How can I improve the performance here?

Thank you,

Igal Sapir
Lucee Core Developer
Lucee.org

--------------07C66F5E3E563E920EA018A7--