jooq 生成的针对 postgres 的查询的性能问题
Performance issue with jooq generated query against postgres
我在使用 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 生成的查询与另一个进行了比较。它们的区别在于 table 别名。 jooq 生成一个 as "alias_108340908"
,而我只使用 t
。并且 jooq 完全限定列名和 tables,如 "public"."at_trail"."point"
。否则这两个查询是相同的。然而,使用 jooq 生成的查询最多需要 30 秒才能完成,而另一个只需要几毫秒。
是什么导致了性能问题?资格?以及如何禁用 it/speed 向上查询?
您的 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 步:通过将 dist
从 SELECT
移动到 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();
取决于你在做什么(我读到这个是寻找到最后一个避难所最近点的路径),这可能更可优化,但为了这个问题,我认为这是已经很不错了
我在使用 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 生成的查询与另一个进行了比较。它们的区别在于 table 别名。 jooq 生成一个 as "alias_108340908"
,而我只使用 t
。并且 jooq 完全限定列名和 tables,如 "public"."at_trail"."point"
。否则这两个查询是相同的。然而,使用 jooq 生成的查询最多需要 30 秒才能完成,而另一个只需要几毫秒。
是什么导致了性能问题?资格?以及如何禁用 it/speed 向上查询?
您的 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 步:通过将 dist
从 SELECT
移动到 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();
取决于你在做什么(我读到这个是寻找到最后一个避难所最近点的路径),这可能更可优化,但为了这个问题,我认为这是已经很不错了