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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T05:34:43+00:00 2026-06-12T05:34:43+00:00

This piece of mysql code SELECT id, value, LENGTH(stuffing) FROM t_limit ORDER BY id

  • 0

This piece of mysql code

SELECT  id, value, LENGTH(stuffing)
FROM  t_limit ORDER BY id LIMIT 150000, 10

can be optimized for better performance by rewriting it like this

Note:Table has Index on Id

SELECT  l.id, value, LENGTH(stuffing)
FROM    (
    SELECT  id
    FROM    t_limit
    ORDER BY
            id
    LIMIT 150000, 10
    ) o
JOIN    t_limit l
ON      l.id = o.id
ORDER BY
    l.id

Ref:http://explainextended.com/2009/10/23/mysql-order-by-limit-performance-late-row-lookups/

Now how to optimize this piece of code in a similar way

SELECT  id, value, LENGTH(stuffing)
FROM  t_limit where value>100 ORDER BY id LIMIT 150000, 10
  • 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-12T05:34:44+00:00Added an answer on June 12, 2026 at 5:34 am

    The basic idea behind the optimization presented in the mentioned post is to query only the index pages without touching data pages. If you look at the query plan of the non-optimized query:

    SELECT  id, value, LENGTH(stuffing) AS len
    FROM    t_limit
    ORDER BY
            id
    LIMIT 150000, 10
    

    it would be:

    +----+-------------+---------+------+---------------+------+---------+------+--------+----------------+
    | id | select_type | table   | type | possible_keys | key  | key_len | ref  | rows   | Extra
    +----+-------------+---------+------+---------------+------+---------+------+--------+----------------+
    |  1 | SIMPLE      | t_limit | ALL  | NULL          | NULL | NULL    | NULL | 200000 | Using filesort |
    +----+-------------+---------+------+---------------+------+---------+------+--------+----------------+
    

    So it’s a simple table-scan. With the subquery optimization we receive:

    +----+-------------+------------+--------+-------------------------------+---------+---------+------+--------+---------------------------------+
    | id | select_type | table      | type   | possible_keys                 | key     | key_len | ref  | rows   | Extra                           |
    +----+-------------+------------+--------+-------------------------------+---------+---------+------+--------+---------------------------------+
    |  1 | PRIMARY     | <derived2> | ALL    | NULL                          | NULL    | NULL    | NULL |     10 | Using temporary; Using filesort |
    |  1 | PRIMARY     | l          | eq_ref | PRIMARY                       | PRIMARY | 4       | o.id |      1 |                                 |
    |  2 | DERIVED     | t_limit    | index  | NULL                          | PRIMARY | 4       | NULL | 150010 | Using index                     |
    +----+-------------+------------+--------+-------------------------------+---------+---------+------+--------+---------------------------------+
    

    Look at the key column which shows that the inner most statement used the PRIMARY index. I slightly modified your query so the value type is compatible:

    SELECT  l.id, value, LENGTH(stuffing) AS len
    FROM    (
            SELECT  id
            FROM    t_limit
            where value like 'Value 1%'
            ORDER BY
                    id
            LIMIT 30000, 10
            ) o
    JOIN    t_limit l
    ON      l.id = o.id
    ORDER BY
            l.id
    

    You need to consider what the where condition does. If you placed it in the outer query you would filter only the 10 rows returned from the inner query – I suppose that it was not what you were asking for. Now in the presented case (the where condition in the inner statement) you will end up with a table scan as there is no index that could fulfill your query:

    +----+-------------+------------+--------+---------------+---------+---------+------+--------+--------------------------------+
    | id | select_type | table      | type   | possible_keys | key     | key_len | ref  | rows   | Extra                           |
    +----+-------------+------------+--------+---------------+---------+---------+------+--------+---------------------------------+
    |  1 | PRIMARY     | <derived2> | ALL    | NULL          | NULL    | NULL    | NULL |     10 | Using temporary; Using filesort |
    |  1 | PRIMARY     | l          | eq_ref | PRIMARY       | PRIMARY | 4       | o.id |      1 |                                 |
    |  2 | DERIVED     | t_limit    | ALL    | NULL          | NULL    | NULL    | NULL | 200000 | Using filesort                  |
    +----+-------------+------------+--------+---------------+---------+---------+------+--------+---------------------------------+
    

    To profit from the same optimization as presented in the blog post you would need an additional non-clustered index, eg.

    create index NCIX_t_limit_id_value on t_limit(id, value)
    

    Now, when you run the above query the plan would be:

    +----+-------------+------------+--------+-------------------------------+-----------------------+---------+------+-------+---------------------------------+
    | id | select_type | table      | type   | possible_keys                 | key                   | key_len | ref  | rows  | Extra                           |
    +----+-------------+------------+--------+-------------------------------+-----------------------+---------+------+-------+---------------------------------+
    |  1 | PRIMARY     | <derived2> | ALL    | NULL                          | NULL                  | NULL    | NULL |    10 | Using temporary; Using filesort |
    |  1 | PRIMARY     | l          | eq_ref | PRIMARY,NCIX_t_limit_id_value | PRIMARY               | 4       | o.id |     1 |                                 |
    |  2 | DERIVED     | t_limit    | index  | NULL                          | NCIX_t_limit_id_value | 66      | NULL | 30010 | Using where; Using index        |
    +----+-------------+------------+--------+-------------------------------+-----------------------+---------+------+-------+---------------------------------+
    

    And again we are only scanning index pages.

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

Sidebar

Related Questions

I got this piece of code from Mysql 4th edition to check table existence
I have this piece of code here: $query = mysql_query(SELECT * FROM example WHERE
this piece of code was given in a book. $query=select name, description from widget
I have this piece of php - mysql code: When a user tagged in
This piece of code is from Adobe docs : package { import flash.display.Loader; import
I have this piece of code: Tipo_Id = mysql_real_escape_string($_REQUEST[tipo]); $Sql = SELECT DISTINCT(tabveiculos.Marca_Id), tabmarcas.Marca_Nome
This piece of code compiles and runs as expected on GCC 3.x and 4.x:
This piece of code is supposed to go through a list and preform some
This piece of code gives a syntax error at the colon of elif process.loop(i,
This piece of code used to work in MVC 1 but it does not

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.