Postgresql 为什么java应用程序查询速度慢,但在PgAdmin中速度快?

Postgresql 为什么java应用程序查询速度慢,但在PgAdmin中速度快?,postgresql,pgadmin,pgadmin-4,Postgresql,Pgadmin,Pgadmin 4,相当复杂的查询的计时: 网络应用程序:12秒 pgAdmin4(5.2版):700ms(加速比超过12倍!) 为什么会有这种差异? 查询只返回10行,其中8列 使用VisualVm对我的应用程序进行采样,结果显示我的应用程序在套接字读取中花费了整个查询时间,因此瓶颈一定在postgres一侧 背景: 创建临时表temp(一列id integer) 在临时表上创建索引 在复杂的select查询中使用temp表 即使不使用临时表,我仍然可以得到7倍的加速差 jdbcdriver'org.post

相当复杂的查询的计时:

  • 网络应用程序:12秒
  • pgAdmin4(5.2版):700ms(加速比超过12倍!)
为什么会有这种差异? 查询只返回10行,其中8列

使用VisualVm对我的应用程序进行采样,结果显示我的应用程序在套接字读取中花费了整个查询时间,因此瓶颈一定在postgres一侧

背景:

  • 创建临时表
    temp
    (一列
    id integer
  • 在临时表上创建索引
  • 在复杂的select查询中使用temp表
  • 即使不使用临时表,我仍然可以得到7倍的加速差

    jdbcdriver'org.postgresql:postgresql:42.2.20'

    博士后12

    编辑 差异似乎是由于
    PreparedStatement
    造成的。使用原始sql文本(嵌入参数)发出查询的效果与预期一样。
    相关:

    通过一条准备好的语句,PostgreSQL缓存查询计划,并最终使用独立于参数值的“通用”计划

    要强制PostgreSQL始终使用自定义计划,请将
    plan\u cache\u mode
    设置为
    force\u custom\u plan


    如果您使用的是临时表,在使用它们之前分析它们可能是一个好主意。

    我认为在
    postgresql.conf
    中设置
    plan\u cache\u mode=force\u custom\u plan
    会降低受益于查询计划缓存的其他快速查询的性能。这是绝对的。您只想在需要的会话/函数/事务中设置它。
    SELECT DISTINCT min("baseprop"."first"), max("baseprop"."first"), max("baseprop"."second"), "baseprop"."type", "join1name"."lexeme", count(DISTINCT "baseprop"."first"), , (array_agg(DISTINCT "baseprop"."first"))[1:10], "baseprop"."meaning"
    FROM "base"
    JOIN "temp" ON "temp"."id" = "base"."id"
    JOIN "baseprop" ON "baseprop"."base" = "base"."id"
    LEFT OUTER JOIN "join1" ON "baseprop"."join1" = "join1"."id"
    LEFT OUTER JOIN "join1name" ON "join1name"."owner" = "join1"."id"
    LEFT OUTER JOIN "join2" ON "join2"."id" = "join1"."join2"
    WHERE (("baseprop"."meaning" = 'm1'
            AND (("join1name"."lexeme" IN ((strip((to_tsvector('en', 'name11')))), (strip((to_tsvector('en', 'name12')))), (strip((to_tsvector('en', 'name13')))))
                  AND "join1"."meaning" = 'm1')
                 OR ("join1name"."lexeme" IN ((strip((to_tsvector('en', 'name21')))))
                     AND "join1"."meaning" = 'm1')
                 OR ("join1name"."lexeme" IN ((strip((to_tsvector('en', 'name22')))))
                     AND "join1"."meaning" = 'm1')
                 OR ("join1name"."lexeme" IN ((strip((to_tsvector('en', 'name23')))))
                     AND "join1"."meaning" = 'm1'))
            AND "join2"."country" = 'en'::country)
           OR ("baseprop"."meaning" = 'm2'
               AND (("join1name"."lexeme" IN ((strip((to_tsvector('en', 'name31')))), (strip((to_tsvector('en', 'name32')))), (strip((to_tsvector('en', 'name33')))), (strip((to_tsvector('en', 'name34')))))
                     AND "join1"."meaning" = 'm2')
                    OR ("join1name"."lexeme" IN ((strip((to_tsvector('en', 'name41')))))
                        AND "join1"."meaning" = 'm2')
                    OR ("join1name"."lexeme" IN ((strip((to_tsvector('en', 'name51')))))
                        AND "join1"."meaning" = 'm2'))
               AND "join2"."country" = 'en'::country)
           OR ("baseprop"."meaning" = 'm3'
               AND "join1name"."lexeme" IN ((strip((to_tsvector('en', 'name61')))))
               AND "join1"."meaning" = 'm3'
               AND "join2"."country" = 'en'::country)
           OR ("baseprop"."meaning" = 'm4'
               AND "join1name"."lexeme" IN ((strip((to_tsvector('en', 'name71')))))
               AND "join1"."meaning" = 'm4'
               AND "join2"."country" = 'en'::country))
    GROUP BY "baseprop"."meaning",
             "baseprop"."type",
             "join1name"."lexeme"