Solved

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

Posted on 2008-10-24
4
1,233 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

In this article, I will show you HOW TO: Install VMware Tools for Windows on a VMware Windows virtual machine on a VMware vSphere Hypervisor 6.5 (ESXi 6.5) Host Server, using the VMware Host Client. The virtual machine has Windows Server 2016 instal…
The Windows functions GetTickCount and timeGetTime retrieve the number of milliseconds since the system was started. However, the value is stored in a DWORD, which means that it wraps around to zero every 49.7 days. This article shows how to solve t…
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
Windows 10 is mostly good. However the one thing that annoys me is how many clicks you have to do to dial a VPN connection. You have to go to settings from the start menu, (2 clicks), Network and Internet (1 click), Click VPN (another click) then fi…

756 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