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 8932075
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T09:15:35+00:00 2026-06-15T09:15:35+00:00

There is a table holding some values like: id | prefix | name —-+—————-+————————–

  • 0

There is a table holding some values like:

id  | prefix         | name 
----+----------------+--------------------------
1   | record1        | name for record 1
2   | record2        | name for record 2
3   | record         | name for record 3
4   | another rec    | name for record 4

In order to select the longest prefix of a given text and return the name I use the following SQL:

select top 1 name from prefixes where :text like prefix + '%' order by prefix desc

And this is exactly what I need, when I give text record1 it returns me name for record 2 when record1 it returns me name for record 1, if I give a it returns me name for record 4.

But the problem is that this is executed a few times and the table is updated a lot, so the performance in my case (table with just 210000 rows) is around 300ms, I would like to reduce this, is there something could be improved on the query or even on the database?

  • 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-15T09:15:36+00:00Added an answer on June 15, 2026 at 9:15 am

    I don’t know Sybase internals really well. However, look at the plan to see if it is using the index. If so, is it doing a full scan of the index or is the engine smart enough to understand the “like”.

    My guess is that the engine is doing a full scan. You might be able to trick it to seeking to the right starting location by changing the query:

    where prefix >= :text and :text like prefix + '%'
    

    However, it will probably do a full scan from that point forward. You can fix this by having a maximum place to search:

    where prefix >= :text and prefix <= :text + 'zzz'
    

    (Assuming that you are using alpha-numeric values in the prefix, this should be ok. You can also use something like :text + ‘}’, because ‘}’ has a very high ASCII value, assuming you are using an ASCII collating sequence.)

    Are your prefixes known in advance? That is, for “record1” is the prefix always “record”? Or are you considering “r”, “re”, and so on.

    If the former, then add a new column which contains the “base” part of the prefix. Build an index on this column and change the join to equality. The engine will fetch only the records from the index.

    The issue of having the column “name” in the index is to prevent the additional step of looking up the name on the data pages for the table. Once again, this depends on how Sybase optimizes the query. It should find the appropriate records only using the index and then look up the fields after applying the top 1. However, if it fetches all the values, then applies the top 1, having “name” in the index will be a benefit.

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

Sidebar

Related Questions

in a Table there is a single attribute(column).it contains numbers.Some are repeated(2 or 3
in an sql table there's an id, first name and last name field. i'd
I have an array controller holding some values, say - subjects. I am displaying
Im making a mysql table which will be holding my blog posts and some
Hello I have table in my database holding some browser information I'm currently using
Consider a database table holding names, with three rows: Peter Paul Mary Is there
Given a table holding edges in a directed graph like this: CREATE TABLE edges
Is there a table i can query all of Oracle Reserved Words? Example, i
In a MySQL (5.1) database table there is data that represents: how long a
I have a dynamically generated table and for each row in the table there

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.