• Sign In

Technology?

A site for testing the new tools I'm building, and a little talk about what I'm learning on the way.

Search
  • Home
  • TechnologyView my occasional technology posts.
  • Life
  • Contact
Edit - Delete
Back and NextBack and Next - Back and Next
« One of the reasons I live in The Gambia
Internet Explorer CSS File Size Limit »
Edit - Delete
Show Media ItemShow Media Item - Converting to SQLite and LIKE Query Optimization

Converting to SQLite and LIKE Query Optimization

africa » gambia » fajara
Monday, January 28, 2008

I recently converted our web application .geographical media to use SQLite as its primary database (relegating SqlExpress to the backup position).  This posed some interesting challenges but overall I was very pleased with the result.  As they say on their website sqlite  is small, fast and reliable.  For us, the major reason for change was to reduce load on our hosting servers enabling us to put many more websites on a single server. 

Our product is built with .NET and we used the System.Data.SQLite library built by Robert Simpson which seems to be extremely well written and was super easy to use.  Great job by Robert and thank you. 

One of the most interesting things I faced during the conversion was the experience I had with one of our more common queries.  We use a LIKE query with a wildcard at the end to enable us to search a hierarchical structure for inclusive location information.  We have a path column which contains data like the following:

geo-Africa-Gambia-

geo-Africa-Egypt-

geo-Africa-Egypt-Cairo

geo-Africa-Senegal

Now to query this table for all records in Egypt we use the query:

    SELECT * FROM Table WHERE Path LIKE 'geo-Africa-Egypt-%'

This has always been fine in our SqlExpress days as even when we have had to do complex queries against 10s of thousands of records the response time was always under 100ms.  So it was with shock and concern that after converting to SQLite I suddently found the same queries taking as much as 16 seconds to run on the same machine! 

Fortunately for me a man name Jim Lyon, way back in september of 2003, wrote a document which both explained and solved my dilemma.  His document can be found here  as is an invaluable document about optimizing your SQLite databases and queries.  The crux of the problem is this

 The GLOB and LIKE operators are expensive in SQLite because they can't make use of an index.

There is some reason for this but fortunately for me our LIKE query just has a wildcard at the end.  Jim explains all the details, but in the end it is possible to optimize this query to look like the following:

    SELECT * FROM Table WHERE Path >= 'geo-Africa-Egypt-' AND Path < 'geo-Africa-Egypt-zzz'

There MUST be an index on the Path column for this to work.  By adding the 'zzz' at the end the query will naturally select everything in the alphabetic sort order between the first and second query parameters. 

The end result was spectacular, my newly optimized queries were now running super fast, on both SQLite (~50ms) and even improved on SqlExpress (~60ms).  I have to give credit to Microsoft, as they must have some optimizations already built in for this type of query and its been making my life easy up until this point.     

I think making the switch to SQLite is a good on and can be extremely rewarding for most applications.  Overall, to get the performance I was looking for it made me a bit more honest of a programmer, I had to ensure all my queries were in proper transactions and indexes were explicitly stated.  Sometimes I found the documentation on the SQLite website a bit lacking. 

Edit - Delete
CommentsComments - Comments
Comments
 
Derk - Europe, World - Friday, April 09, 2010 10:50 PM
I found that document to be invaluable too. Tried to contact the person asking if he was going to complete it someday. But his e-mail address was outdated.
Wilson - Manila, National Capital Region - Friday, January 28, 2011 4:48 AM
How about LIKE %string%

How is it converted?

I have seen the docs and searched Google and I cannot see any optimization for it.

Your Comment

Your Name

Your Location

To receive emails create a username or Sign In
Your Email
Username
Password
Confirm Password
Add me to the contact list.
Remember me on this computer.
Sign in below or Sign Up
Username
Password
loading...  Post Comment
 
Edit - Delete
Related TopicsRelated Topics - Related Topics
Related Topics
sqlite, sql, query, optimization
Edit - Delete
Media ActionsMedia Actions - Media Actions
Media Actions
0
Promote
Email to a friend
Inquire
Save to delicious
Digg this
Stumble it
Edit - Delete
See AlsoSee Also - See Also
See Also
  • Internet Explorer CSS File Size Limit
  • Javascript Transition Effects
  • Libyan Envoy Hails Gambia-Libya Relations
  • MRC Laboratories, The Gambia
  • Portfolio Slideshow

Try Lara

Build your own website with Lara.  A powerful content management system with easy and fun to use design tools. 

Visit http://getlara.com to learn more.

Go here for a good Javascript Html Editor.

Subscribe Now

Recent Posts

  • Ultimate Frisbee Mauritius
  • The best free icon collection on the internet - Icons Etc.
  • VGA Planets "Nu" - The official VGA Planets remake
  • Sending Email from Amazon EC2
  • Using SSL (https) with Mono HTTPListener
  • .NET MimeTypes Class in C#
  • Great WordPress Plugins
  • Flash IO Error #2038 when Uploading Files in .NET
  • Javascript DOM Ready for SSL in IE
  • Javascript Replace All with Variable
Website created with Lara by Geographical Media