in

System.Data.SQLite

An open source ADO.NET provider for the SQLite database engine

Select Really Slow

Last post 05-22-2010 9:43 AM by seinkraft. 5 replies.
Page 1 of 1 (6 items)
Sort Posts: Previous Next
  • 05-18-2010 3:02 PM

    Select Really Slow

    I've a db with three tables. 

    images = wich contains the info of the image.( id | filename | width | height | private | ... )

    nodes = wich contains the image id and the tag id. ( image_id | tag_id | private )

    tags = wich contains tags. ( id | tag )

     

    The user can select the images seaching by tags using :

    SELECT images.* FROM images INNER JOIN nodes ON images.id = nodes.image_id WHERE tag_id IN (SELECT tags.id FROM tags WHERE tags.tag IN ('tag1','tag2')) GROUP BY images.id HAVING COUNT(*) = 2

    The problem is that i've nearly 50,000 entries on images table, the nodes table contains 452,299 entries and the tags table contains 4,612 entries.

    Now, the problem is that i try to retrieve 5000 images i've to wait almost 7 minutes to have all results :S

    The tables don't have primary keys, only the tags table, because the data is stored using the ids of the images downloaded from a website, so i cant set autoincrements on images table or ser a primary key because the user can download the images and also add his owns setting if it is private on tables images and nodes (private as char = "n" or "y")...

    So how can i increase the select speed? Increasing pragma?  cache? i'm really new on sqlite and don't have enough knowledge about it.

    Filed under:
  • 05-19-2010 6:47 AM In reply to

    Re: Select Really Slow

    Just in case you haven't thought of this, make sure you have an index created on any field you are looking for in a WHERE clause.  If you don't have an index, then it has to look through all of the records...

  • 05-19-2010 8:16 AM In reply to

    Re: Select Really Slow

    And how i can create an index non unique for the image table from vb.net?

  • 05-22-2010 7:54 AM In reply to

    Re: Select Really Slow

    I'm using a tool called NAVICAT which has a GUI interface to do it.

    I think the SQl command is something like: create index idx_tag on tags (tag) asc

    documentation: 

     http://www.sqlite.org/lang_createindex.html

  • 05-22-2010 8:03 AM In reply to

    Re: Select Really Slow

    I think you'll also need an index on the nodes table as well:

    create unique index idx_tag_id on nodes (tag_id) asc

     Basically you need an index on anything after a where clause if its not the primary index.

  • 05-22-2010 9:43 AM In reply to

    Re: Select Really Slow

     Yes, thank you. I've created a lot of indexes to solve thhis issues but is working fine. I ussualy use SQLite administrator.

Page 1 of 1 (6 items)
Powered by Community Server (Commercial Edition), by Telligent Systems