summaryrefslogtreecommitdiffstats
path: root/src/plugins/sqldrivers
diff options
context:
space:
mode:
authorAndy Shaw <andy.shaw@qt.io>2018-01-08 15:48:01 +0100
committerJani Heikkinen <jani.heikkinen@qt.io>2018-02-08 18:24:48 +0000
commit6c6ace9d23f90845fd424e474d38fe30f070775e (patch)
treedef1c304f95c6ea902448fe66d09f15da5f1e7de /src/plugins/sqldrivers
parent41bcb31ab9538328ca05efcb4a01569c9803198c (diff)
psql: Improve performance of record()v5.10.1
In order to save having to always run a query to get the tablename for a known oid then we cache the result on the driver side. The oid stays the same while the table exists, so only on dropping it would it change. Recreating the table causes it to get a new oid, so there is no risk of the old one being associated with the wrong table when this happens, if the driver is still open at that point. The benchmark added shows the improvement from the previous code, before the results for PostgreSQL was: RESULT : tst_QSqlRecord::benchmarkRecord():"0_QPSQL@localhost": 259 msecs per iteration (total: 259, iterations: 1) whereas now it is: RESULT : tst_QSqlRecord::benchmarkRecord():"0_QPSQL@localhost": 0.000014 msecs per iteration (total: 59, iterations: 4194304) Task-number: QTBUG-65226 Change-Id: Ic290cff719102743da84e2044cd23e540f20c96c Reviewed-by: Christian Ehrlicher <ch.ehrlicher@gmx.de> Reviewed-by: Robert Szefner <robertsz27@interia.pl> Reviewed-by: Edward Welbourne <edward.welbourne@qt.io>
Diffstat (limited to 'src/plugins/sqldrivers')
-rw-r--r--src/plugins/sqldrivers/psql/qsql_psql.cpp14
1 files changed, 10 insertions, 4 deletions
diff --git a/src/plugins/sqldrivers/psql/qsql_psql.cpp b/src/plugins/sqldrivers/psql/qsql_psql.cpp
index 35b0f9a3e3..c381572713 100644
--- a/src/plugins/sqldrivers/psql/qsql_psql.cpp
+++ b/src/plugins/sqldrivers/psql/qsql_psql.cpp
@@ -183,6 +183,7 @@ public:
void setDatestyle();
void setByteaOutput();
void detectBackslashEscape();
+ mutable QHash<int, QString> oidToTable;
};
void QPSQLDriverPrivate::appendTables(QStringList &tl, QSqlQuery &t, QChar type)
@@ -553,11 +554,16 @@ QSqlRecord QPSQLResult::record() const
f.setName(QString::fromUtf8(PQfname(d->result, i)));
else
f.setName(QString::fromLocal8Bit(PQfname(d->result, i)));
- QSqlQuery qry(driver()->createResult());
- if (qry.exec(QStringLiteral("SELECT relname FROM pg_class WHERE pg_class.oid = %1")
- .arg(PQftable(d->result, i))) && qry.next()) {
- f.setTableName(qry.value(0).toString());
+ const int tableOid = PQftable(d->result, i);
+ auto &tableName = d->drv_d_func()->oidToTable[tableOid];
+ if (tableName.isEmpty()) {
+ QSqlQuery qry(driver()->createResult());
+ if (qry.exec(QStringLiteral("SELECT relname FROM pg_class WHERE pg_class.oid = %1")
+ .arg(tableOid)) && qry.next()) {
+ tableName = qry.value(0).toString();
+ }
}
+ f.setTableName(tableName);
int ptype = PQftype(d->result, i);
f.setType(qDecodePSQLType(ptype));
int len = PQfsize(d->result, i);