pgjdbc / r2dbc-postgresql

Postgresql R2DBC Driver

Home Page:https://r2dbc.io

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Performance issue with PostgresqlRow.getColumn(String name) when select many columns

yuki-teraoka opened this issue · comments

When selecting many fields, a lot of CPU is consumed because the fields are looped.

In one case I encountered, with a select of millions of records with about 700 fields, I forced the query to stop because it was consuming 100% CPU for several hours.

At this time, I checked Java's stack trace many times, and most of them were processing the following for statement.

for (int i = 0; i < this.fields.size(); i++) {
RowDescription.Field field = this.fields.get(i);
if (field.getName().equalsIgnoreCase(name)) {
return i;
}
}

Would it be possible to prepare field name and order mappings in advance?

This is possible. PGJDBC uses a lazy approach to create the column name to index map, see https://github.com/pgjdbc/pgjdbc/blob/master/pgjdbc/src/main/java/org/postgresql/jdbc/PgResultSet.java#L3108-L3136

Happy to merge a pull request.