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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T03:53:17+00:00 2026-06-06T03:53:17+00:00

In my jsp application I have a search box that lets user to search

  • 0

In my jsp application I have a search box that lets user to search for user names in the database. I send an ajax call on each keystroke and fetch 5 random names starting with the entered string.
I am using the below query:
select userid,name,pic from tbl_mst_users where name like 'queryStr%' order by rand() limit 5
But this is very slow as I have more than 2000 records in my table.

Is there any better approach which takes less time and let me achieve the same..? I need random values.

  • 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-06T03:53:18+00:00Added an answer on June 6, 2026 at 3:53 am

    How slow is “very slow”, in seconds?

    The reason why your query could be slow is most likely that you didn’t place an index on name. 2000 rows should be a piece of cake for MySQL to handle.

    The other possible reason is that you have many columns in the SELECT clause. I assume in this case the MySQL engine first copies all this data to a temp table before sorting this large result set.

    I advise the following, so that you work only with indexes, for as long as possible:

    SELECT userid, name, pic
    FROM tbl_mst_users
    JOIN (
        -- here, MySQL works on indexes only
        SELECT userid
        FROM tbl_mst_users
        WHERE name LIKE 'queryStr%'
        ORDER BY RAND() LIMIT 5
    ) AS sub USING(userid); -- join other columns only after picking the rows in the sub-query.
    

    This method is a bit better, but still does not scale well. However, it should be sufficient for small tables (2000 rows is, indeed, small).

    The link provided by @user1461434 is quite interesting. It describes a solution with almost constant performance. Only drawback is that it returns only one random row at a time.

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

Sidebar

Related Questions

I have a JSP struts application that uses Spring to integrate with the services/dao/database.
Lets say in an application you have an entity called User. Each User can
situation: I have an application that uses separate jsp files for each specific country(I've
I have a simple jsp/servlet application and I want to add AJAX feature to
In my application I have added interceptor to filter the request.Here each user is
I have a application which allows user to make a search. Based on the
I have a spring web application with jsp pages that calls different web services
I have a home page in JSP of my MVC application that have some
I have a decent working web application (Java/Servlet/Jsp) that I would like to improve
I have a tomcat - spring mvc - jsp application. I have discovered that

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.