Jump to content

MySQL query takes 20 secs when ordered by PK but 0.0006 ordered by timestamp?


Andy-H

Recommended Posts

We have a database of locations, this query executes fine when run on other devices, but this phone number has 65,000 results, and it takes around 20 seconds to execute when ordered by id (which is a primary key so must be indexed, right?)

 

 


Showing rows 0 - 9 (10 total, Query took 20.2472 sec)
SELECT * 
FROM  `newlocations` 
WHERE device =  '077********'
ORDER BY id DESC 
LIMIT 10

 

 

But when ordered by thetimestamp (unix timestamp, also indexed), it takes < 0.001 second

 

 


Showing rows 0 - 9 (10 total, Query took 0.0008 sec)
SELECT * 
FROM  `newlocations` 
WHERE device =  '077********'
ORDER BY thetimestamp DESC 
LIMIT 10

 

 

Anyone got any idea what could be causing this to happen? As I say, with other phone numbers the query executes reasonably quickly

 

 


Showing rows 0 - 9 (10 total, Query took 0.3845 sec)
SELECT * 
FROM  `newlocations` 
WHERE device =  '077********'
ORDER BY id DESC 
LIMIT 10

Link to comment
Share on other sites

There are some cases where mysql can't use indexes for sorting..one that  case is when key in where clause in not same as that of order by clause.

You can prepend your query with  EXPLAIN or EXPLAIN EXTENDED to know whether indexes are used for order by optimization or not .

Link to comment
Share on other sites

Cheers, I've never really understood what this information means and can't find any decent resources to help me figure it out (maybe an opportunity for a PHPFreaks tutorial?), could you shed any light on it for me?

 

 

 

post-67458-13482403545975_thumb.png

Link to comment
Share on other sites

I am no expert at that...

 

You can find more information here

 

http://dev.mysql.com/doc/refman/5.6/en/explain-output.html

 

From the output .... it identified possible keys( latestlocation) to use as index and it is not suitable to filter the rows ..it chosen primary key as index .

 

Can you post the EXPLAIN output for other query where you are ordering by timestamp  ?

 

Link to comment
Share on other sites

It is quite obvious that query optimizer chosen a wrong plan for one of the query .

You can use Force Index or Force Key to make mysql to pick your plan.

 

which image is for which query is still confusing though... you have not named them properly.

Link to comment
Share on other sites

I would suggest putting an INDEX on (`id`, `device`).  I'm assuming you do not currently have an INDEX on `device`?

 

And perhaps you could better normalize your database.  You say that this one device, alone, has ~65K results in a single table?  Why is that?  What's different about each record for that particular device?

 

What is the `latestlocation` index/key made up of?

 

When you hit a CONST as the 'ref', you can *typically* expect fast execution and results.  Your initial query was not able to make that same connection.

 

Could you please run:

 

SHOW INDEXES FROM `newlocations`;

 

Within either phpMyAdmin or from shell, and display the results here.

Link to comment
Share on other sites

We are in the process of re-designing the whole database, I didn't design the current database, no there is no index on ID / device. My boss just asked me if I knew why it was happening then I got interested, I don't think it's a query used anywhere in any application as we normally need locations in chronological order.

post-67458-13482403546131_thumb.png

Link to comment
Share on other sites

The higher the cardinality, the better.  You can see - and use this for future reference - that device has a very low cardinality compared to thetimestamp.

 

People often make the mistake of placing an index on every column thinking that will speed up your system queries, but it can also have a negative impact especially if the optimizer chooses to use that index.

 

When you have a low cardinality, that typically means that you could better normalize your database.  As I asked earlier, having ~65K records of the same device in your table seems quite off.

Link to comment
Share on other sites

There is like 1 repetitive field in there for location type, plus it has input1, input2, input3, input4, input5, input6 and inputi, but as I said, were re-designing the database - the current one was designed before I worked there.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.