Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8762767
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T15:37:00+00:00 2026-06-13T15:37:00+00:00

I have a couple of issues with SQLite query. Actually I start thinking that

  • 0

I have a couple of issues with SQLite query. Actually I start thinking that SQLite is not designed for tables with more then 10 rows, really, SQLite is a nightmare.

The following query

SELECT * FROM [Table] WHERE [Name] LIKE 'Text%'

It works fine. EXPLAIN shows that the index is used and result is returned after about 70ms.

Now I need to run this query from .NET SQLite driver, so I’m changing query

SELECT * FROM [Table] WHERE [Name] LIKE @Pattern || '%'

Index is not used. When I run the following query in any SQLite tool the index is not used as well

SELECT * FROM [Table] WHERE [Name] LIKE 'Text' || '%'

So I guess SQLite doesn’t have any kind of preprocessing logic implemented.

OK. Let’s try to solve it, I’m still binding variables and doing the following

SELECT * FROM [Table] WHERE [Name] LIKE @Pattern

But now I append % wildcard symbol to the end of my pattern string, like this

command.Parameters.Add(new SQLiteParameter("@Pattern", pattern + '%'));

It works very slow. I can’t say why, because when I run this query from SQLite tool it works fine, however when I bind this variable from .NET code it works slow.

OK. I’m still trying to solve this. I’m getting rid of the pattern parameter binding and building this condition dynamically.

pattern = pattern.Replace("'", "''");
pattern = pattern.Replace("%", "\\%");
where = string.Format("LIKE '{0}%' ESCAPE '\\'", pattern);

Index is not used again. It’s not used because of ESCAPE. I see that when I run

EXPLAIN QUERY PLAN SELECT * FROM [Table] WHERE [Name] LIKE 'Text%' ESCAPE '\'

As soon as I remove ESCAPE it starts using index again and the query finishes in 60-70ms.

UPDATE

Here are the results.

EXPLAIN QUERY PLAN
SELECT * FROM [RegistryValues]
WHERE
     [ValueName] LIKE 'windir%' ESCAPE '\' 

SCAN TABLE RegistryValues (~3441573 rows)

and the one without ESCAPE

EXPLAIN QUERY PLAN
SELECT * FROM [RegistryValues]
WHERE
     [ValueName] LIKE 'windir%'

SEARCH TABLE RegistryValues USING INDEX IdxRegistryValuesValueNameKeyIdKeyHiveFileId (ValueName>? AND ValueName<?) (~31250 rows)

UPDATE

Just found this

http://www.sqlite.org/optoverview.html

4.0 The LIKE optimization

The ESCAPE clause cannot appear on the LIKE operator

So what should I do then?

Do I understand it right? I can’t search string containing wildcards using LIKE operator in SQLite. By saying wildcards I mean _ % ^ !

It’s impossible simply because I can’t escape them.
Actually I can, but I can’t use indexes in this case, so the query will not be efficient.

Am I right?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T15:37:02+00:00Added an answer on June 13, 2026 at 3:37 pm

    An index can only be used with a LIKE clause when the % is at the end so that SQLite can rewrite it to two simple comparisons (as shown in the EXPLAIN output).

    Therefore, to get the same effect, write the comparisons yourself.
    This requires that you construct some string that is guaranteed to compare ‘larger’ than any of the matched values (beware of non-ASCII characters).
    Instead of:

    ... WHERE Name LIKE 'Text%'
    

    use:

    ... WHERE Name BETWEEN 'Text' AND 'Textzzzzzzzzzzzzz'
    

    or, as parameter:

    ... WHERE Name BETWEEN @Pattern AND @Pattern || 'zzzzzzzzzz'
    

    (This construct never needs escapes. 🙂

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a couple of things I'm working on, namely a page that issues
We have a couple of synonyms that are being used to reference tables in
I have a couple of tables in a SQL 2008 server that I need
I have couple of issues- the first constraint on select is loaded when reference
For the last couple months i have ben having some issues with my app.config
I have couple of dozen pieces of data that I need to save and
I have couple resource DLLs that I currently load when application starts using following
I have a couple of sqlite dbs (i'd say about 15GBs), with about 1m
T4MVC is cool, but I have a couple of issues integrating it in my
I have a couple issues with it: media fails to play and continues to

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.