Results 1 to 6 of 6

Thread: fast search + QThread

  1. #1
    Join Date
    Nov 2010
    Posts
    30
    Thanks
    3
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default fast search + QThread

    Hi All,
    i want to search in large libraries with sql
    about 20000 books;
    each book is in one table and all book names stored in table named books;

    my code for search is :
    Qt Code:
    1. void MyThread::run()
    2. {
    3. QStringList bookTables =db->getList("Select booktable From books;");
    4. QStringList bookNames =db->getList("Select bookNames From books;");
    5. int rowBook=0;
    6. int totalResultBooks=0;
    7. int totalResult=0;
    8. int allBookCount=bookTables.size();
    9. emit sendMaxProgress(allBookCount-1);
    10. while(rowBook<allBookCount )
    11. {
    12. QString query = QString("Select count(id) From b%1 Where text LIKE '%%2%' Limit 0,1 ; ").arg(bookTables.at(rowBook)).arg(searchPatterned);
    13. QString resultBook =db->getStr(query);
    14. if(resultBook.toInt()>0)
    15. {
    16. totalResult+= resultBook.toInt();
    17. totalResultBooks++;
    18. item->setText(bookNames.at(rowBook)+" ("+resultBook+") ");
    19. item->setData(12,bookTables.at(rowBook));
    20. QString str = "found "+QString::number(totalResult)+"at"+QString::number(totalResultBooks)+"Books";
    21. emit sendResult(item,str,rowBook);
    22. }
    23. msleep(40);
    24. rowBook++;
    25. }
    26.  
    27. }
    To copy to clipboard, switch view to plain text mode 

    my question is :
    i used :
    Qt Code:
    1. msleep(40);
    To copy to clipboard, switch view to plain text mode 

    to free 20% of cpu ..

    is this way right?
    is there any better way ?

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: fast search + QThread

    First of all you cannot use the same database connection in more than one thread, and that's what you are doing here. Furthermore, with msleep(40) you are not "saving" CPU power, you're just making your search slower. Third of all, you can't access the GUI from within a worker thread and that's what you are doing too.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  3. #3
    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: fast search + QThread

    The search for free text in a large number of books would be greatly assisted by putting them all in one table and using Sqlite's full text search extension (you will need to enable this in Sqlite yourself: plenty of Google sources). It will improve performance so dramatically you will likely not feel the need to farm the work out into a thread (with all the problems that entails) .

    Even if you did not use the FTS extension, putting all the books into a single table would still be a performance improvement: one query to prepare and execute versus 20000+.

    Random thought: What is the point of a LIMIT clause on a query that can only ever return one row?

  4. #4
    Join Date
    Nov 2010
    Posts
    30
    Thanks
    3
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: fast search + QThread

    Quote Originally Posted by wysota View Post
    with msleep(40) you are not "saving" CPU power, you're just making your search slower. Third of all, you can't access the GUI from within a worker thread and that's what you are doing too.
    i can access to gui within search in linux but in windows can not access to gui ..

    however help me how use multi threaded for that code



    Added after 13 minutes:


    Quote Originally Posted by ChrisW67 View Post
    The search for free text in a large number of books would be greatly assisted by putting them all in one table and using Sqlite's full text search extension (you will need to enable this in Sqlite yourself: plenty of Google sources). It will improve performance so dramatically you will likely not feel the need to farm the work out into a thread (with all the problems that entails) .

    Even if you did not use the FTS extension, putting all the books into a single table would still be a performance improvement: one query to prepare and execute versus 20000+.
    the size of my book in sqlite is larger than 15 gig !
    is it goode way to store all books in one table ?
    also i want to use regexp faction in query and i don't think fts can do that .
    Random thought: What is the point of a LIMIT clause on a query that can only ever return one row?
    it jast find matched name books
    by clicking in book in list item find all result by this query :
    QString("Select text,page From b%1 Where text REGEXP '%2' ; ").arg(item->data(12).toString()).arg(searchPatterned);
    Last edited by solook; 13th November 2012 at 03:45.

  5. #5
    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: fast search + QThread

    the size of my book in sqlite is larger than 15 gig !
    So?
    is it goode way to store all books in one table ?
    They're all in the one Sqlite file already aren't they? Sqlite will have no issue with a single table of that size: the default database limits are typically 1 billion 1k pages and a table can use all that.
    also i want to use regexp faction in query and i don't think fts can do that .
    Your examples doesn't show that you wanted to use a regular expression. Sqlite doesn't have a regular expression matching ability by default; you have to provide an implementation of that yourself. See http://stackoverflow.com/questions/5...a-sqlite-query.

    FTS has a boolean search language that's quite capable but only you know your actual requirement. You still benefit from only executing one query rather than 20000 either way.


    Line 12 of your example is a select returning the count(*) aggregate without grouping of any sort. This will always return one row and only one row, but you have a limit clause.

  6. #6
    Join Date
    Nov 2010
    Posts
    30
    Thanks
    3
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: fast search + QThread

    thanks..

    They're all in the one Sqlite file already aren't they? Sqlite will have no issue with a single table of that size: the default database limits are typically 1 billion 1k pages and a table can use all that.
    i tested but this error returned :
    Qt Code:
    1. database disk image is malformed
    To copy to clipboard, switch view to plain text mode 

    Your examples doesn't show that you wanted to use a regular expression. Sqlite doesn't have a regular expression matching ability by default; you have to provide an implementation of that yourself. See http://stackoverflow.com/questions/5...a-sqlite-query.
    i know Sqlite doesn't have a regular expression by default
    i enabled it by myself

    Qt Code:
    1. QString query = QString("Select count(id) From b%1 Where text REGEXP '%%2%' Limit 0,1 ; ").arg(bookTables.at(rowBook)).arg(searchPatterned);
    To copy to clipboard, switch view to plain text mode 

    Line 12 of your example is a select returning the count(*) aggregate without grouping of any sort. This will always return one row and only one row, but you have a limit clause.
    i don't want to show all result in each book
    but just find that matched book name and count of result
    then by clicking in book list find all result of selected book :
    QString("Select text,page From b%1 Where text REGEXP '%2' ; ").arg(item->data(12).toString()).arg(searchPatterned);

Similar Threads

  1. Replies: 1
    Last Post: 14th November 2012, 21:00
  2. Replies: 1
    Last Post: 4th October 2012, 14:49
  3. Fast QList search/access
    By tryinghard in forum Newbie
    Replies: 4
    Last Post: 15th January 2011, 02:00
  4. Qt3 more fast than Qt4 ?
    By commarmi in forum Qt Programming
    Replies: 11
    Last Post: 14th September 2007, 14:36
  5. Fast Keyboard, help !
    By Alex63 in forum Qt Programming
    Replies: 2
    Last Post: 27th June 2006, 18:18

Tags for this Thread

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
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.