Results 1 to 5 of 5

Thread: code it is slow.

  1. #1
    Join Date
    Mar 2010
    Location
    spain
    Posts
    25
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows
    Thanks
    1

    Default code it is slow.

    I have a code but it is slow.

    Is there any way to do this differently faster or efficient?


    Qt Code:
    1. QSqlQuery query(db);
    2.  
    3.  
    4. int row;
    5. QDateTime FechaHoy;
    6.  
    7. query.exec("SELECT rowid,* FROM HORAS");
    8. query.first();
    9.  
    10.  
    11. QSqlQuery queryFecha(db);
    12. queryFecha.prepare("UPDATE HORAS SET"
    13. " HORA = ?"
    14. " WHERE rowid = ?");
    15.  
    16.  
    17. while (query.isValid()){
    18. QSqlRecord rec = query.record();
    19.  
    20. row=rec.value("rowid").toInt();
    21.  
    22.  
    23. if(rec.value("HORA").toDateTime().time()<= QTime::currentTime()){
    24. FechaHoy.setDate(QDate::currentDate().addDays(1));
    25.  
    26. }else{
    27. FechaHoy.setDate(QDate::currentDate());
    28. }
    29.  
    30.  
    31.  
    32. FechaHoy.setTime(rec.value("HORA").toDateTime().time());
    33.  
    34. queryFecha.addBindValue(FechaHoy);
    35. queryFecha.addBindValue(row) ;
    36. queryFecha.exec();
    37. query.next();
    38.  
    39.  
    40. }
    41.  
    42.  
    43. }
    To copy to clipboard, switch view to plain text mode 

    thanks

  2. #2
    Join Date
    Aug 2008
    Location
    Ukraine, Krivoy Rog
    Posts
    1,963
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows
    Thanked 370 Times in 336 Posts

    Default Re: code it is slow.

    Move it in other thread. This is nice how to.
    Qt Assistant -- rocks!
    please, use tags [CODE] & [/CODE].

  3. #3
    Join Date
    Mar 2010
    Location
    spain
    Posts
    25
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows
    Thanks
    1

    Default Re: code it is slow.

    is not possible in a single update statement

  4. #4
    Join Date
    Aug 2008
    Location
    Ukraine, Krivoy Rog
    Posts
    1,963
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows
    Thanked 370 Times in 336 Posts

    Default Re: code it is slow.

    Quote Originally Posted by valgaba View Post
    is not possible in a single update statement
    Please, explain.
    Qt Assistant -- rocks!
    please, use tags [CODE] & [/CODE].

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

    Default Re: code it is slow.

    Quote Originally Posted by valgaba View Post
    is not possible in a single update statement
    Sure it is. Here's the Sqlite example:
    Qt Code:
    1. create table HORAS ( HORA varchar(19) );
    2. insert into HORAS values('2012-08-14 04:03:00');
    3. insert into HORAS values('2012-08-14 05:00:00');
    4. insert into HORAS values('2012-08-13 04:03:00');
    5. insert into HORAS values('2012-08-13 05:00:00');
    6.  
    7. select 'Run at: ' || datetime();
    8. select rowid, date(HORA), time(HORA) from HORAS;
    9. select '---';
    10.  
    11. update HORAS
    12. set HORA =
    13. CASE
    14. WHEN time(HORA) <= time() THEN datetime(date(), time(HORA), '+1 day')
    15. ELSE datetime(date(), time(HORA))
    16. END;
    17.  
    18. select rowid, date(HORA), time(HORA) from HORAS;
    To copy to clipboard, switch view to plain text mode 
    I am fairly certain a workable Oracle equivalent would be possible.

    If you persist with the iterative approach then here are some tips:
    • As described in the QSqlQuery docs, "For the sake of efficiency, there are no functions to access a field by name." It goes on to describe using record().indexOf(). Do this name to column number conversion only once as in the example.
    • Better still, explicitly name the columns you need, and only those columns, in your query at line 7 and use the column index numbers. No need to fetch 10 columns when you only need one.
    • Convert the column value to a QDateTime only once.
    • The result will always be either today's date or tomorrow's date with the column's existing time so you can rearrange the code a bit.
    • At least for Sqlite rowid is not an int, it is a 64-bit number.
    • If you are running this on a million row table then you are executing a million queries: this will take some time no matter what.
    • If you are doing this in Sqlite do it inside a transaction: see http://www.sqlite.org/faq.html#q19 which would equally apply to Delete I expect.


    Something like:
    Qt Code:
    1. db.transaction();
    2. while (query.next()) {
    3. QDateTime FechaHoy = query.value(1).toDateTime();
    4. FechaHoy.setDate(QDate::currentDate());
    5. if (FechaHoy.time() <= QTime::currentTime())
    6. FechaHoy = FechaHoy.addDays(1);
    7.  
    8. // bind and exec()
    9. }
    10. db.commit(); // I have left out error checking
    To copy to clipboard, switch view to plain text mode 

Similar Threads

  1. Very slow startup by the first run
    By denizlitr in forum Qt for Embedded and Mobile
    Replies: 14
    Last Post: 2nd November 2016, 07:30
  2. Win 7 , Ms compiler, too slow .....
    By tonnot in forum Installation and Deployment
    Replies: 0
    Last Post: 8th February 2012, 09:41
  3. Qt goes slow?
    By Cucus in forum Qt Programming
    Replies: 2
    Last Post: 20th July 2011, 04:43
  4. Qt4 designer SLOW - how to fix?
    By hvengel in forum Qt Tools
    Replies: 3
    Last Post: 10th January 2008, 12:07
  5. Pasting code from code tag in emacs
    By Gopala Krishna in forum General Discussion
    Replies: 0
    Last Post: 16th February 2007, 05:47

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.