1

I need to copy two columns from an QSqlTableModel and put into a QVector.

I'm trying this:

ParameterData pd;
QSqlTableModel *m = mapTbModels.value(table);
QList<QSqlField> parameterList = getFields(table);

for (int j = 0; j <parameterList.size(); j++) {
    QSqlField f = parameterList[j];
    QVector<QPointF> v;
    if (f.type() != QVariant::Int)
        continue;
    else {
        pd.name = f.name();
        timer.start();
        for (int i = 0; i < m->rowCount(); i++)
            v << value(m, i, f.name());
        qDebug() << "Database" << timer.elapsed();
    }
    pd.data = v;
    pd.table = table;
    emit data(pd);
    emit status(QString::number(j*100/parameterList.size()));
    QCoreApplication::processEvents();
}

What's the fastest way?

nsejxT nsejxT
  • 87
  • 1
  • 4

1 Answers1

0

I once made some performance tests with Qt 5.2 and postgres to check the qsql features. It turned out, that using QSqlTableModel is around 1000 times slower than using own SQL queries like

    QSqlQuery query("",db);
    query.prepare("SELECT a.fueltype_longname, coalesce(a.bunkerlog_amount,0) FROM (\
                SELECT f.fueltype_longname, b.bunkerlog_amount, rank() OVER (PARTITION BY b.bunkerlog_fueltype ORDER BY b.bunkerlog_id DESC) FROM data.t_bunkerlog2 b\
                RIGHT JOIN data.t_fueltype f ON b.bunkerlog_fueltype = f.fueltype_longname  \
      ) a WHERE a.rank=1");
     query.exec();

So for a data intensive app use query/prepare/exec. If you just want so show simple tables to the user, use QSqlTableModel etc. for convenience.

transistor
  • 649
  • 6
  • 11