自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

MySQL優(yōu)化案例系列 — RAND()優(yōu)化

數(shù)據(jù)庫 MySQL
眾所周知,在MySQL中,如果直接 ORDER BY RAND() 的話,效率非常差,因?yàn)闀?huì)多次執(zhí)行。事實(shí)上,如果等值查詢也是用 RAND() 的話也如此,我們先來看看下面這幾個(gè)SQL的不同執(zhí)行計(jì)劃和執(zhí)行耗時(shí)。

   眾所周知,在MySQL中,如果直接 ORDER BY RAND() 的話,效率非常差,因?yàn)闀?huì)多次執(zhí)行。事實(shí)上,如果等值查詢也是用 RAND() 的話也如此,我們先來看看下面這幾個(gè)SQL的不同執(zhí)行計(jì)劃和執(zhí)行耗時(shí)。

  首先,看下建表DDL,這是一個(gè)沒有顯式自增主鍵的InnoDB表:

  1. [yejr@imysql]> show create table t_innodb_random\G 
  2. *************************** 1. row *************************** 
  3. Table: t_innodb_random 
  4. Create TableCREATE TABLE `t_innodb_random` ( 
  5. `id` int(10) unsigned NOT NULL
  6. `uservarchar(64) NOT NULL DEFAULT ''
  7. KEY `idx_id` (`id`) 
  8. ) ENGINE=InnoDB DEFAULT CHARSET=latin1 

 

  往這個(gè)表里灌入一些測(cè)試數(shù)據(jù),至少10萬以上, id 字段也是亂序的。

 

  1. [yejr@imysql]> select count(*) from t_innodb_random\G 
  2. *************************** 1. row *************************** 
  3. count(*): 393216 

 

  1、常量等值檢索:

  1. [yejr@imysql]> explain select id from t_innodb_random where id = 13412\G 
  2. *************************** 1. row *************************** 
  3. id: 1 
  4. select_type: SIMPLE 
  5. table: t_innodb_random 
  6. type: ref 
  7. possible_keys: idx_id 
  8. key: idx_id 
  9. key_len: 4 
  10. ref: const 
  11. rows: 1 
  12. Extra: Using index 
  13.  
  14. [yejr@imysql]> select id from t_innodb_random where id = 13412; 
  15. 1 row in set (0.00 sec) 

 

  可以看到執(zhí)行計(jì)劃很不錯(cuò),是常量等值查詢,速度非???。

  2、使用RAND()函數(shù)乘以常量,求得隨機(jī)數(shù)后檢索:

 

  1. [yejr@imysql]> explain select id from t_innodb_random where id = round(rand()*13241324)\G 
  2. *************************** 1. row *************************** 
  3. id: 1 
  4. select_type: SIMPLE 
  5. table: t_innodb_random 
  6. type: index 
  7. possible_keys: NULL 
  8. key: idx_id 
  9. key_len: 4 
  10. ref: NULL 
  11. rows: 393345 
  12. Extra: Using where; Using index 
  13.  
  14. [yejr@imysql]> select id from t_innodb_random where id = round(rand()*13241324)\G 
  15. Empty set (0.26 sec) 

 

  可以看到執(zhí)行計(jì)劃很糟糕,雖然是只掃描索引,但是做了全索引掃描,效率非常差。因?yàn)閃HERE條件中包含了RAND(),使得MySQL把它當(dāng)做變量來處理,無法用常量等值的方式查詢,效率很低。

  我們把常量改成取t_innodb_random表的***id值,再乘以RAND()求得隨機(jī)數(shù)后檢索看看什么情況:

 

  1. [yejr@imysql]> explain select id from t_innodb_random where id = round(rand()*(select max(id) from t_innodb_random))\G 
  2. *************************** 1. row *************************** 
  3. id: 1 
  4. select_type: PRIMARY 
  5. table: t_innodb_random 
  6. type: index 
  7. possible_keys: NULL 
  8. key: idx_id 
  9. key_len: 4 
  10. ref: NULL 
  11. rows: 393345 
  12. Extra: Using where; Using index 
  13. *************************** 2. row *************************** 
  14. id: 2 
  15. select_type: SUBQUERY 
  16. tableNULL 
  17. type: NULL 
  18. possible_keys: NULL 
  19. keyNULL 
  20. key_len: NULL 
  21. ref: NULL 
  22. rowsNULL 
  23. Extra: Select tables optimized away 
  24.  
  25. [yejr@imysql]> select id from t_innodb_random where id = round(rand()*(select max(id) from t_innodb_random))\G 
  26. Empty set (0.27 sec) 

 

  可以看到,執(zhí)行計(jì)劃依然是全索引掃描,執(zhí)行耗時(shí)也基本相當(dāng)。

  3、改造成普通子查詢模式 ,這里有兩次子查詢

 

  1. [yejr@imysql]> explain select id from t_innodb_random where id = (select round(rand()*(select max(id) from t_innodb_random)) as nid)\G 
  2. *************************** 1. row *************************** 
  3. id: 1 
  4. select_type: PRIMARY 
  5. table: t_innodb_random 
  6. type: index 
  7. possible_keys: NULL 
  8. key: idx_id 
  9. key_len: 4 
  10. ref: NULL 
  11. rows: 393345 
  12. Extra: Using where; Using index 
  13. *************************** 2. row *************************** 
  14. id: 3 
  15. select_type: SUBQUERY 
  16. tableNULL 
  17. type: NULL 
  18. possible_keys: NULL 
  19. keyNULL 
  20. key_len: NULL 
  21. ref: NULL 
  22. rowsNULL 
  23. Extra: Select tables optimized away 
  24.  
  25. [yejr@imysql]> select id from t_innodb_random where id = (select round(rand()*(select max(id) from t_innodb_random)) as nid)\G 
  26. Empty set (0.27 sec) 

 

  可以看到,執(zhí)行計(jì)劃也不好,執(zhí)行耗時(shí)較慢。

  4、改造成JOIN關(guān)聯(lián)查詢,不過***值還是用常量表示

 

  1. [yejr@imysql]> explain select id from t_innodb_random t1 join (select round(rand()*13241324) as id2) as t2 where t1.id = t2.id2\G 
  2. *************************** 1. row *************************** 
  3. id: 1 
  4. select_type: PRIMARY 
  5. table: <derived2> 
  6. type: system 
  7. possible_keys: NULL 
  8. keyNULL 
  9. key_len: NULL 
  10. ref: NULL 
  11. rows: 1 
  12. Extra: 
  13. *************************** 2. row *************************** 
  14. id: 1 
  15. select_type: PRIMARY 
  16. table: t1 
  17. type: ref 
  18. possible_keys: idx_id 
  19. key: idx_id 
  20. key_len: 4 
  21. ref: const 
  22. rows: 1 
  23. Extra: Using where; Using index 
  24. *************************** 3. row *************************** 
  25. id: 2 
  26. select_type: DERIVED 
  27. tableNULL 
  28. type: NULL 
  29. possible_keys: NULL 
  30. keyNULL 
  31. key_len: NULL 
  32. ref: NULL 
  33. rowsNULL 
  34. Extra: No tables used 
  35.  
  36. [yejr@imysql]> select id from t_innodb_random t1 join (select round(rand()*13241324) as id2) as t2 where t1.id = t2.id2\G 
  37. Empty set (0.00 sec) 

 

  這時(shí)候執(zhí)行計(jì)劃就非常***了,和最開始的常量等值查詢是一樣的了,執(zhí)行耗時(shí)也非常之快。

  這種方法雖然很好,但是有可能查詢不到記錄,改造范圍查找,但結(jié)果LIMIT 1就可以了:

 

  1. [yejr@imysql]> explain select id from t_innodb_random where id > (select round(rand()*(select max(id) from t_innodb_random)) as nid) limit 1\G 
  2. *************************** 1. row *************************** 
  3. id: 1 
  4. select_type: PRIMARY 
  5. table: t_innodb_random 
  6. type: index 
  7. possible_keys: NULL 
  8. key: idx_id 
  9. key_len: 4 
  10. ref: NULL 
  11. rows: 393345 
  12. Extra: Using where; Using index 
  13. *************************** 2. row *************************** 
  14. id: 3 
  15. select_type: SUBQUERY 
  16. tableNULL 
  17. type: NULL 
  18. possible_keys: NULL 
  19. keyNULL 
  20. key_len: NULL 
  21. ref: NULL 
  22. rowsNULL 
  23. Extra: Select tables optimized away 
  24.  
  25. [yejr@imysql]> select id from t_innodb_random where id > (select round(rand()*(select max(id) from t_innodb_random)) as nid) limit 1\G 
  26. *************************** 1. row *************************** 
  27. id: 1301 
  28. 1 row in set (0.00 sec) 

 

  可以看到,雖然執(zhí)行計(jì)劃也是全索引掃描,但是因?yàn)橛辛薒IMIT 1,只需要找到一條記錄,即可終止掃描,所以效率還是很快的。

  小結(jié):

  從數(shù)據(jù)庫中隨機(jī)取一條記錄時(shí),可以把RAND()生成隨機(jī)數(shù)放在JOIN子查詢中以提高效率。

  5、再來看看用ORDRR BY RAND()方式一次取得多個(gè)隨機(jī)值的方式:

 

  1. [yejr@imysql]> explain select id from t_innodb_random order by rand() limit 1000\G 
  2. *************************** 1. row *************************** 
  3. id: 1 
  4. select_type: SIMPLE 
  5. table: t_innodb_random 
  6. type: index 
  7. possible_keys: NULL 
  8. key: idx_id 
  9. key_len: 4 
  10. ref: NULL 
  11. rows: 393345 
  12. Extra: Using index; Using temporary; Using filesort 
  13.  
  14. [yejr@imysql]> select id from t_innodb_random order by rand() limit 1000; 
  15. 1000 rows in set (0.41 sec) 

 

  全索引掃描,生成排序臨時(shí)表,太差太慢了。

  6、把隨機(jī)數(shù)放在子查詢里看看:

 

  1. [yejr@imysql]> explain select id from t_innodb_random where id > (select rand() * (select max(id) from t_innodb_random) as nid) limit 1000\G 
  2. *************************** 1. row *************************** 
  3. id: 1 
  4. select_type: PRIMARY 
  5. table: t_innodb_random 
  6. type: index 
  7. possible_keys: NULL 
  8. key: idx_id 
  9. key_len: 4 
  10. ref: NULL 
  11. rows: 393345 
  12. Extra: Using where; Using index 
  13. *************************** 2. row *************************** 
  14. id: 3 
  15. select_type: SUBQUERY 
  16. tableNULL 
  17. type: NULL 
  18. possible_keys: NULL 
  19. keyNULL 
  20. key_len: NULL 
  21. ref: NULL 
  22. rowsNULL 
  23. Extra: Select tables optimized away 
  24.  
  25. [yejr@imysql]> select id from t_innodb_random where id > (select rand() * (select max(id) from t_innodb_random) as nid) limit 1000\G 
  26. 1000 rows in set (0.04 sec) 

 

  嗯,提速了不少,這個(gè)看起來還不賴:)

  7、仿照上面的方法,改成JOIN和隨機(jī)數(shù)子查詢關(guān)聯(lián)

 

  1. [yejr@imysql]> explain select id from t_innodb_random t1 join (select rand() * (select max(id) from t_innodb_random) as nid) t2 on t1.id > t2.nid limit 1000\G 
  2. *************************** 1. row *************************** 
  3. id: 1 
  4. select_type: PRIMARY 
  5. table: <derived2> 
  6. type: system 
  7. possible_keys: NULL 
  8. keyNULL 
  9. key_len: NULL 
  10. ref: NULL 
  11. rows: 1 
  12. Extra: 
  13. *************************** 2. row *************************** 
  14. id: 1 
  15. select_type: PRIMARY 
  16. table: t1 
  17. type: range 
  18. possible_keys: idx_id 
  19. key: idx_id 
  20. key_len: 4 
  21. ref: NULL 
  22. rows: 196672 
  23. Extra: Using where; Using index 
  24. *************************** 3. row *************************** 
  25. id: 2 
  26. select_type: DERIVED 
  27. tableNULL 
  28. type: NULL 
  29. possible_keys: NULL 
  30. keyNULL 
  31. key_len: NULL 
  32. ref: NULL 
  33. rowsNULL 
  34. Extra: No tables used 
  35. *************************** 4. row *************************** 
  36. id: 3 
  37. select_type: SUBQUERY 
  38. tableNULL 
  39. type: NULL 
  40. possible_keys: NULL 
  41. keyNULL 
  42. key_len: NULL 
  43. ref: NULL 
  44. rowsNULL 
  45. Extra: Select tables optimized away 
  46.  
  47. [yejr@imysql]> select id from t_innodb_random t1 join (select rand() * (select max(id) from t_innodb_random) as nid) t2 on t1.id > t2.nid limit 1000\G 
  48. 1000 rows in set (0.00 sec) 

 

  可以看到,全索引檢索,發(fā)現(xiàn)符合記錄的條件后,直接取得1000行,這個(gè)方法是最快的。

  綜上,想從MySQL數(shù)據(jù)庫中隨機(jī)取一條或者N條記錄時(shí),***把RAND()生成隨機(jī)數(shù)放在JOIN子查詢中以提高效率。

  上面說了那么多的廢話,***簡(jiǎn)單說下,就是把下面這個(gè)SQL:

 

  1. SELECT id FROM table ORDER BY RAND() LIMIT n; 

  改造成下面這個(gè):

 

  1. SELECT id FROM table t1 JOIN (SELECT RAND() * (SELECT MAX(id) FROM tableAS nid) t2 ON t1.id > t2.nid LIMIT n; 

  如果想要達(dá)到完全隨機(jī),還可以改成下面這種寫法:

 

  1. SELECT id FROM table t1 JOIN (SELECT round(RAND() * (SELECT MAX(id) FROM table)) AS nid FROM table LIMIT n) t2 ON t1.id = t2.nid; 

  就可以享受在SQL中直接取得隨機(jī)數(shù)了,不用再在程序中構(gòu)造一串隨機(jī)數(shù)去檢索了。

責(zé)任編輯:honglu 來源: MySQL中文網(wǎng)
相關(guān)推薦

2010-06-12 15:31:04

MySQL查詢優(yōu)化

2020-10-19 19:45:58

MySQL數(shù)據(jù)庫優(yōu)化

2019-07-25 13:22:43

AndroidAPK文件優(yōu)化

2013-12-17 16:21:17

iOSiOS性能優(yōu)化

2023-10-09 07:42:49

PawSQL數(shù)據(jù)庫管理

2023-10-16 07:49:25

PawSQL數(shù)據(jù)庫

2016-12-20 18:26:51

Mysql優(yōu)化

2022-03-29 13:27:22

Android優(yōu)化APP

2022-07-19 16:47:53

Android抖音

2018-06-07 08:54:01

MySQL性能優(yōu)化索引

2023-12-11 06:27:39

MySQL線上業(yè)務(wù)優(yōu)化后臺(tái)上傳文件

2012-07-18 10:47:49

Java

2021-11-09 09:57:46

Webpack 前端分包優(yōu)化

2024-10-09 23:32:50

2022-04-28 15:07:41

抖音內(nèi)存泄漏Android

2020-01-16 18:30:07

技術(shù)SQL優(yōu)化

2017-05-08 17:40:23

Oracle視圖優(yōu)化案例分析

2020-03-23 15:15:57

MySQL性能優(yōu)化數(shù)據(jù)庫

2010-03-02 09:53:14

MySQL性能優(yōu)化

2009-04-20 08:51:50

MySQL查詢優(yōu)化數(shù)據(jù)庫
點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號(hào)