#include <QCoreApplication>
#include <QSqlDatabase>
#include <QSqlQuery>
#include <QDebug>
#include <QTime>
int main(int argc, char *argv[]) {
command = "Driver={Microsoft Access Driver (*.mdb, *.accdb)};";
command += "FIL={MS Access};DBQ=C:/database.accdb";
sql.setDatabaseName(command);
qDebug() << "open" << sql.open();
// Slow query
QSqlQuery query
("SELECT name + 'x' FROM users", sql
);
time.start();
while(query.next()) query.value(0);
qDebug
() <<
"query1 (slow)" <<
QString::number(time.
elapsed()) + "ms";
// Others slow queries:
// SELECT 'x' FROM users
// SELECT '' FROM users
// SELECT CStr('x') FROM users
// Normal query
QSqlQuery query2
("SELECT name FROM users", sql
);
time.start();
while(query2.next()) query2.value(0);
qDebug
() <<
"query2" <<
QString::number(time.
elapsed()) + "ms";
return a.exec();
}
#include <QCoreApplication>
#include <QSqlDatabase>
#include <QSqlQuery>
#include <QDebug>
#include <QTime>
int main(int argc, char *argv[]) {
QCoreApplication a(argc, argv);
QTime time;
QString command;
command = "Driver={Microsoft Access Driver (*.mdb, *.accdb)};";
command += "FIL={MS Access};DBQ=C:/database.accdb";
QSqlDatabase sql = QSqlDatabase::addDatabase("QODBC3");
sql.setDatabaseName(command);
qDebug() << "open" << sql.open();
// Slow query
QSqlQuery query("SELECT name + 'x' FROM users", sql);
time.start();
while(query.next()) query.value(0);
qDebug() << "query1 (slow)" << QString::number(time.elapsed()) + "ms";
// Others slow queries:
// SELECT 'x' FROM users
// SELECT '' FROM users
// SELECT CStr('x') FROM users
// Normal query
QSqlQuery query2("SELECT name FROM users", sql);
time.start();
while(query2.next()) query2.value(0);
qDebug() << "query2" << QString::number(time.elapsed()) + "ms";
return a.exec();
}
To copy to clipboard, switch view to plain text mode
open true
query1 (slow) "408ms"
query2 "6ms"
open true
query1 (slow) "408ms"
query2 "6ms"
To copy to clipboard, switch view to plain text mode
The problem happen when I try select a virtual field (SELECT 'str') or a field concatenated with literal string (SELECT field + 'str').
The database is only an example with one table (users), two fields (id, name) and 1000 records.
Greetings and thanks.
Bookmarks