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. Extremely slow QSqlQuery SELECT on a small database
QtWS25 Last Chance

Extremely slow QSqlQuery SELECT on a small database

Scheduled Pinned Locked Moved General and Desktop
4 Posts 3 Posters 3.5k 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.
  • M Offline
    M Offline
    Mike347
    wrote on last edited by
    #1

    Hi!

    I'm completely new to C++ and Qt but please don't hold that against me :)

    I'm building a database front end in Qt as part of a university research project I'm involved in. The database was created in MS Access 2013 initially, converted to SQL Azure using the SSMA application and then hosted online. The access database is adequate for some of our needs but we also really need a custom front end that will operate on multiple OS's.

    Now - to the problem.

    I've got a table on my Azure SQL server which contains 1015 rows with 11 columns, most of them with no data inside. When I run a QSqlQuery to iterate through the table and store the values so they can be added to the GUI it takes upwards of 55 seconds (55610 ms to be exact).

    The code is here:

    @ QSqlQuery shipQuery(db);
    shipQuery.prepare("SELECT ShipID, ShipName, ShipFlag, ShipRig, ShipTonnage, ShipStandardisedTonnage, ShipYearConstructed, ShipPlaceRegistered, ShipYearRegistered, ShipFrom, ShipTo FROM mydatabase.tblShips");
    shipQuery.setForwardOnly(true);
    shipQuery.exec();
    while (shipQuery.next()) {
    int shId = shipQuery.value(0).toInt();
    ShipData* shData = new ShipData(shId);
    shData->ShipName = shipQuery.value(1).toString();
    shData->ShipFlag = shipQuery.value(2).toString();
    shData->ShipRig = shipQuery.value(3).toString();
    shData->ShipTonnage = shipQuery.value(4).toString();
    shData->ShipStandardTonnage = shipQuery.value(5).toString();
    shData->ShipYearConstructed = shipQuery.value(6).toString();
    shData->ShipPlaceRegistered = shipQuery.value(7).toString();
    shData->ShipYearRegistered = shipQuery.value(8).toString();
    shData->ShipFrom = shipQuery.value(9).toString();
    shData->ShipTo = shipQuery.value(10).toString();
    shipData.insert(shId, shData);
    }@

    Pulling the data into another application using a similar SELECT statement takes about 1 second so I'm wondering where I'm going wrong. A very helpful friend has been working with me on the build and guiding me through C++ and Qt but I'm feeling bad about monopolising their time, so if anyone can make any suggestions to speed this up I'd really appreciate it.

    Thanks,

    Mike

    1 Reply Last reply
    2
    • C Offline
      C Offline
      clochydd
      wrote on last edited by
      #2

      Hi, and welcome to DevNet,

      without knowing how your shData is declared, I think your problem lies here:

      @
      ShipData* shData = new ShipData(shId);
      @

      Is it really what you want: Create a new shData with every iteration?
      I suggest to generate ShipData once before while {}.

      1 Reply Last reply
      0
      • E Offline
        E Offline
        Eric Vialas
        wrote on last edited by
        #3

        You can have a problem with the network latency if, each time you call shipQuery.next(), the driver execute a round trip to the server get next row.
        The Qt documentation said that setForwardOnly must be called before preparing the query. Give it a try ...

        1 Reply Last reply
        2
        • M Offline
          M Offline
          Mike347
          wrote on last edited by
          #4

          Thanks for the replies! Moving setForwardOnly above prepare reduced the time taken to open from 55 seconds to 265ms!

          Really appreciate it.

          1 Reply Last reply
          1

          • Login

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