Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. How long does the write operation of the database take
QtWS25 Last Chance

How long does the write operation of the database take

Scheduled Pinned Locked Moved Solved General and Desktop
22 Posts 5 Posters 3.0k Views
  • 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.
  • T Offline
    T Offline
    tovax
    wrote on 22 Mar 2021, 07:31 last edited by tovax
    #1

    OS: Win10 64bit
    CPU: Intel Core i5-7400 3.00GHz
    Memory: 8GB
    Database driver: QSQLITE
    The operation of writing to the database took about 80 milliseconds. Does it normally take such a long time?
    I have tested two writing methods and the results are the same:

    1. QSqlTableModel::setData
    2. QSqlQuery::exec("UPDATE test_table SET ...")

    Best regards!

    J 1 Reply Last reply 22 Mar 2021, 07:34
    0
    • T tovax
      22 Mar 2021, 07:31

      OS: Win10 64bit
      CPU: Intel Core i5-7400 3.00GHz
      Memory: 8GB
      Database driver: QSQLITE
      The operation of writing to the database took about 80 milliseconds. Does it normally take such a long time?
      I have tested two writing methods and the results are the same:

      1. QSqlTableModel::setData
      2. QSqlQuery::exec("UPDATE test_table SET ...")

      Best regards!

      J Offline
      J Offline
      jsulm
      Lifetime Qt Champion
      wrote on 22 Mar 2021, 07:34 last edited by
      #2

      @tovax This really depends on many factors, like: how big was the data you wrote, where is the database file located (HDD, SSD, RAM)...

      https://forum.qt.io/topic/113070/qt-code-of-conduct

      T 1 Reply Last reply 22 Mar 2021, 07:48
      1
      • J jsulm
        22 Mar 2021, 07:34

        @tovax This really depends on many factors, like: how big was the data you wrote, where is the database file located (HDD, SSD, RAM)...

        T Offline
        T Offline
        tovax
        wrote on 22 Mar 2021, 07:48 last edited by
        #3

        @jsulm Hi, thanks!
        The database is located on the HDD, and just write a double type of data. Is this normal speed?

        J 1 Reply Last reply 22 Mar 2021, 07:52
        0
        • T tovax
          22 Mar 2021, 07:48

          @jsulm Hi, thanks!
          The database is located on the HDD, and just write a double type of data. Is this normal speed?

          J Offline
          J Offline
          jsulm
          Lifetime Qt Champion
          wrote on 22 Mar 2021, 07:52 last edited by
          #4

          @tovax Never measured SQLite, but would guess writing itself should be faster than 80ms. How exactly do you measure (and what).

          https://forum.qt.io/topic/113070/qt-code-of-conduct

          T 1 Reply Last reply 22 Mar 2021, 08:00
          1
          • J jsulm
            22 Mar 2021, 07:52

            @tovax Never measured SQLite, but would guess writing itself should be faster than 80ms. How exactly do you measure (and what).

            T Offline
            T Offline
            tovax
            wrote on 22 Mar 2021, 08:00 last edited by
            #5

            @jsulm

            QElapsedTimer timer;
            timer.start();
            model->setData(index, value, role);
            qDebug() << __FUNCTION__ << timer.elapsed();
            

            I have no experience with the read and write speed of the database. I don't know if "80ms" is a normal level.

            J 1 Reply Last reply 22 Mar 2021, 08:07
            0
            • T tovax
              22 Mar 2021, 08:00

              @jsulm

              QElapsedTimer timer;
              timer.start();
              model->setData(index, value, role);
              qDebug() << __FUNCTION__ << timer.elapsed();
              

              I have no experience with the read and write speed of the database. I don't know if "80ms" is a normal level.

              J Offline
              J Offline
              J.Hilk
              Moderators
              wrote on 22 Mar 2021, 08:07 last edited by
              #6

              @tovax store the elapsed time in a variable beforehand, that qDebug() call may take forever


              Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


              Q: What's that?
              A: It's blue light.
              Q: What does it do?
              A: It turns blue.

              T 1 Reply Last reply 22 Mar 2021, 08:19
              3
              • J J.Hilk
                22 Mar 2021, 08:07

                @tovax store the elapsed time in a variable beforehand, that qDebug() call may take forever

                T Offline
                T Offline
                tovax
                wrote on 22 Mar 2021, 08:19 last edited by
                #7

                @J-Hilk Hi, thanks!

                QElapsedTimer timer;
                timer.start();
                model->setData(index, value, role);
                int64_t elapsedBuffer = timer.elapsed();
                qDebug() << __FUNCTION__ << elapsedBuffer;
                
                J 1 Reply Last reply 22 Mar 2021, 08:23
                0
                • T tovax
                  22 Mar 2021, 08:19

                  @J-Hilk Hi, thanks!

                  QElapsedTimer timer;
                  timer.start();
                  model->setData(index, value, role);
                  int64_t elapsedBuffer = timer.elapsed();
                  qDebug() << __FUNCTION__ << elapsedBuffer;
                  
                  J Offline
                  J Offline
                  JonB
                  wrote on 22 Mar 2021, 08:23 last edited by JonB
                  #8

                  @tovax
                  I assume by the way that you have timed this many times in several different places, and taken averages? For example, timing one operation which is, say, the first write to a database/file can be very far from "typical" of your overall throughput rate.

                  Depending on what timings/operations you want, you may, or may not be interested, in timing multiple writes, and/or writes separated by time, and so on.

                  If you really want to know what's going, if profiling is available to you fro Creator you could try using that to instrument behaviour.

                  1 Reply Last reply
                  3
                  • T Offline
                    T Offline
                    tovax
                    wrote on 22 Mar 2021, 08:46 last edited by
                    #9

                    Source code:

                    void CentralMachiningInfo::setValue(const MachiningInfoRow::T row, const QVariant value, int32_t role)
                    {
                        QWriteLocker locker(&modelLock);
                        QModelIndex index = model->index(row, MachiningInfoColumn::Value);
                        if (model->data(index, role) != value) {
                            QElapsedTimer timer;
                            timer.start();
                            model->setData(index, value, role);
                            int64_t elapsedBuffer = timer.elapsed();
                            qDebug() << __func__ << elapsedBuffer;
                        }
                    }
                    

                    Debug output:

                    setValue 92
                    setValue 67
                    setValue 77
                    setValue 72
                    setValue 82
                    setValue 69
                    setValue 74
                    setValue 76
                    setValue 71
                    setValue 72
                    setValue 69
                    setValue 71
                    setValue 82
                    setValue 69
                    setValue 71
                    setValue 72
                    setValue 76
                    setValue 69
                    setValue 72
                    setValue 82
                    setValue 76
                    setValue 71
                    setValue 81
                    setValue 84
                    setValue 91
                    setValue 84
                    
                    J 1 Reply Last reply 22 Mar 2021, 08:49
                    0
                    • T tovax
                      22 Mar 2021, 08:46

                      Source code:

                      void CentralMachiningInfo::setValue(const MachiningInfoRow::T row, const QVariant value, int32_t role)
                      {
                          QWriteLocker locker(&modelLock);
                          QModelIndex index = model->index(row, MachiningInfoColumn::Value);
                          if (model->data(index, role) != value) {
                              QElapsedTimer timer;
                              timer.start();
                              model->setData(index, value, role);
                              int64_t elapsedBuffer = timer.elapsed();
                              qDebug() << __func__ << elapsedBuffer;
                          }
                      }
                      

                      Debug output:

                      setValue 92
                      setValue 67
                      setValue 77
                      setValue 72
                      setValue 82
                      setValue 69
                      setValue 74
                      setValue 76
                      setValue 71
                      setValue 72
                      setValue 69
                      setValue 71
                      setValue 82
                      setValue 69
                      setValue 71
                      setValue 72
                      setValue 76
                      setValue 69
                      setValue 72
                      setValue 82
                      setValue 76
                      setValue 71
                      setValue 81
                      setValue 84
                      setValue 91
                      setValue 84
                      
                      J Offline
                      J Offline
                      JonB
                      wrote on 22 Mar 2021, 08:49 last edited by JonB
                      #10

                      @tovax
                      Don't forget: setData() may be doing more than just writing a value to the database. For example, it raises a dataChanged() signal, and if you have a view slotted to that (and using the default DirectConnection) you will be timing the whole of the view's response code here.... At minimum you might disable Qt signals for the duration. That's why I said profiling would be the best way to see what is really going on.

                      T 1 Reply Last reply 22 Mar 2021, 09:47
                      4
                      • Christian EhrlicherC Offline
                        Christian EhrlicherC Offline
                        Christian Ehrlicher
                        Lifetime Qt Champion
                        wrote on 22 Mar 2021, 08:52 last edited by
                        #11

                        @tovax said in How long does the write operation of the database take:

                        QWriteLocker locker(&modelLock);

                        This looks fishy...

                        Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
                        Visit the Qt Academy at https://academy.qt.io/catalog

                        J 1 Reply Last reply 22 Mar 2021, 08:53
                        4
                        • Christian EhrlicherC Christian Ehrlicher
                          22 Mar 2021, 08:52

                          @tovax said in How long does the write operation of the database take:

                          QWriteLocker locker(&modelLock);

                          This looks fishy...

                          J Offline
                          J Offline
                          JonB
                          wrote on 22 Mar 2021, 08:53 last edited by
                          #12

                          @Christian-Ehrlicher
                          Oh yes I didn't notice that! Could be making a huge difference!

                          @tovax
                          Comment that out for a start and see whether it is making any difference?

                          T 1 Reply Last reply 22 Mar 2021, 09:11
                          1
                          • J JonB
                            22 Mar 2021, 08:53

                            @Christian-Ehrlicher
                            Oh yes I didn't notice that! Could be making a huge difference!

                            @tovax
                            Comment that out for a start and see whether it is making any difference?

                            T Offline
                            T Offline
                            tovax
                            wrote on 22 Mar 2021, 09:11 last edited by
                            #13

                            @JonB
                            There is no difference after commenting out "QWriteLocker".

                            1 Reply Last reply
                            0
                            • Christian EhrlicherC Offline
                              Christian EhrlicherC Offline
                              Christian Ehrlicher
                              Lifetime Qt Champion
                              wrote on 22 Mar 2021, 09:12 last edited by
                              #14

                              @tovax said in How long does the write operation of the database take:

                              There is no difference after commenting out "QWriteLocker".

                              I don't see why you need it at all - that's why I pointed it out.

                              Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
                              Visit the Qt Academy at https://academy.qt.io/catalog

                              T 1 Reply Last reply 22 Mar 2021, 09:16
                              1
                              • Christian EhrlicherC Christian Ehrlicher
                                22 Mar 2021, 09:12

                                @tovax said in How long does the write operation of the database take:

                                There is no difference after commenting out "QWriteLocker".

                                I don't see why you need it at all - that's why I pointed it out.

                                T Offline
                                T Offline
                                tovax
                                wrote on 22 Mar 2021, 09:16 last edited by
                                #15

                                @Christian-Ehrlicher Hi, thank you very much, this class was a singleton class in the previous version, and the current version does not require a read-write lock.

                                1 Reply Last reply
                                0
                                • Christian EhrlicherC Offline
                                  Christian EhrlicherC Offline
                                  Christian Ehrlicher
                                  Lifetime Qt Champion
                                  wrote on 22 Mar 2021, 09:17 last edited by
                                  #16

                                  So your previous implementation was wrong - you must not access the gui from without the main thread :)

                                  Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
                                  Visit the Qt Academy at https://academy.qt.io/catalog

                                  T 1 Reply Last reply 22 Mar 2021, 09:30
                                  2
                                  • Christian EhrlicherC Christian Ehrlicher
                                    22 Mar 2021, 09:17

                                    So your previous implementation was wrong - you must not access the gui from without the main thread :)

                                    T Offline
                                    T Offline
                                    tovax
                                    wrote on 22 Mar 2021, 09:30 last edited by
                                    #17

                                    @Christian-Ehrlicher Thank you very much for your reply :)

                                    1 Reply Last reply
                                    0
                                    • J JonB
                                      22 Mar 2021, 08:49

                                      @tovax
                                      Don't forget: setData() may be doing more than just writing a value to the database. For example, it raises a dataChanged() signal, and if you have a view slotted to that (and using the default DirectConnection) you will be timing the whole of the view's response code here.... At minimum you might disable Qt signals for the duration. That's why I said profiling would be the best way to see what is really going on.

                                      T Offline
                                      T Offline
                                      tovax
                                      wrote on 22 Mar 2021, 09:47 last edited by
                                      #18

                                      @JonB I have checked the signals and slots of the database and there is no connection during the reading and writing test.

                                      J J 2 Replies Last reply 22 Mar 2021, 10:12
                                      0
                                      • T tovax
                                        22 Mar 2021, 09:47

                                        @JonB I have checked the signals and slots of the database and there is no connection during the reading and writing test.

                                        J Offline
                                        J Offline
                                        jsulm
                                        Lifetime Qt Champion
                                        wrote on 22 Mar 2021, 10:12 last edited by
                                        #19

                                        @tovax To find out whether the write itself is slow you should omit the model and write directly using QSqlQuery.

                                        https://forum.qt.io/topic/113070/qt-code-of-conduct

                                        1 Reply Last reply
                                        3
                                        • T tovax
                                          22 Mar 2021, 09:47

                                          @JonB I have checked the signals and slots of the database and there is no connection during the reading and writing test.

                                          J Offline
                                          J Offline
                                          JonB
                                          wrote on 22 Mar 2021, 10:16 last edited by
                                          #20

                                          @tovax
                                          Well I'm not sure what you want us to say here? At your timings you can achieve ~15 write operations per second. I would not be very happy with this!

                                          Try:

                                                  timer.start();
                                                  for (int = 0; i < 10; i++)
                                                  {
                                                      model->setData(index, value, role);
                                                      // change `index` and/or `value`
                                                  }
                                                  int64_t elapsedBuffer = timer.elapsed();
                                          

                                          Does this take 10x as long??

                                          I have checked the signals and slots of the database and there is no connection during the reading and writing test.

                                          So you have no views at all attached to the model?

                                          T 1 Reply Last reply 22 Mar 2021, 10:34
                                          1

                                          1/22

                                          22 Mar 2021, 07:31

                                          • Login

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