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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T03:35:26+00:00 2026-05-29T03:35:26+00:00

I am running the following query: SELECT MyField, COUNT(*) AS MyCount FROM MyTable NATURAL

  • 0

I am running the following query:

SELECT 
        MyField,
        COUNT(*) AS MyCount
    FROM
        MyTable
    NATURAL JOIN
        AnotherTable
    WHERE
        Timestamp >= 1000 AND Timestamp <= 10000
    GROUP BY
        MyField
    ORDER BY
        MyCount DESC;

This runs fine and takes about 6 seconds to complete. If I want to limit the result to show only the 20 highest MyCounts, I add LIMIT 20 on to the end of the query. Suddenly it takes 6 minutes to complete!

The EXPLAIN output for the original query:

+----+-------------+-------------+--------+---------------------------+---------+---------+---------------------------+---------+----------------------------------------------+
| id | select_type | table       | type   | possible_keys             | key     | key_len | ref                       | rows    | Extra                                        |
+----+-------------+-------------+--------+---------------------------+---------+---------+---------------------------+---------+----------------------------------------------+
|  1 | SIMPLE      | MyTable     | ALL    | mytable_fkey              | NULL    | NULL    | NULL                      | 6858209 | Using where; Using temporary; Using filesort |
|  1 | SIMPLE      | AnotherTable| eq_ref | PRIMARY                   | PRIMARY | 4       | test.MyTable.FKeyID       |       1 | Using index                                  |
+----+-------------+-------------+--------+---------------------------+---------+---------+---------------------------+---------+----------------------------------------------+

The EXPLAIN output for the query with LIMIT 20:

+----+-------------+-------------+--------+---------------------------+-------------------------+---------+---------------------------+---------+----------------------------------------------+
| id | select_type | table       | type   | possible_keys             | key                     | key_len | ref                       | rows    | Extra                                        |
+----+-------------+-------------+--------+---------------------------+-------------------------+---------+---------------------------+---------+----------------------------------------------+
|  1 | SIMPLE      | MyTable     | index  | mytable_fkey              | myfield_timestamp_index | 771     | NULL                      | 6858209 | Using where; Using temporary; Using filesort |
|  1 | SIMPLE      | AnotherTable| eq_ref | PRIMARY                   | PRIMARY                 | 4       | test.MyTable.FKeyID       |       1 | Using index                                  |
+----+-------------+-------------+--------+---------------------------+-------------------------+---------+---------------------------+---------+----------------------------------------------+

What is the explanation for this? Is there a better way I can limit the number of rows?

  • 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-29T03:35:26+00:00Added an answer on May 29, 2026 at 3:35 am

    If you see Using temporary; Using filesort in your EXPLAIN output, you are probably missing a suitable index and you’re getting killed because of it.

    Make sure your JOIN and GROUP BY fields are both available in the same index.

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

Sidebar

Related Questions

I am running following query.. Select T1.* from T1 LEFT JOIN T2 ON T1.C1
I'm running the following query: SELECT * FROM all_tab_cols c LEFT JOIN all_varrays v
I'm currently running the following query: SELECT * from people WHERE id = 4;
I am running following query. SELECT T1.C1, T2.C2..., IF( T1.C1<>T2.C1,Changed,1) AS NewColumn From T1
Using phpMyAdmin and running the following query I get 19 registers: SELECT * FROM
I am running into some difficulty with the following query. SELECT maker, speed FROM
I am running the following SQL query: SELECT * FROM cms_albums WHERE id IN
I am running the following MySQL query : select * from combinations where family_type='f597';
I have the following query: SELECT wm_concat(DISTINCT NAME) as Methods FROM TPM_TRAININGPLAN JOIN TPM_DELIVERYMETHODS
I have the following query running against a mysql database: select value from fact_data

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.