если выборка идет без LIMIT и WHERE,а база большая то будет медленноАвтор оригинала: Вурдалак
tz-lom
От количества выбираемых данных мало что зависит. Важно сколько данных в таблице.
сомневаюсь,сейчас прогнал тест на таблице из 85 записейАвтор оригинала: c0dex
на where тут будет пофик, все равно будет фуллскан таблицы =р
mysql> SELECT BENCHMARK(10000,(SELECT COUNT(*) FROM `jos_content` ORDER BY RAND()));
+-----------------------------------------------------------------------+
| BENCHMARK(10000,(SELECT COUNT(*) FROM `jos_content` ORDER BY RAND())) |
+-----------------------------------------------------------------------+
| 0 |
+-----------------------------------------------------------------------+
1 row in set (0.30 sec)
mysql> SELECT BENCHMARK(10000,(SELECT COUNT(*) FROM `jos_content` WHERE id=44 OR id=45 ORDER BY RAND()));
+--------------------------------------------------------------------------------------------+
| BENCHMARK(10000,(SELECT COUNT(*) FROM `jos_content` WHERE id=44 OR id=45 ORDER BY RAND())) |
+--------------------------------------------------------------------------------------------+
| 0 |
+--------------------------------------------------------------------------------------------+
1 row in set (0.05 sec)
mysql> explain select * from reference_cities ORDER BY RAND() LIMIT 20;
+----+-------------+------------------+------+---------------+------+---------+------+-------+---------------------------------+
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
+----+-------------+------------------+------+---------------+------+---------+------+-------+---------------------------------+
| 1 | SIMPLE | reference_cities | ALL | NULL | NULL | NULL | NULL | 11372 | Using temporary; Using filesort |
+----+-------------+------------------+------+---------------+------+---------+------+-------+---------------------------------+
1 row in set (0.00 sec)
mysql> explain select * from reference_cities WHERE country_id=3159 OR country_id=2617 ORDER BY RAND() LIMIT 20;
+----+-------------+------------------+-------+---------------+------------+---------+------+------+----------------------------------------------+
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
+----+-------------+------------------+-------+---------------+------------+---------+------+------+----------------------------------------------+
| 1 | SIMPLE | reference_cities | range | country_id | country_id | 4 | NULL | 4094 | Using where; Using temporary; Using filesort |
+----+-------------+------------------+-------+---------------+------------+---------+------+------+----------------------------------------------+
1 row in set (0.00 sec)
mysql> explain select * from reference_cities WHERE country_id=3159 OR country_id=2617 ORDER BY RAND();
+----+-------------+------------------+------+---------------+------+---------+------+-------+----------------------------------------------+
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
+----+-------------+------------------+------+---------------+------+---------+------+-------+----------------------------------------------+
| 1 | SIMPLE | reference_cities | ALL | country_id | NULL | NULL | NULL | 11372 | Using where; Using temporary; Using filesort |
+----+-------------+------------------+------+---------------+------+---------+------+-------+----------------------------------------------+
1 row in set (0.00 sec)
Генеровать надо смещения (offsets) для LIMIT.