gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-29 12:57:03 25 4
gpt4 key购买 nike

我有一个 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 秒才能完成,而另一个只需要几毫秒。是什么导致了性能问题?资格?以及如何禁用它/加快查询速度?

最佳答案

您的 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();

取决于您在做什么(我将其理解为寻找到最后一个避难所最近点的路径),这可能更可优化,但为了这个问题,我认为这已经很不错了好。

关于java - jooq 生成的针对 postgres 的查询的性能问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40350721/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com