Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. Mobile and Embedded
  4. SQLIte database, How do I improve insert statement speed?
Forum Updated to NodeBB v4.3 + New Features

SQLIte database, How do I improve insert statement speed?

Scheduled Pinned Locked Moved Mobile and Embedded
9 Posts 5 Posters 15.1k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • H Offline
    H Offline
    hungchuviet
    wrote on last edited by
    #1

    I have 500000 records.
    How to insert them to sqlite databas with less time?
    Thanks you!

    1 Reply Last reply
    0
    • L Offline
      L Offline
      lyuts
      wrote on last edited by
      #2

      Is that going to be a single insert session? Or do you want it to handle this number of inserts per some period of time?

      I'm a rebel in the S.D.G.

      1 Reply Last reply
      0
      • F Offline
        F Offline
        Felix
        wrote on last edited by
        #3

        what do you mean with less time? can you provide your code that we can perhabs propose some improvments?

        But generally you should do it in one transaction, that saves time for commiting and writing the data to disk.

        1 Reply Last reply
        0
        • H Offline
          H Offline
          harryF
          wrote on last edited by
          #4

          Use prepared queries, see "Qt docs":http://doc.qt.nokia.com/qt-maemo-4.7/qsqlquery.html for details:

           QSqlQuery query;
           query.prepare("INSERT INTO person (id, forename, surname) "
                         "VALUES (?, ?, ?)");
           query.addBindValue(1001);
           query.addBindValue("Bart");
           query.addBindValue("Simpson");
           query.exec();
          

          // happy hacking

          1 Reply Last reply
          0
          • ? This user is from outside of this forum
            ? This user is from outside of this forum
            Guest
            wrote on last edited by
            #5

            hi harry, can you convert it to a link

            1 Reply Last reply
            0
            • H Offline
              H Offline
              hungchuviet
              wrote on last edited by
              #6

              Yes I do.
              I've put all insert statments in one transaction and use parameters query, but it is slow.
              @
              db.exec("BEGIN TRANSACTION;");
              qr.prepare("insert into test (f1,f2) values (:f1,:f2)");
              for (int i=0;i<500000;i++)
              {
              qr.bindValue(":f1",QString:number(i));
              qr.bindValue(":f2",QString:number(i+1));
              qr.exec();
              }
              db.exec("END TRANSACTION;");
              db.close();
              @

              1 Reply Last reply
              0
              • A Offline
                A Offline
                alexander
                wrote on last edited by
                #7

                hungchuviet
                you can try to change some PRAGMA statement: "http://www.sqlite.org/pragma.html":http://www.sqlite.org/pragma.html

                1 Reply Last reply
                0
                • H Offline
                  H Offline
                  harryF
                  wrote on last edited by
                  #8

                  The "BEGIN TRANSACTION" and "END TRANSACTION" blocks look strange - first, you don't check the return value, and second, you shouldn't have semicolons in SQL statements (you only need semicolons when using the sqlite shell tool).

                  Try using db.transaction() and db.commit() instead.

                  // happy hacking

                  1 Reply Last reply
                  0
                  • F Offline
                    F Offline
                    Felix
                    wrote on last edited by
                    #9

                    can you descripe what you mean with "less time"? how long does your code take?

                    1 Reply Last reply
                    0

                    • Login

                    • Login or register to search.
                    • First post
                      Last post
                    0
                    • Categories
                    • Recent
                    • Tags
                    • Popular
                    • Users
                    • Groups
                    • Search
                    • Get Qt Extensions
                    • Unsolved