?
Solved

Problems with large MySQL table using ORDER BY and LIMIT

Posted on 2010-09-23
3
Medium Priority
?
590 Views
Last Modified: 2013-12-16
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
Comment
Question by:elorc
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
3 Comments
 
LVL 46

Accepted Solution

by:
Kent Olsen earned 2000 total points
ID: 33746540
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
 
LVL 1

Author Closing Comment

by:elorc
ID: 33747852
That makes a ridiculous difference. Thank you.
0
 
LVL 46

Expert Comment

by:Kent Olsen
ID: 33747938
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

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article shows the steps required to install WordPress on Azure. Web Apps, Mobile Apps, API Apps, or Functions, in Azure all these run in an App Service plan. WordPress is no exception and requires an App Service Plan and Database to install
In this blog post, we’ll look at how using thread_statistics can cause high memory usage.
In this video, Percona Solution Engineer Dimitri Vanoverbeke discusses why you want to use at least three nodes in a database cluster. To discuss how Percona Consulting can help with your design and architecture needs for your database and infras…
In this video, Percona Solution Engineer Rick Golba discuss how (and why) you implement high availability in a database environment. To discuss how Percona Consulting can help with your design and architecture needs for your database and infrastr…
Suggested Courses

800 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question