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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T04:51:06+00:00 2026-06-15T04:51:06+00:00

Say I have an Order table that has 100+ columns and 1 million rows.

  • 0

Say I have an Order table that has 100+ columns and 1 million rows. It has a PK on OrderID and FK constraint StoreID –> Store.StoreID.

1) select * from 'Order' order by OrderID desc limit 10;

the above takes a few milliseconds.

2) select * from 'Order' o join 'Store' s on s.StoreID = o.StoreID order by OrderID desc limit 10;

this somehow can take up to many seconds. The more inner joins I add, slows it down further more.

3) select OrderID, column1 from 'Order' o join 'Store' s on s.StoreID = o.StoreID order by OrderID desc limit 10;

this seems to speed the execution up, by limiting the columns we select.

There are a few points that I dont understand here and would really appreciate it if anyone more knowledgeable with mysql (or rmdb query execution in general) can enlighten me.

Query 1 is fast since it’s just a reverse lookup by PK and DB only needs to return the first 10 rows it encountered.

I don’t see why Query 2 should take for ever. Shouldn’t the operation be the same? i.e. get the first 10 rows by PK and then join with other tables. Since there’s a FK constraint, it is guaranteed that the relationship will be satisfied. So DB doesn’t need to join more rows than necessary and then trim the result, right? Unless, FK constraint allows null FK? In which case I guess a left join would make this much faster than an inner join?

Lastly, I’m guess query 3 is simply faster because less columns are used in those unnecessary joins? But why would the query execution need the other columns while joining? Shouldn’t it just join using PKs first, and then get the columns for just the 10 rows?

Thanks!

  • 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-15T04:51:07+00:00Added an answer on June 15, 2026 at 4:51 am

    My understanding is that the mysql engine applies limit after any join‘s happen.

    From http://dev.mysql.com/doc/refman/5.0/en/select.html, The HAVING clause is applied nearly last, just before items are sent to the client, with no optimization. (LIMIT is applied after HAVING.)

    EDIT: You could try using this query to take advantage of the PK speed.

    select * from (select * from 'Order' order by OrderID desc limit 10) o
    join 'Store' s on s.StoreID = o.StoreID;

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

Sidebar

Related Questions

Let's say I have an Order table which has a FirstSalesPersonId field and a
I have application that has to check multiple data from user table, in order
Let's say I have players table. It consists with 3 rows(it has much more,
Lets say i have a table that has the following field names 'id' 'likes'
Let's say we have a Product table, and Order table and a (junction table)
Say you have a user table and an order table which references user (user
I have a table that has years 1990 - 2011 in it. If i
Let's say I have a table with the following columns: p_id userid points Let's
I have a table, call it MyTable which has a column, say myvalue. I
Let's say I have a simple table like that: unique ID | url |

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.