mysql order by rand () optimization method

mysql order by rand () method is applied to redeem the prize optimization projects
<pre>
MySQL> SELECT * from User Order by RAND () limit. 1;
+ + ------- ---------- - + ---------------------------------- + ---------- + - + ----------- + -------------
| the above mentioned id | Phone | password | Salt | the country_code | ip |
+ ------- + - ---------------------------------- + ---------- + ---- + -------------- + ----------- + ------
| 15160 | 6549721306 | e4f302120c006880a247b652ad0e42f2 | 40,343,586 | 86 | 127.0.0.1 |
+ ------- + ------------ + ----------------------------- + ---------- + -------------- + ----- + -----------
1 Row in the SET (0.25 sec)
MySQL> EXPLAIN SELECT * from User Order by RAND () limit. 1;
+ ---- + ------- + ----- + ------------- - + --------------- + ------ + ------ + --------- + -------- + --------------------------------- +
| The above mentioned id | SELECT_TYPE | the Table | of the type | possible_keys | Key | key_len | ref | rows | Extra |
+ ---- + ------- + ------------- + - ----- + ----- + ------ + --------- + ------ + ---- + --------------------------------- + ----
| 1 | SIMPLE | the User | ALL | NULL | NULL | NULL | NULL | 200303 | the Using the Temporary; the Using filesort |
+ ------------- + ---- + ------- + ------ + - -------------- + ------ + ------ + --------- + -------- + - + -------------------------------
. 1 in Row SET (0.00 sec)
</ pre>
Based on the results, operation requires 0.25 seconds, order by rand () requires the use of a temporary table (using temporary), requires the use of sort files (using filesort), inefficient.


Improvement
<pre>
<pre>
? <PHP
// Get the total number of records
$ sqlstr = 'SELECT COUNT (*) AS from the recount User';
$ = Query the mysql_query ($ sqlstr) or Die (mysql_error ());
$ STAT mysql_fetch_assoc = ($ Query);
$ Total = $ STAT [ 'the recount'];

// random offset
$ offset = mt_rand (0, $ total-1);

// After the query offset
$ sqlstr = 'SELECT * from limit User' $ offset ',. 1';..
$ Query = the mysql_query ($ sqlstr) or Die (mysql_error ());
$ Result = mysql_fetch_assoc ($ Query);

print_r($result);
?>
</pre>
<pre>
mysql> select * from user limit 23541,1;
+-------+------------+----------------------------------+----------+--------------+-----------+
| id | phone | password | salt | country_code | ip |
+-------+------------+----------------------------------+----------+--------------+-----------+
| 23542 | 3740507464 | c8bc1890de179538d8a49cc211859a46 | 93863419 | 86 | 127.0.0.1 |
+-------+------------+----------------------------------+----------+--------------+-----------+
1 row in set (0.01 sec)

mysql> explain select * from user limit 23541,1;
+----+-------------+-------+------+---------------+------+---------+------+--------+-------+
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
+----+-------------+-------+------+---------------+------+---------+------+--------+-------+
| 1 | SIMPLE | user | ALL | NULL | NULL | NULL | NULL | 200303 | NULL |
+----+-------------+-------+------+---------------+------+---------+------+--------+-------+
1 row in set (0.00 sec)
</pre>
</pre>

 

Guess you like

Origin www.cnblogs.com/newmiracle/p/11865478.html