summaryrefslogtreecommitdiffstats
path: root/src/sql/drivers
diff options
context:
space:
mode:
authorMilian Wolff <milian.wolff@kdab.com>2014-12-22 18:25:13 +0100
committerSérgio Martins <sergio.martins@kdab.com>2015-08-13 16:53:45 +0000
commit194403a3483b7317cc9511bc8b2ab307775643c5 (patch)
treecc223d55f395e24d8f74c9cf371ac02fe0b48256 /src/sql/drivers
parent807ec8ea48281d5dbe365895fd9679da5b6753a5 (diff)
Remove temporary string allocations when reading prepared statement.
Instead, we use the binary MySQL encoding and copy the data directly into the QVariant of the desired type. This gets rid of the temporary string allocations and greatly improves the performance of the added benchmark. On my machine, the results are: Before: 0.562 msecs per iteration (total: 563, iterations: 1000) 1,922,479.330 instructions per iteration (total: 1,922,479,330, iterations: 1000) After: 0.381 msecs per iteration (total: 381, iterations: 1000) 774,132.957 instructions per iteration (total: 774,132,958, iterations: 1000) Note that the same could be applied to floating point data types in the future. Additionally, special support for MYSQL_TIME structure coult be added to get rid of the string conversions there. To ensure everything keeps working, a new auto test is added as well that verifies the select statements and insertions of integral data into a MySql table works as intended. [ChangeLog][QtSql] Improve performance when reading integer values from MySQL databases via prepared statements. Change-Id: I21dd9277661971ded934546f09535014b63f8eb8 Reviewed-by: Olivier Goffart (Woboq GmbH) <ogoffart@woboq.com>
Diffstat (limited to 'src/sql/drivers')
-rw-r--r--src/sql/drivers/mysql/qsql_mysql.cpp34
1 files changed, 20 insertions, 14 deletions
diff --git a/src/sql/drivers/mysql/qsql_mysql.cpp b/src/sql/drivers/mysql/qsql_mysql.cpp
index 086a232746..ea8a918dcf 100644
--- a/src/sql/drivers/mysql/qsql_mysql.cpp
+++ b/src/sql/drivers/mysql/qsql_mysql.cpp
@@ -237,7 +237,11 @@ static QVariant::Type qDecodeMYSQLType(int mysqltype, uint flags)
QVariant::Type type;
switch (mysqltype) {
case FIELD_TYPE_TINY :
+ type = static_cast<QVariant::Type>((flags & UNSIGNED_FLAG) ? QMetaType::UChar : QMetaType::Char);
+ break;
case FIELD_TYPE_SHORT :
+ type = static_cast<QVariant::Type>((flags & UNSIGNED_FLAG) ? QMetaType::UShort : QMetaType::Short);
+ break;
case FIELD_TYPE_LONG :
case FIELD_TYPE_INT24 :
type = (flags & UNSIGNED_FLAG) ? QVariant::UInt : QVariant::Int;
@@ -316,14 +320,12 @@ static bool qIsBlob(int t)
static bool qIsInteger(int t)
{
- return t == MYSQL_TYPE_TINY
- || t == MYSQL_TYPE_SHORT
- || t == MYSQL_TYPE_LONG
- || t == MYSQL_TYPE_LONGLONG
- || t == MYSQL_TYPE_INT24;
+ return t == QMetaType::Char || t == QMetaType::UChar
+ || t == QMetaType::Short || t == QMetaType::UShort
+ || t == QMetaType::Int || t == QMetaType::UInt
+ || t == QMetaType::LongLong || t == QMetaType::ULongLong;
}
-
void QMYSQLResultPrivate::bindBlobs()
{
int i;
@@ -371,14 +373,9 @@ bool QMYSQLResultPrivate::bindInValues()
// after mysql_stmt_exec() in QMYSQLResult::exec()
fieldInfo->length = 0;
hasBlobs = true;
+ } else if (qIsInteger(f.type)) {
+ fieldInfo->length = 8;
} else {
- // fieldInfo->length specifies the display width, which may be too
- // small to hold valid integer values (see
- // http://dev.mysql.com/doc/refman/5.0/en/numeric-types.html ), so
- // always use the MAX_BIGINT_WIDTH for integer types
- if (qIsInteger(fieldInfo->type)) {
- fieldInfo->length = MAX_BIGINT_WIDTH;
- }
fieldInfo->type = MYSQL_TYPE_STRING;
}
bind = &inBinds[i];
@@ -598,6 +595,9 @@ QVariant QMYSQLResult::data(int field)
if (f.nullIndicator)
return QVariant(f.type);
+ if (qIsInteger(f.type))
+ return QVariant(f.type, f.outField);
+
if (f.type != QVariant::ByteArray)
val = toUnicode(d->driver->d_func()->tc, f.outField, f.bufLength);
} else {
@@ -605,18 +605,24 @@ QVariant QMYSQLResult::data(int field)
// NULL value
return QVariant(f.type);
}
+
fieldLength = mysql_fetch_lengths(d->result)[field];
+
if (f.type != QVariant::ByteArray)
val = toUnicode(d->driver->d_func()->tc, d->row[field], fieldLength);
}
- switch(f.type) {
+ switch (static_cast<int>(f.type)) {
case QVariant::LongLong:
return QVariant(val.toLongLong());
case QVariant::ULongLong:
return QVariant(val.toULongLong());
+ case QMetaType::Char:
+ case QMetaType::Short:
case QVariant::Int:
return QVariant(val.toInt());
+ case QMetaType::UChar:
+ case QMetaType::UShort:
case QVariant::UInt:
return QVariant(val.toUInt());
case QVariant::Double: {