Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 600
  • Last Modified:

Problems with large MySQL table using ORDER BY and LIMIT

I have a MySQL table that is pretty huge. There are 21 columns and (at this point) about 3 million rows. The interface I'm writing in C# to display data from this table will obviously need the ability to break results into pages. The way I was going to do this was using LIMIT. All results would be sorted by date. So the query for the first page would look like this:

   SELECT * FROM BigTable ORDER BY ItemDate ASC LIMIT 0,250;

This takes forever though, because before it applies the limit it goes through and orders the entire table by the date field. So I'm not really sure how to speed this up and make it work efficiently. Any recommendations?
0
elorc
Asked:
elorc
  • 2
1 Solution
 
Kent OlsenData Warehouse Architect / DBACommented:
Hi elorc,

There are a couple of ways.  :)

Is there an Identity column on the table?  That's an easy solution.

Is the ItemDate column indexed?  Assuming so, try the code below.


Kent

SELECT *
FROM
(
  SELECT ItemDate 
  FROM BigTable 
  ORDER BY ItemDate ASC 
  LIMIT 0,250
) t0
LEFT JOIN BigTable
  ON t0.ItemDate = BigTable.ItemDate;

Open in new window

0
 
elorcAuthor Commented:
That makes a ridiculous difference. Thank you.
0
 
Kent OlsenData Warehouse Architect / DBACommented:
Hi elorc,

Just so you know how this works, MySQL recognizes that there is an index on the ItemDate column and grabs the first 250 dates from the index instead of reading the table.

It then joins only those 250 rows from the table.



Kent
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now