jooq针对postgres生成查询的性能问题

时间:2016-10-31 21:18:01

标签: java performance postgresql jooq

我有一个jooq生成的查询的性能问题,它应该等于普通的字符串查询。 jooq查询如下所示:

return ctx.select(AT_TRAIL.POINT)
        .from(AT_TRAIL)
        .where(AT_TRAIL.ID.le(
                ctx.select(AT_TRAIL.ID)
                .from(
                        ctx.select(AT_TRAIL.ID, AT_TRAIL.POINT, field(
                                "point <-> ( " +
                                "  select point " +
                                "    from at_shelter  " +
                                "   where id = ( " +
                                "          select at_shelter " +
                                "            from at_last_shelter " +
                                "        ) "+
                                ")"
                        ).as("dist"))
                        .from(AT_TRAIL)
                        .orderBy(field("dist").asc())
                        .limit(1)
                )
        ))
        .orderBy(AT_TRAIL.ID.asc())
        .fetch()
        .map(r -> {
            PGpoint point = r.get(AT_TRAIL.POINT, PGpoint.class);
            return ImmutableMap.of("lat", point.x, "lng", point.y);
        });

我的普通字符串查询看起来像这样

return ctx.fetch(
        "  select point " +
        "    from at_trail " +
        "   where id <= ( " +
        "          select id " +
        "            from ( " +
        "                  select id, point, point <-> ( " +
        "                          select point " +
        "                            from at_shelter  " +
        "                           where id = ( " +
        "                                  select at_shelter " +
        "                                    from at_last_shelter " +
        "                                ) " +
        "                        ) as dist " +
        "                    from at_trail " +
        "                order by dist asc  " +
        "                   limit 1 " +
        "                ) t " +
        "        ) " +
        "order by id asc"
)
.map(r -> {
    PGpoint point = r.get(AT_TRAIL.POINT, PGpoint.class);
    return ImmutableMap.of("lat", point.x, "lng", point.y);
});

我将jooq生成的查询与另一个查询进行了比较。它们在表别名中有所不同。 jooq生成as "alias_108340908",而我只使用t。并且jooq完全定义了"public"."at_trail"."point"之类的列名和表。否则两个查询是相同的。但是,使用jooq生成的查询最多需要30秒才能完成,而另一个只需要几毫秒。 导致性能问题的原因是什么?资格?如何禁用它/加快查询速度?

1 个答案:

答案 0 :(得分:2)

您的jOOQ查询错误(假设您的纯SQL查询是正确的)。考虑一下:

return ctx.select(AT_TRAIL.POINT)
        .from(AT_TRAIL)
        .where(AT_TRAIL.ID.le(
                ctx.select(AT_TRAIL.ID) // This is the outer query's ID, not the local ID
                .from(...)
        ))
        .orderBy(AT_TRAIL.ID.asc())
        .fetch()

你打算写的是:

return ctx.select(AT_TRAIL.POINT)
        .from(AT_TRAIL)
        .where(AT_TRAIL.ID.le(
                ctx.select(field("id", AT_TRAIL.ID.getDataType())) // Better
                .from(...)
        ))
        .orderBy(AT_TRAIL.ID.asc())
        .fetch()

现在,您当然可以简化原始查询,以简化此操作。例如。这似乎做了同样的事情:

第1步:将distSELECT移至ORDER BY,删除一个嵌套查询:

select point 
from at_trail 
where id <= ( 
  select id
  from at_trail 
  order by point <-> ( 
    select point 
    from at_shelter  
    where id = ( 
      select at_shelter 
      from at_last_shelter 
    ) 
  ) asc  
  limit 1 
) 
order by id asc

第2步:转换回jOOQ

以上查询将如下:

ctx.select(AT_TRAIL.POINT)
   .from(AT_TRAIL)
   .where(AT_TRAIL.ID.le(
      select(AT_TRAIL.ID) // Now, no scoping problem anymore
     .from(AT_TRAIL)
     .orderBy(field("{0} <-> {1}", // jOOQ doesn't support this op, resorting to plain SQL
        AT_TRAIL.POINT,
        select(AT_SHELTER.POINT)
       .from(AT_SHELTER)
       .where(AT_SHELTER.ID.eq(
          select(AT_LAST_SHELTER.AT_SHELTER)
         .from(AT_LAST_SHELTER)
       ))
     ).asc())
     .limit(1)
   ))
   .orderBy(AT_TRAIL.ID.asc())
   .fetch();

取决于你正在做什么(我把它读作直到离最后一个避难所最近点的路径),这可能更加优化,但为了这个问题,我认为这是已经很不错了。