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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T11:58:37+00:00 2026-05-16T11:58:37+00:00

Suppose I have the following tables: CREATE TABLE Game ( GameID INT UNSIGNED NOT

  • 0

Suppose I have the following tables:

CREATE TABLE Game (
    GameID INT UNSIGNED NOT NULL,
    GameType TINYINT UNSIGNED NOT NULL,
    PRIMARY KEY (GameID),
    INDEX Index_GameType (GameType, GameID)
) ENGINE=INNODB

CREATE TABLE HighScore (
    Game INT UNSIGNED NOT NULL,
    Score SMALLINT UNSIGNED,
    PRIMARY KEY (Game),
    INDEX Index_Score (Score, Game),
    CONSTRAINT Constr_Score_Game_fk
        FOREIGN KEY Score_Game_fk (Game) REFERENCES Game (GameID)
) ENGINE=INNODB

(These are slimmed-down versions of real tables I am working with. The real tables have more columns and indexes. The above tables capture the essential features of the situation.)

(The number of different GameTypes should be assumed to be small, so that the index Index_GameType is not very selective.)

Suppose I run the following query:

SELECT
    HighScore.Score
FROM
    HighScore
    JOIN Game ON HighScore.Game = Game.GameID
WHERE
    Game.GameType = 42
ORDER BY
    HighScore.Score DESC
LIMIT 50

Looking at this query and the table design, we can probably agree that the sensible thing would be to scan down the HighScore table and join rows until 50 have been found for which the WHERE condition is satisfied. However, an EXPLAIN for me showed (using my real, more complicated tables) that MySQL actually planned to look for all rows in Game satisfying the WHERE condition, join this with HighScore, and do a filesort to get the rows in sorted order.

It seemed sensible therefore to specify a STRAIGHT_JOIN instead in the above query. Now the EXPLAIN output indicates that the first table, HighScore, is “using index” (as expected), but the number of rows reported appears to be the number of rows in the HighScore table. Should I take this to mean that MySQL plans to take essentially the entire index, join every row in that index to the other table, and only then throw away rows below the top 50? That seems ridiculous, but I’m not sure if that’s what it would actually do. Does anyone have any idea?

  • 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-16T11:58:38+00:00Added an answer on May 16, 2026 at 11:58 am

    Since the fields your are filtering and ordering on are in different tables, they cannot be covered by a single index.

    If you add a STRAIGHT_JOIN clause, you force MySQL to take every record from HighScore (using index on Score), find the matching record in Game, check if it’s 42 and return (or neglect) it.

    Since MySQL cannot tell in advance how many records will match, it will assume the worst and just show the total number of HighScore records in the plan.

    In reality, the query will stop after 50 mathing records will be returned.

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

Sidebar

Related Questions

Suppose I have the following tables CREATE TABLE plugins ( id int primary key,
I have the following tables; CREATE TABLE IF NOT EXISTS `tags` ( `tag_id` int(11)
Suppose I have the following table: CREATE TABLE EXAMPLETABLE ( ID NUMBER(10,0) NOT NULL,
Suppose we have 2 tables, a and b: CREATE TABLE a (id_a INT NOT
suppose I have the following tables and relations: CREATE TABLE [dbo].[Customers] ( [CustomerID] [int]
Suppose that we have following tables create table Employee( 2 EMPNO NUMBER(3), 3 ENAME
Suppose I have a table like CREATE TABLE associacao ( id bigserial NOT NULL,
Suppose I have the following table definition: CREATE TABLE x (i serial primary key,
suppose i have the following table CREATE TABLE #ResultTable (NettAmount money, GrossAmount money,TotalVat money)
Let's suppose I have the following table (let's call it my_table ): CREATE TABLE

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.