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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T11:07:45+00:00 2026-06-08T11:07:45+00:00

I have a very simple MySQL table with some very weird behavior. Incidentally the

  • 0

I have a very simple MySQL table with some very weird behavior. Incidentally the weird behavior is exactly what I want it to do, but I don’t want to put this into production not know why its doing what its doing.

Anyways, I’ve got a table created like so:

Create table `raceTimes` (
    `userID` mediumint(8) unsigned,
    `time` time,
    primary key (`userID`),
    key `idx_time` (`time`)
) engine=InnoDB default charset=utf8;

Now when I do a Select * from raceTimes query, I get a result set like this:

mysql> Select * from raceTimes;
+--------+----------+
| userID | time     |
+--------+----------+
|     14 | 12:37:46 |
|      6 | 12:41:11 |
|      5 | 12:48:45 |
|     13 | 12:55:46 |
|     10 | 13:13:37 |
|      9 | 13:40:37 |
|     17 | 15:30:44 |
|     18 | 15:46:58 |
|      3 | 16:16:45 |
|      8 | 16:40:11 |
|      7 | 16:41:11 |
|      4 | 16:48:45 |
|     16 | 20:30:44 |
|     15 | 20:37:44 |
|      1 | 21:00:00 |
|      2 | 21:16:00 |
|     11 | 23:13:37 |
|     20 | 23:14:58 |
|     19 | 23:46:58 |
|     12 | 23:55:46 |
+--------+----------+

Notice that the result set is order based on the times, from lowest to highest. Ok, so that is exactly what I want the table to do since I’m trying to use this for a leaderboard in a game. When I run explain on my query I get this:

mysql> explain select * from raceTimes;
+----+-------------+------------+-------+---------------+----------+---------+------+------+-------------+
| id | select_type | table      | type  | possible_keys | key      | key_len | ref  | rows | Extra       |
+----+-------------+------------+-------+---------------+----------+---------+------+------+-------------+
|  1 | SIMPLE      | raceTimes  | index | NULL          | idx_time | 4       | NULL |   20 | Using index |
+----+-------------+------------+-------+---------------+----------+---------+------+------+-------------+

So far everything is great. I’m getting back a sorted result set due to the idx_time index being sorted (as indexes are) and to that end I’m hitting on an index. Now for the weird behavior.

From what I’ve read, the primary key is indexed by default and is supposed to be the fastest index when querying a table. Yet its not being used. My guess for this is that the idx_time index is smaller than the primary key index, due to it being a time type as opposed to a mediumint(8) type. But it’s just a guess.

Now if I create an identical table to the one I created above, but omit the primary key, like so:

Create table `raceTimes2` (
    `userID` mediumint(8) unsigned,
    `time` time,
    key `idx_time` (`time`)
) engine=InnoDB default charset=utf8;

then the result set isn’t sorted on the time column this go. This behavior holds even if I tell it to specifically use the idx_time index in my query. Also, if I do an explain on the query I get this:

mysql> explain select * from testTable6 use index(`idx_time`);
+----+-------------+------------+------+---------------+------+---------+------+------+-------+
| id | select_type | table      | type | possible_keys | key  | key_len | ref  | rows | Extra |
+----+-------------+------------+------+---------------+------+---------+------+------+-------+
|  1 | SIMPLE      | raceTimes2 | ALL  | NULL          | NULL | NULL    | NULL |   20 |       |
+----+-------------+------------+------+---------------+------+---------+------+------+-------+

So what I’m trying to find out is what’s going on behind the scenes. Why does it appear that if I’ve got a primary key and another index, I can get a result set sorted on the index without even trying and why does the query optimizer go with that other index rather than the primary key index?

  • 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-08T11:07:47+00:00Added an answer on June 8, 2026 at 11:07 am

    As Gordon noted, you should not depend on the natural order of the result set. The reason for the results you are getting is the following:

    In the first case MySQL executes the query only by using the idx_time index, without opening the actual table. This is possible when all the columns you are using are in the index (the primary key of an InnoDB table is always appended to the end of every index, so your index is actualy (time,userID) behind the scenes). The results are ordered by time because this is the actual order in the time index.

    In the second case the column userID is not part of any index and MySQL has to do a regular table scan to fetch the result. “use index(idx_time)” in this case does nothing because there is no WHERE clause using the time column.

    Edit:
    It applies only when there is a choice, but if its not possible to use the index specified in USE INDEX MySQL will not use any index on that table for searching (WHERE/ON clause) and will read the entire table. So you should be very careful when using index hints.
    Also a row in explain with type=’index’ means that all the rows in the table will be read, and is almost as bad as type=’ALL’.

    You should check out the MySQL manual on index hints and explain output.

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

Sidebar

Related Questions

I have a very simple MySQL table Name | Time | Count James |
I currently have a very simple MySQL database (articlesDB) with 1 table (articles) and
This might come across as a simple question, but I have very limited experience
Let's assume we have this very simple table: |class |student| --------------- Math Alice Math
I have a very simple table called Member , which consists of the following:
I have this very simple jQuery function: $(.milestone-in-tree).live({ mouseenter: function() { setTimeout( $.ajax({ type:
i've created a very simple mysql class in c+, but when happen that mysql
This might be quite simple but I have spent hours in Google but couldn't
So I have this simple PHP loop that is generating html table data with
I have a MySQL query like this: SELECT *, SUM(...some SQL removed for brevety)

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.