The Cache: Technology Expert's Forum
 
*
Welcome, Guest. Please login or register. February 12, 2012, 07:57:14 PM

Login with username, password and session length


Pages: [1]
  Print  
Author Topic: rand() faster than "order by" datetime, even faster without index (mysql)  (Read 1306 times)
walrus
Rookie
**
Offline Offline

Posts: 46


View Profile
« on: January 03, 2010, 05:14:33 AM »

Code:
+------------------+---------------------+------+-----+---------+----------------+
| Field            | Type                | Null | Key | Default | Extra          |
+------------------+---------------------+------+-----+---------+----------------+
| id               | bigint(20) unsigned | NO   | PRI | NULL    | auto_increment |
| object           | varchar(1000)       | NO   | UNI | NULL    |                |
| should_use       | int(10) unsigned    | NO   | MUL | 0       |                |
| last_used        | datetime            | NO   | MUL | NULL    |                |
+------------------+---------------------+------+-----+---------+----------------+


select count(*)
93029 results

select count(*) where should_use=1
7970 results

Indexes on id(primary), should_use, last_used.


Look at the following queries, shouldn't the rand() query take longer than the other?

select id from table where should_use=1 order by last_used limit 1
0.17 sec


select id from table where should_use=1 order by rand() limit 1
0.04 sec


If I *DROP* the index on last_used, I get this:

select id from table where should_use=1 order by last_used limit 1
0.04 sec


I don't get it. It's all backwards.
Logged
walrus
Rookie
**
Offline Offline

Posts: 46


View Profile
« Reply #1 on: January 03, 2010, 05:33:29 AM »

New compound index on both (should_use, last_used) solved it.. phew  Idea...
Logged
Pages: [1]
  Print  
 
Jump to:  

Perkiset's Place Home   Best of The Cache   phpMyIDE: MySQL Stored Procedures, Functions & Triggers
Politics @ Perkiset's   Pinkhat's Perspective   
cache
mart
coder
programmers
ajax
php
javascript
Powered by MySQL Powered by PHP Powered by SMF 1.1.2 | SMF © 2006-2007, Simple Machines LLC
Seo4Smf v0.2 © Webmaster's Talks


Valid XHTML 1.0! Valid CSS!