Results 1 to 10 of 10

Thread: qsqlite waaay to slow

  1. #1
    Join Date
    Dec 2006
    Posts
    23
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default qsqlite waaay to slow

    I am writing a program and I'm reading in data from an xml string and loading that data into sqlite tables. I'm taking a HUGE performance hit when I do this. for example, to load about 1000 records into a table where each record has four fields takes well over 4-5 minutes.

    Here is the code where i create the table:
    bool DBManager::createControlsTable()
    {
    Logger* pLog = Logger::getInstance();

    QSqlQuery query(db);

    if(!query.exec("create table Controls( \
    ToolName varchar(20) \
    , ToolID varchar(20) \
    , DoD8500 varchar(50) \
    , NIST varchar(1024))"))
    {
    pLog->ERR("DBManager::createControls - Could not create DyneticsIAControls Table");
    QSqlError err = query.lastError();
    QString estr = err.text();
    pLog->ERR("DBManager::createControls() - DB error", estr);
    errorsFound = true;
    return false;
    }

    return true;
    }


    This is the code that I write the insertion query for the table:
    bool DBManager::storeDataToIAControlsTable(QString tName, QString tID, QString DoD, QString Nist)
    {
    Logger* pLog = Logger::getInstance();

    QSqlQuery query(db);

    query.prepare("INSERT INTO Controls(ToolName, ToolID, DoD8500, NIST)" "VALUES(:ToolName, :ToolID, oD8500, :NIST)");
    query.bindValue(":ToolName", tName);
    query.bindValue(":ToolID", tID);
    query.bindValue("oD8500", DoD);
    query.bindValue(":NIST", Nist);

    if(!query.exec())
    {
    pLog->ERR("DBManager::storeDataToIAControlsTable - Could not populate IAControls Table");
    QSqlError err = query.lastError();
    QString estr = err.text();
    pLog->ERR("DBManager::storeDataToIAControlsTable - DB error", estr);
    errorsFound = true;
    return false;
    }

    return true;
    }

    And here is where I call this function...
    First I open the xml document, read it in, etc. Here's the meat of the code...

    QDomNode n = root.firstChild();

    QString tName;
    QString tID;
    QString DoD;
    QString Nist;

    while(!n.isNull())
    {
    QDomElement e = n.toElement();
    if(!e.isNull())
    {
    QDomNode a = n.namedItem("ToolName");
    QDomElement ae = a.toElement();
    tName = ae.text();

    a = n.namedItem("ToolID");
    ae = a.toElement();
    tID = ae.text();

    a = n.namedItem("DoDSecurityControl");
    ae = a.toElement();
    DoD = ae.text();

    a = n.namedItem("FederalSecurityControl");
    ae = a.toElement();
    Nist = ae.text();

    }

    if(!storeDataToIAControlsTable(tName, tID, DoD, Nist))
    {
    pLog->ERR("DBManager::readInIAControlData - Failed to populate IAControls Table");
    errorsFound = true;
    return false;
    }
    n = n.nextSibling();
    }

    return true;

    I hope this have given you enough to understand the kinds of things I'm doing. I really need to figure out how to speed things up. I appreciate any help you can give me in advance.

    Thanks

  2. #2
    Join Date
    Dec 2006
    Posts
    849
    Thanks
    6
    Thanked 163 Times in 151 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: qsqlite waaay to slow

    search the forums, there were posts on that before iirc.
    The solution was to wrap all this inside a transaction, I think.

  3. #3
    Join Date
    Dec 2006
    Posts
    23
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: qsqlite waaay to slow

    I've been looking through the posts and I can't find anything relating to my problem. I'm also not sure how to use transactions, (especially when using bindValue()). Any help would be appreciated. Thanks.

  4. #4
    Join Date
    Dec 2006
    Posts
    849
    Thanks
    6
    Thanked 163 Times in 151 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: qsqlite waaay to slow

    e.g.
    http://www.qtcentre.org/forum/f-gene...lite-4180.html

    in short:
    wrap your insert calls into a transaction and see if that brings an improvement.

    HTH

  5. #5
    Join Date
    Oct 2006
    Posts
    42
    Thanks
    1
    Thanked 8 Times in 8 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: qsqlite waaay to slow

    If I read your code correct ... For every row you insert you create a QSqlQuery object and prepare a query. That's not very efficient.

    You should create only one QSqlQuery, prepare the query only once, and reuse it. Also, wrap everything up in a single transaction. That should speed up things considerably.

  6. #6
    Join Date
    Aug 2009
    Posts
    2
    Qt products
    Qt4

    Red face transactions ?

    Hi !

    I have something like:

    Qt Code:
    1. QSqlQuery query;
    2.  
    3. for (int i=0 ; i <= 10000 ; i++)
    4. {
    5.  
    6. query.prepare("INSERT INTO table ( id, name )
    7. VALUES ( :id, :name )" );
    8. query.bindValue(":id", QStringList_row[i] );
    9. query.bindValue(":name", QStringList_row[i] );
    10.  
    11. query.exec();
    12.  
    13. }
    To copy to clipboard, switch view to plain text mode 


    inserting into a mysql db.
    Could anyone show me how to use transactions in this case ? (if possible)

    Thank you !

  7. #7
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: qsqlite waaay to slow

    Untested, but something like this:
    Qt Code:
    1. QSqlQuery query;
    2.  
    3. // Prepare only once
    4. query.prepare("INSERT INTO table ( id, name )
    5. VALUES ( :id, :name )" );
    6.  
    7. // Start a transaction on the database
    8. query.driver()->beginTransaction();
    9.  
    10. // Do the bulk insert
    11. for (int i=0 ; i <= 10000 ; i++)
    12. {
    13. query.bindValue(":id", QStringList_row[i] );
    14. query.bindValue(":name", QStringList_row[i] );
    15. query.exec();
    16. }
    17.  
    18. // Commit if all is well
    19. query.driver()->commitTransaction();
    20. // or roll back if not
    21. query.driver()->rollbackTransaction();
    To copy to clipboard, switch view to plain text mode 

  8. #8
    Join Date
    Aug 2009
    Posts
    2
    Qt products
    Qt4

    Default Re: qsqlite waaay to slow

    Could you test that, I am getting an error..
    passing `const QSqlDriver' as `this' argument of `virtual bool QSqlDriver::beginTransaction()' discards qualifiers

  9. #9
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: qsqlite waaay to slow

    I'm still not somewhere I can run a Qt compile. Try:
    Qt Code:
    1. QSqlDatabase::database().transaction();
    To copy to clipboard, switch view to plain text mode 
    in place of line 8 and similar changes for 19 & 21 . You may also need to move the transaction() call before the prepare. Have a look under "Transactions" in the docs for QtSql.

  10. The following user says thank you to ChrisW67 for this useful post:

    vladozar (6th October 2009)

  11. #10
    Join Date
    Apr 2009
    Posts
    17
    Thanks
    7
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Smile Re: qsqlite waaay to slow

    I have ran into a similar problem where I had to insert thousands of records at a time and with this post I have found a solution.

    Below is a general idea of what I have done based on your code

    Qt Code:
    1. QSqlQuery query;
    2.  
    3. // Start a transaction on the database
    4. QSqlDatabase::database().transaction();
    5. // Prepare only once
    6. query.prepare("INSERT INTO table ( id, name )
    7. VALUES ( :id, :name )" );
    8.  
    9. // Do the bulk insert
    10. for (int i=0 ; i <= 10000 ; i++)
    11. {
    12. query.bindValue(":id", QStringList_row[i]);
    13. query.bindValue(":name", QStringList_row[i]);
    14. query.exec();
    15. }
    16.  
    17. // Commit if all is well
    18. QSqlDatabase::database().commit();
    To copy to clipboard, switch view to plain text mode 

    Thanks for the post

Similar Threads

  1. Why is drawText so terribly slow?
    By Ntoskrnl in forum Qt Programming
    Replies: 8
    Last Post: 1st August 2008, 19:15
  2. QTextEdit slow to insert text
    By thomaspu in forum Qt Programming
    Replies: 4
    Last Post: 10th January 2008, 12:05

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Qt is a trademark of The Qt Company.