Solved

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

Posted on 2008-10-24
4
1,227 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
Comment Utility
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
Comment Utility
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
Comment Utility
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]
Comment Utility
>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

Promote certifications in your email signature

Has your company recently won an award or achieved a certification? They'll no doubt want to show it off. Email signature images used to promote certifications & awards can instantly establish credibility with a recipient and provide you with numerous benefits.

Join & Write a Comment

The use of stolen credentials is a hot commodity this year allowing threat actors to move laterally within the network in order to avoid breach detection.
This is an article about Leadership and accepting and adapting to new challenges. It focuses mostly on upgrading to Windows 10.
In this video, we discuss why the need for additional vertical screen space has become more important in recent years, namely, due to the transition in the marketplace of 4x3 computer screens to 16x9 and 16x10 screens (so-called widescreen format). …
Windows 8 came with a dramatically different user interface known as Metro. Notably missing from that interface was a Start button and Start Menu. Microsoft responded to negative user feedback of the Metro interface, bringing back the Start button a…

772 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

11 Experts available now in Live!

Get 1:1 Help Now