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 1ldf1O-0006DE-Ci for pgsql-performance@arkaria.postgresql.org; Mon, 03 May 2021 20:18:26 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1ldf1N-0002y1-6N for pgsql-performance@arkaria.postgresql.org; Mon, 03 May 2021 20:18:25 +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 1ldf1M-0002uT-Me for pgsql-performance@lists.postgresql.org; Mon, 03 May 2021 20:18:24 +0000 Received: from mail-ot1-x336.google.com ([2607:f8b0:4864:20::336]) by magus.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.92) (envelope-from ) id 1ldf1F-0002sR-8L for pgsql-performance@postgresql.org; Mon, 03 May 2021 20:18:24 +0000 Received: by mail-ot1-x336.google.com with SMTP id q7-20020a9d57870000b02902a5c2bd8c17so5608638oth.5 for ; Mon, 03 May 2021 13:18:16 -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:in-reply-to:user-agent; bh=oxFyl39Ropk88y++kQLCjsPakfnNWXIyt2/U96bMsdI=; b=u7kGfmkIudGnzFHalvMj5J90v/ZnQjHmyJPNm4y7JrpOEJ2Q7lRmdfgWsjlFBt73si 5XyyzSMYBySGOz9/at/F4oyQYc+iN9F7VFWMaoX/Qa33tnf2SC8CpE1oAlFzhGpcjujW WBucM1vOeWN4tEjE9qCOYE62mU7Msa8FuSRmBSZdwg/qT0USa4fQ7q0a49wliBfDMTCs MAisDOYkAXYMjuESWC1McDXXa4P4rlcM9YOP61eFhF/AuOx/VM1lQmwvW51vw3DXryyc aFPj9JaGP93uERWf9WyhVXjboE8zokgIHzaScsYIecNNNn2r3d043j7B0GXlGEolomFL wdog== 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:user-agent; bh=oxFyl39Ropk88y++kQLCjsPakfnNWXIyt2/U96bMsdI=; b=lp5CuYMXJKzIsUxkEo58NKrzxiZvCRaMN2YOT/kwQ4y5xFwwFjt5tferu9oe82Qa3i FDLoFNX+dpn8h4r4fcUlSnSoLKYwcIEFzE9czec4FFHW7mXRuRFNqETbF3ge8yI3YXvd weAb24gToCb4cbHihUDJUMhHLSQT8KBJkoWQB/TXwLinLO8XKtDdCJWI4v3dGPCmb8EZ yrvg8vTLLXYbJm7acw/2w7RGVHM/fiGSHVWQ8TApVTzmold9743HknnAaBHof32MejFU zejbqrPjszctJZGUcAHwdMQOwgN+gh0ioCKCNUH8Bxvbz0u0mk72F3IvWlm72S8ZE6SB gYbQ== X-Gm-Message-State: AOAM533c80dfXbqdhHknCvCIBWfShY4EXjmbSaNGLr89vW8HeyaB3jYD wY3uH19OAzwAC2YG3JVHMIjYDRU8HDZaBA== X-Google-Smtp-Source: ABdhPJzhbzO9tOR+/wXEcbTHpbSHLH8609WwJX93QBRP0eXf5w+ePsB63h+FUQ+4biJEaveqEyKLwg== X-Received: by 2002:a9d:6317:: with SMTP id q23mr15523582otk.151.1620073093955; Mon, 03 May 2021 13:18:13 -0700 (PDT) Received: from pryzbyj.telsasoft (charmander.telsasoft.com. [50.244.222.1]) by smtp.gmail.com with ESMTPSA id q184sm197067oic.35.2021.05.03.13.18.13 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Mon, 03 May 2021 13:18:13 -0700 (PDT) Received: by pryzbyj.telsasoft (Postfix, from userid 1000) id 39A438009E1; Mon, 3 May 2021 15:18:12 -0500 (CDT) Date: Mon, 3 May 2021 15:18:12 -0500 From: Justin Pryzby To: Alex Cc: pgsql-performance@postgresql.org Subject: Re: 15x slower PreparedStatement vs raw query Message-ID: <20210503201811.GA27406@telsasoft.com> References: <3f8c88d8a6ef9.d82fa04cf610d.ref@yahoo.com> <3f8c88d8a6ef9.d82fa04cf610d@yahoo.com> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <3f8c88d8a6ef9.d82fa04cf610d@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 Sun, May 02, 2021 at 07:45:26PM +0000, Alex wrote: > PreparedStatement: 15s > Raw query with embedded params: 1s > See issue on github with query and explain analyze: > https://github.com/pgjdbc/pgjdbc/issues/2145 | ..PostgreSQL Version? 12 |Prepared statement |... |Planning Time: 11.596 ms |Execution Time: 14799.266 ms | |Raw statement |Planning Time: 22.685 ms |Execution Time: 1012.992 ms The prepared statemnt has 2x faster planning time, which is what it's meant to improve. The execution time is slower, and I think you can improve it with this. https://www.postgresql.org/docs/12/runtime-config-query.html#GUC-PLAN-CACHE_MODE |plan_cache_mode (enum) | Prepared statements (either explicitly prepared or implicitly generated, for example by PL/pgSQL) can be executed using custom or generic plans. Custom plans are made afresh for each execution using its specific set of parameter values, while generic plans do not rely on the parameter values and can be re-used across executions. Thus, use of a generic plan saves planning time, but if the ideal plan depends strongly on the parameter values then a generic plan may be inefficient. The choice between these options is normally made automatically, but it can be overridden with plan_cache_mode. The allowed values are auto (the default), force_custom_plan and force_generic_plan. This setting is considered when a cached plan is to be executed, not when it is prepared. For more information see PREPARE. -- Justin