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 1jPRMg-0000Wr-FB for pgsql-general@arkaria.postgresql.org; Fri, 17 Apr 2020 13:49:06 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1jPRMe-0003H9-C1 for pgsql-general@arkaria.postgresql.org; Fri, 17 Apr 2020 13:49:04 +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 1jPRMd-0003EB-Tu for pgsql-general@lists.postgresql.org; Fri, 17 Apr 2020 13:49:04 +0000 Received: from mail-il1-x131.google.com ([2607:f8b0:4864:20::131]) by makus.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.92) (envelope-from ) id 1jPRMb-00014a-MZ for pgsql-general@lists.postgresql.org; Fri, 17 Apr 2020 13:49:02 +0000 Received: by mail-il1-x131.google.com with SMTP id t10so2144222ilg.9 for ; Fri, 17 Apr 2020 06:49:01 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=content-transfer-encoding:from:mime-version:subject:date:message-id :references:cc:in-reply-to:to; bh=G35q1pICxHzS2k6mDkh8rVUSGR80WRTNMClobX2jMcA=; b=Q1OFbJwrZwJi6qFkohih8vM9LwlFl1ak5IfngQC4Q/yuexumvutUEJJD9/0tSbpmkD T4o1kQEQ5pK2XC8zfQ67eUzdbLdBqQufG4o49Rdm++TGAnLMt3rUWoDLwjBRTB90Z2X6 RcAMH8X/PLKdcpHvTvoz6INgUwcCf9uXKaANyCnBTh9w8qO14O+imH7AIYcgxZ27AnxP jbeSON0/8jLAZyDNrMcX76Vr352iSc3QfI41EZZaV2yDbobHmlfx6lshQKbK/piuuCfF Px+4NP+UCX1jIszse1iiQdGxcBfSAmqOhhLuA6waihK5Ds+i0t2nkIt6QDr7We8pyRe2 NHTg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:content-transfer-encoding:from:mime-version :subject:date:message-id:references:cc:in-reply-to:to; bh=G35q1pICxHzS2k6mDkh8rVUSGR80WRTNMClobX2jMcA=; b=YadRP/6j5Al0jzhJ+/HVGaTUdQ5S+L0Lepb8oErTMWdf3zZ/ttjAbBdLE7H40T2pzR UVQts/IwjIpsKhtId3lpoezbLlx4b0EkPIRhiq2/2FCzVKFpY1gxPvKUtWFEoenGG7h7 C6EiFHUPl6wXBM1VQ1JSN0MXEN6i1JzMf/Z9P+zCWApN8OBR6AHMG2RvqCRUAEKdgKBi eRy7CNvH2ll16xhEuj+9Rfw9uOt9Xqi8Xn9LYsRl8L48f61mN2V+yXjcxZYEK+qXd2p2 Qw7atvGFrmBIYaQgi3X+UcJY3hIlGcrXzqoFnOWcz2oyMRNQFs/O3c6hDpMCnQuM5Io2 lZ+Q== X-Gm-Message-State: AGi0PuanyjmlK3CHKeE9Pp7YZKpgaoStJ0p1b7xt3rssAonMedZe4RK1 ng0H35txJjGrBS5ppyphvvhwxK7PDes= X-Google-Smtp-Source: APiQypLQCjTIqfDexTsaAYTt3X2OjGRh+MTKAAhJSFN7PkTHYgEONzJdR0nWJYVB4tODRQfkaPa1pQ== X-Received: by 2002:a92:dc8d:: with SMTP id c13mr2849522iln.278.1587131340628; Fri, 17 Apr 2020 06:49:00 -0700 (PDT) Received: from ?IPv6:2601:681:5500:dde0:1460:8da3:27e4:7a2e? ([2601:681:5500:dde0:1460:8da3:27e4:7a2e]) by smtp.gmail.com with ESMTPSA id p22sm7020776ioh.11.2020.04.17.06.48.59 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Fri, 17 Apr 2020 06:49:00 -0700 (PDT) Content-Type: multipart/alternative; boundary=Apple-Mail-A7ED3135-7931-46D0-84CF-709CD4309A9B Content-Transfer-Encoding: 7bit From: Rob Sargent Mime-Version: 1.0 (1.0) Subject: Re: performance of first exec of prepared statement Date: Fri, 17 Apr 2020 07:48:59 -0600 Message-Id: <6E38A116-75BB-462A-8D72-50C7FC8D7A25@gmail.com> References: Cc: pgsql-general@lists.postgresql.org In-Reply-To: To: Ted Toth X-Mailer: iPhone Mail (17D50) List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk --Apple-Mail-A7ED3135-7931-46D0-84CF-709CD4309A9B Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: quoted-printable > On Apr 17, 2020, at 7:34 AM, Ted Toth wrote: >=20 > =EF=BB=BF >=20 >=20 >> On Fri, Apr 17, 2020 at 8:28 AM Ted Toth wrote: >>=20 >>=20 >>> On Thu, Apr 16, 2020 at 8:09 PM Rob Sargent wrot= e: >>> On 4/16/20 6:15 PM, Adrian Klaver wrote: >>> > On 4/16/20 4:59 PM, Ted Toth wrote: >>> >> >>> >> >>> >> On Thu, Apr 16, 2020 at 6:29 PM Ted Toth >> >> > wrote: >>> >> >>> >> I've noticed that the first exec of an INSERT prepared statement >>> >> takes ~5 time longer (I'm using libpq in C and wrapping the calls= to >>> >> time them) then subsequent exec's is this the expected behavior a= nd >>> >> if so is there any thing I can do to mitigate this affect? >>> >> >>> >> Ted >>> >> >>> >> >>> >> For example (in my environment) I'm seeing the prepare take ~10ms,=20= >>> >> the first exec take ~30 ms and subsequent exec's take ~4 ms. >>> >> >>> > >>> > I don't have an answer. I believe though that to help those that might= =20 >>> > it would be helpful to show the actual code. >>> > >>> > >>> You expect the subsequent calls to benefit from the cached query parse=20= >>> and planning. What does you query cost without begin wrapped in a=20 >>> prepared statement (preferably from a cold start). >>>=20 >>>=20 >>>=20 >> I thought that's what the PQprepare call was supposed to do i.e. parsing/= planning. >>=20 >> It's a bit difficult to get an unprepared query cost since there are a lo= t of columns :( >> #define INSERT_SQL "INSERT INTO t () VALUES ($1,$2,$3,$4,$5= ,$6,$7,$8,$9,$10,$11,$12,$13,$14,$15,$16,$17,$18,$19,$20,$21,$22,$23,$24,$25= ,$26,$27,$28,$29,$30,$31,$32,$33,$34,$35,$36,$37,$38,$39,$40,$41,$42,$43,$44= ,$45,$46,$47,$48,$49,$50,$51,$52,$53,$54,$55,$56,$57,$58,$59,$60,$61,$62,$63= ,$64,$65,$66,$67,$68,$69,$70,$71,$72,$73,$74,$75,$76,$77,$78,$79,$80,$81,$82= ,$83,$84,$85,$86,$87);"=20 >>=20 > Ah prepare does the parsing and execute does the planning. >=20 > Another related question is that my implementation uses strings for all va= lues is there a performance benefit to using actual values and specifying th= eir datatype? The cost in this example is 87-N toSting() calls on client plus 87-N valueOf= (Sting) calls on server where N is the number of textual columns >=20 --Apple-Mail-A7ED3135-7931-46D0-84CF-709CD4309A9B Content-Type: text/html; charset=utf-8 Content-Transfer-Encoding: quoted-printable


On Apr 17, 2020, at 7:34 AM, Ted Toth <txt= oth@gmail.com> wrote:

=EF=BB=BF


<= div class=3D"gmail_quote">
On Fri, Apr 1= 7, 2020 at 8:28 AM Ted Toth <txtoth@g= mail.com> wrote:


On Thu, Apr 16, 2020 at 8:09 PM R= ob Sargent <ro= bjsargent@gmail.com> wrote:
On 4/16/20 6:15 PM, Adrian Klaver wrote:
> On 4/16/20 4:59 PM, Ted Toth wrote:
>>
>>
>> On Thu, Apr 16, 2020 at 6:29 PM Ted Toth <txtoth@gmail.com
>> <mailto:tx= toth@gmail.com>> wrote:
>>
>>     I've noticed that the first exec of an INSERT pr= epared statement
>>     takes ~5 time longer (I'm using libpq in C and w= rapping the calls to
>>     time them) then subsequent exec's is this the ex= pected behavior and
>>     if so is there any thing I can do to mitigate th= is affect?
>>
>>     Ted
>>
>>
>> For example (in my environment) I'm seeing the prepare take ~10ms, <= br> >> the first exec take ~30 ms and subsequent exec's take ~4 ms. >>
>
> I don't have an answer. I believe though that to help those that might <= br> > it would be helpful to show the actual code.
>
>
You expect the subsequent calls to benefit from the cached query parse
and planning.  What does you query cost without begin wrapped in a
= prepared statement (preferably from a cold start).



I thought that's what the PQprepare call was supposed t= o do i.e. parsing/planning.

It's a bit difficul= t to get an unprepared query cost since there are a lot of columns :(
<= div>#define INSERT_SQL "INSERT INTO t (<column names>) VALUES ($1,$2,$= 3,$4,$5,$6,$7,$8,$9,$10,$11,$12,$13,$14,$15,$16,$17,$18,$19,$20,$21,$22,$23,= $24,$25,$26,$27,$28,$29,$30,$31,$32,$33,$34,$35,$36,$37,$38,$39,$40,$41,$42,= $43,$44,$45,$46,$47,$48,$49,$50,$51,$52,$53,$54,$55,$56,$57,$58,$59,$60,$61,= $62,$63,$64,$65,$66,$67,$68,$69,$70,$71,$72,$73,$74,$75,$76,$77,$78,$79,$80,= $81,$82,$83,$84,$85,$86,$87);" 

Ah prepare does the parsing and execute does the planning.

Another related question is that my implementation u= ses strings for all values is there a performance benefit to using actual va= lues and specifying their datatype?

The cost in this example is 87-N toSting() calls on client p= lus 87-N valueOf(Sting) calls on server where N is the number of textual col= umns

= --Apple-Mail-A7ED3135-7931-46D0-84CF-709CD4309A9B--