Solved

How do improve SQLCE 3.5 performance when using LIKE & wildcards in where statement?

Posted on 2008-10-24
4
1,229 Views
Last Modified: 2013-12-17
Here is an example of a the SQL query that I run in a C#.Net mobile application running on a PPC2003 device:

SELECT top (100) ITEM_ID, DESCRIPTION, RETAIL, UPC, CUSTOMER_SKU FROM items
 where
(
ITEM_ID  like '%54%'
OR
DESCRIPTION  like '%54%'
OR
UPC  like '%54%'
OR
CUSTOMER_SKU  like '%54%'
)

The data is in a SQL CE 3.5 compact database file.  The .sdf file is 18 megs, with 54K records.  All four of the columns being used in the where statement have an index.  When I execute the above query, the results are populated to a grid instanteously.  But if the user enters a search value of "1554", the query will look like this:
SELECT top (100) ITEM_ID, DESCRIPTION, RETAIL, UPC, CUSTOMER_SKU FROM items
 where
(
ITEM_ID  like '%1554%'
OR
DESCRIPTION  like '%1554%'
OR
UPC  like '%1554%'
OR
CUSTOMER_SKU  like '%1554%'
)

But in the case of "1554" or another larger number, the query takes a LONG time - about 1-2 minutes to return a result.  All the fields are NVARCHAR.  DESCRIPTION is an 80 char field, the others are about 15 characters.

Background: The idea here is for the user to be able to enter a search string and search ALL of these data columns, anywhere within the column, to find occurrances of the string.  The returned dataset then populates a grid.

Here is a snip of the C#.Net code:
private void search(string sqlstatement)
        {
            //Fire tis when this form loads to search for text entered by user.
            try
            {
                {
                    if (App.DBConnected == true)
                    {
                        //Wait cursor
                        Cursor.Current = Cursors.WaitCursor;

                        //Create new command
                        SqlCeCommand cmd = App.Conn.CreateCommand();
                        //Set SQL statement                              

                        string sql = sqlstatement;
                        long reccount = 0;

                        cmd.CommandText = sql;

                        // Create a DataSet to put our results in
                        DataSet ds = new DataSet();

                        // Create the adapter
                        SqlCeDataAdapter da = new SqlCeDataAdapter(cmd);

                        resizedRows.Clear();

                        // Fill it
                        da.Fill(ds, "Items");

                        reccount = ds.Tables["items"].Rows.Count;
                        lblRecCount.Text = "Found: " + reccount.ToString("#,##0");
                        //dtgItemList.DataSource = ds.Tables["items"];
                        dgLookup.DataSource = ds.Tables["items"];


Any idea why this behavior would occur?  Any ideas to improve the query, achieve the same results?
0
Comment
Question by:dhollow2
  • 2
  • 2
4 Comments
 
LVL 142

Accepted Solution

by:
Guy Hengel [angelIII / a3] earned 250 total points
ID: 22799769
field LIKE '%xxx%'
cannot be optimized or indexed.


with full sql server, a full-text index could be an alternative, but not with CE
0
 

Author Comment

by:dhollow2
ID: 22800306
Thanks for the reply.  That helps me understand the problem with the compact version of SQL.  I does work much better without the LIKE % stuff with it.  This definately lets me know that Full-Text Search isn't avail either.  

Just helping me understand fully here:
Is it true that the more characters I use to search with, the longer the result will take to return?  That seems to be the behavior to me.
0
 

Author Closing Comment

by:dhollow2
ID: 31509813
I just had a follow-up question to clarify the issue, but am awarding points now.  Thanks for quick reply.
0
 
LVL 142

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 22800354
>Is it true that the more characters I use to search with, the longer the result will take to return?  
it should be actually the other way round, because the longer the value you search, the less chances/tests there are to be done to check ...

however, I don't know if CE version works "differently" in that matter.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I'm trying, I really am. But I've seen so many wrong approaches involving date(time) boundaries I despair about my inability to explain it. I've seen quite a few recently that define a non-leap year as 364 days, or 366 days and the list goes on. …
This article describes how to use the timestamp of existing data in a database to allow Tableau to calculate the prior work day instead of relying on case statements or if statements to calculate the days of the week.
This video Micro Tutorial explains how to clone a hard drive using a commercial software product for Windows systems called Casper from Future Systems Solutions (FSS). Cloning makes an exact, complete copy of one hard disk drive (HDD) onto another d…
The Task Scheduler is a powerful tool that is built into Windows. It allows you to schedule tasks (actions) on a recurring basis, such as hourly, daily, weekly, monthly, at log on, at startup, on idle, etc. This video Micro Tutorial is a brief intro…

929 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now