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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T04:38:17+00:00 2026-06-14T04:38:17+00:00

This is the table I have: CREATE TABLE `person` ( `id` bigint(10) NOT NULL

  • 0

This is the table I have:

CREATE TABLE `person` (
  `id` bigint(10) NOT NULL AUTO_INCREMENT,
  `name` varchar(20) DEFAULT NULL,
  `age` int(10) NOT NULL DEFAULT '0',
  PRIMARY KEY (`id`),
  KEY `age` (`age`)
) ENGINE=InnoDB AUTO_INCREMENT=10000 DEFAULT CHARSET=latin1;

This is the output of explain:

mysql> explain select * from person order by age\G
*************************** 1. row ***************************
           id: 1
  select_type: SIMPLE
        table: person
         type: ALL
possible_keys: NULL
          key: NULL
      key_len: NULL
          ref: NULL
         rows: 10367
        Extra: Using filesort
1 row in set (0.00 sec)

What’s going on? Why isn’t MySQL using the age index to do the sorting? I tried doind analyze table, but it didn’t make any difference.

Just for reference, here’s the distribution of data in the table:

mysql> select age, count(*) from person group by age;
+-----+----------+
| age | count(*) |
+-----+----------+
|  21 |     1250 |
|  22 |     1216 |
|  23 |     1278 |
|  24 |     1262 |
|  25 |     1263 |
|  26 |     1221 |
|  27 |     1239 |
|  28 |     1270 |
+-----+----------+
8 rows in set (0.04 sec)

UPDATE

@grisha seems to think that you can’t select a field not in the index. That doesn’t seem to make any sense, however, it looks like the following works:

mysql> explain select age from person order by age \G
*************************** 1. row ***************************
           id: 1
  select_type: SIMPLE
        table: person
         type: index
possible_keys: NULL
          key: age
      key_len: 4
          ref: NULL
         rows: 10367
        Extra: Using index
1 row in set (0.00 sec)

And also if I add an index that covers all the field it works as well:

mysql> alter table person add key `idx1` (`age`, `id`, `name`);
Query OK, 0 rows affected (0.29 sec)
Records: 0  Duplicates: 0  Warnings: 0

mysql> explain select * from person order by age\G
*************************** 1. row ***************************
           id: 1
  select_type: SIMPLE
        table: person
         type: index
possible_keys: NULL
          key: idx1
      key_len: 35
          ref: NULL
         rows: 10367
        Extra: Using index
1 row in set (0.00 sec)

@eggyal suggested using index hints. This seems to work as well, and is probably the correct answer:

mysql> explain select * from person force key for order by (age) order by age\G
*************************** 1. row ***************************
           id: 1
  select_type: SIMPLE
        table: person
         type: index
possible_keys: NULL
          key: age
      key_len: 4
          ref: NULL
         rows: 10367
        Extra: 
1 row in set (0.02 sec)
  • 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-14T04:38:19+00:00Added an answer on June 14, 2026 at 4:38 am

    Index can help you in sorting, when you select only index column. In your case you select *, therefore mysql doesn’t use index.

    Why usually index can’t help in sorting ?

    If we want to sort some table t by field my_field using index on my_field, we will do :

    for each my_field f in index, do :
        get all records where my_field = f and add to result
    return result
    

    Assuming not clustered index, the above will execute as many random I/O’s as the number of rows in t(might be huge), whereas simple external sorting algorithm will read the data by blocks/pages sequentially and will execute much less random I/O’s.

    So, of course you can say to db : “I want to do sorting using index”, but it’s really not efficient.

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

Sidebar

Related Questions

i have this table create table eveniment( + evenimentId bigint not null auto_increment primary
I have this table CREATE TABLE `codes` ( `id` int(11) unsigned NOT NULL AUTO_INCREMENT,
I have this table: CREATE TABLE `test` ( `ID` int(11) NOT NULL auto_increment, `text`
I have this table: CREATE TABLE `point` ( `id` INT(11) NOT NULL AUTO_INCREMENT, `siteid`
I have this table CREATE TABLE [dbo].[friend_blocked_list]( [subdomain] [varchar](50) NOT NULL, [un] [nvarchar](50) NOT
I have a table like this: CREATE TABLE book_info ( book_id VARCHAR(32) not null,
I have created table using this command successfully create table Person( first_name varchar(25) not
I have this table: CREATE TABLE IF NOT EXISTS `produtos` ( `id` int(11) NOT
For example, say I have this SQL Script: create table Person ( id int
I have a table like this: create table t1 { person_id int, item_name varchar(30),

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.