Solved

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

Posted on 2008-10-24
4
1,231 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 143

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 143

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

How our DevOps Teams Maximize Uptime

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us. Read the use case whitepaper.

Question has a verified solution.

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

NTFS file system has been developed by Microsoft that is widely used by Windows NT operating system and its advanced versions. It is the mostly used over FAT file system as it provides superior features like reliability, security, storage, efficienc…
If you get continual lockouts after changing your Active Directory password, there are several possible reasons.  Two of the most common are using other devices to access your email and stored passwords in the credential manager of windows.
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…
Windows 8 comes with a dramatically different user interface known as Metro. Notably missing from the new interface is a Start button and Start Menu. Many users do not like it, much preferring the interface of earlier versions — Windows 7, Windows X…

856 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