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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T09:02:52+00:00 2026-05-29T09:02:52+00:00

The Problem I have a table that’s about 2 million rows (at 115 MB)

  • 0

The Problem

I have a table that’s about 2 million rows (at 115 MB) and it’s about to be much larger. When running some utility scripts on the table I noticed one of my queries was taking a long time (15+ seconds) when a query that was nearly identical took less than a half second right before. Here are the queries:

Query 1:

SELECT `id` FROM `my_table` WHERE `my_column`='test' ORDER BY `id` LIMIT 28000, 1000
Execution time: 0.204 seconds

Query 2:

SELECT `id` FROM `my_table` WHERE `my_column`='test' ORDER BY `id` LIMIT 29000, 1000
Execution time: 10.203 seconds

Indexing and table info

id is a primary key and my_column is also indexed (although at the moment its cardinality is only 1)

• id is an int
• my_column is a varchar(50)

Queries explained

Query 1: type: index, possible_keys: my_column, key: PRIMARY, key_len: 4, rows: 29,000, Extra: Using where

Query 2: type: range, possible_keys: my_column, key: my_column, key_len: 53, rows: 2,139,123 Extra: Using where; Using filesort

As you can see the 2nd query is using the my_column key and filesort and taking forever, but all I did was increment the limit offset by 1,000.

How I temporarily fixed the problem

1) If I remove the WHERE my_column = 'test' condition the mysql optimizer correctly uses the primary key to sort, but I can’t remove this condition because soon enough there will be other values in my_column which I’m going need to filter out for this query.

2) If I use FORCE INDEX (PRIMARY) the mysql optimizer will also use the proper index, but this seems to be sort of a hack.

My question

Why exactly is mysql choosing to use the my_column index instead of the primary key? And is there a better way to handle this either in the table definition, indexes, or my query structure?

  • 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-29T09:02:53+00:00Added an answer on May 29, 2026 at 9:02 am

    I would try creating a composite index on the combination of (my_column, id).

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

Sidebar

Related Questions

My today's problem is that I have a table that has rows like that:
The problem: I have a table that records data rows in foo . Each
I have table rows that move up and down, but my problem is that
I have a big problem. I want to extract text from html table that
I have a class that is mapped to a table using NHibernate. The problem
I have a fairly simple sync problem. I have a table with about 10
Running into a problem. I have a table defined to hold the values of
here is a simple problem. I have a table of 500 rows and what
I have the following problem (simplified): I have a table that contains animals, e.g:
My problem is I have an existing table that I can not modify 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.