I have performance problems with an jooq generated query that sould be equal to an normal string query. The jooq query looks like this:
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);
});
And my plain string query looks like this
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);
});
I compared the jooq generated query, with the other one. They differ in the table alias. jooq generates an as "alias_108340908", while I only use t. And jooq fully quallifies column names and tables like "public"."at_trail"."point". Otherwise the two queries are identical. However the query generated with jooq needs up to 30 seconds to complete, while the other needs only some milli seconds.
What is causing the performance issue? The qualification? And how do I disable it/speed up the query?
Select.asTable("t")for instace. The full qualification (schema) can be avoided by specifyingSetting.renderSchemafor instance. Will it help? Also, what are the generated execution plans (runEXPLAIN SELECT ...in PostgreSQL)ctx.select(field("id", Integer.class))and it worked. Apparently the qualification is wrong at this point, because it is the id of the temporary table. (I guess)IDcolumn from the outer table instead of referencing the localt.ID. This happened to me recently in an ordinary SQL statement. Do you want to answer your own question or shall I write up an answer?