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

  • SEARCH
  • Home
  • 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 3356870
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T02:33:56+00:00 2026-05-18T02:33:56+00:00

I have a MySQL table in which a column contains string prefixes. For instance

  • 0

I have a MySQL table in which a column contains string prefixes. For instance these prefixes could be top-level directories on an Unix file system:

my_table:    
+---------+
| prefix  |
+---------+
|  /usr/  |
|  /bin/  |
|  /var/  |
|  /lib/  |
+---------+

How can I write a query that efficiently finds all rows in this table where the value of the prefix column is the beginning of a given string?

For instance given the string ‘/usr/bin/cat’ how can I write a query that finds the row containing ‘/usr/’ which is the beginning of ‘/usr/bin/cat’.

My first guess is to use LIKE this way:

SELECT * FROM my_table
WHERE '/usr/bin/cat' LIKE CONCAT(prefix, '%')

But I’m afraid this query won’t be using the index I have on the prefix column.

I also came up with the following:

SELECT * FROM my_table
WHERE prefix <= '/usr/bin/cat' ORDER BY prefix DESC LIMIT 1

Which retrieves the prefix equal to or immediately preceding ‘/usr/bin/cat’ in lexicographical order. I can then verify whether that prefix actually begins with ‘/usr/bin/cat’ or not.

But that only works with a single row and I wonder if that’s the optimal solution.

Edit: I used root directories as an example but I’d like to know if there’s a way to deal with arbitrary strings as well. Perhaps these strings won’t contain path separators or the prefix could be several level deep. Say: ‘/usr/lib’.

Edit: It seems that my second query is bogus. ‘/usr/’ is smaller than ‘/usr/bin/cat’ but so is ‘/usr/a’. That query is still much faster than a full table scan on a large table but to make it work I have to fetch more rows and go through them until I find the first actual prefix.

So it seems an index can help in this kind of prefix search but I still don’t know the best way to take advantage of it.

  • 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-05-18T02:33:56+00:00Added an answer on May 18, 2026 at 2:33 am

    — situation: We do not know where the string can be cut.

    — But we must know maximal length of the prefix.

    — EDIT: It would also help to know the minimal length of prefix – to eliminate lots of false positives that we do not want to find. (min = 2 characters).

    — This will definitely use the index: in this example it is max.8 characters. x = 8
    — in your application, just try to generate such SQL query:
    — No full table scan,just (x – min +1) times uses the index. Hopefully this will be FAST enough! 🙂

    SELECT * FROM my_table WHERE prefix = '/u'
    
     UNION
    
    SELECT * FROM my_table WHERE prefix = '/us'
    
     UNION
    
    SELECT * FROM my_table WHERE prefix = '/usr'
    
     UNION
    
    SELECT * FROM my_table WHERE prefix = '/usr/'
    
     UNION
    
    SELECT * FROM my_table WHERE prefix = '/usr/b'
    
     UNION
    
    SELECT * FROM my_table WHERE prefix = '/usr/bi'
    
     UNION
    
    SELECT * FROM my_table WHERE prefix = '/usr/bin';
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Say that I have a table with a bunch of records, which I want
I have a windows form application which interacts with a remote database. the database
I'm using two tables. First (friendlist), which contains users who are on the list
I have two tables. One contains information on hotels, added to my website by
I have two tables. One contains information on hotels, added to my website by
Is there any single msql query which can update customer DOB? I want to
I have my form <% form_tag users_path, :id => 'registrationForm' do %> <div class=formElement>
I just made a very simple test app using documentation from MSDN. All I
I am working on an application that allows users to dynamically add questions 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.