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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T10:44:20+00:00 2026-05-27T10:44:20+00:00

I have a table which will be about 2 – 5 million rows on

  • 0

I have a table which will be about 2 – 5 million rows on average. It has a primary key/index called ‘instruction_id’ and another indexed field called ‘mode’. now ‘instruction_id’ is of course unique since it is the primary key but ‘mode’ will only be one of 3 different values. The query I run all the time is

SELECT * FROM tablename WHERE mode = 'value1' ORDER BY instruction_id LIMIT 50

This currently takes about 25 sec ( > 1 sec is unacceptably long) but there are only 600K rows right now so it will get worse as the table grows. Would indexing in a different way help? If I index instruction_id and mode together will that make a difference? If I somehow am able to naturally order the table by instruction_id so I don’t have to ask for the order by would be another way around this but I don’t know how to do that… Any help would be great.

  • 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-27T10:44:20+00:00Added an answer on May 27, 2026 at 10:44 am

    You should try index on (mode, instruction_id), in that order.

    The reasoning behind that index is that it creates an index like this

    mode  instruction_id
    A     1
    A     3
    A     4
    A     5
    A     10
    A     11
    B     2
    B     8
    B     12
    B     13
    B     14
    C     6
    C     7
    C     9
    C     15
    C     16
    C     17
    

    If you search for mode B the sql server can search the index with a binary search on mode until it finds the first B, then it can simply output the next n rows. This would be really fast, about 22 compares for 4M rows.

    Always use ORDER BY if you expect the result to be ordered, regardless of how the data is stored. The query engine might choose a query plan that output the rows in a different order than the order of the PK (maybe not in such simple cases as this, but in general).

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

Sidebar

Related Questions

I have got a table which has an id (primary key with auto increment),
I have table which has about 50 rows. Each row has link, but I
if i have a table which has columns with fixed lenght, Will mySQL count
I have a table in SQL server which has a DATETIME field called Date_Printed.
I have a table 'service' which contains details about serviced vehicles. It has an
I have a table which needs 2 fields. One will be a foreign key,
I have table which will only contain information about files, so, as the file
What I can think of is: Algo: Have a hash table which will store
I have a table of transactions which will occasionally have duplicate entries. If/When an
I have created an archive table which will store data for selecting only. Daily

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.