最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

MySQL優(yōu)化之對RAND()的優(yōu)化方法

 更新時間:2014年07月22日 09:52:10   投稿:junjie  
這篇文章主要介紹了MySQL優(yōu)化之對RAND()的優(yōu)化方法,本文詳細分析了Mysql中對RAND()的幾種優(yōu)化方法,并最終得出一個結(jié)論,需要的朋友可以參考下

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

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

復(fù)制代碼 代碼如下:

[yejr@imysql]> show create table t_innodb_random\G
*************************** 1. row ***************************
Table: t_innodb_random
Create Table: CREATE TABLE `t_innodb_random` (
`id` int(10) unsigned NOT NULL,
`user` varchar(64) NOT NULL DEFAULT '',
KEY `idx_id` (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1

往這個表里灌入一些測試數(shù)據(jù),至少10萬以上, id 字段也是亂序的。
復(fù)制代碼 代碼如下:

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

1、常量等值檢索:

復(fù)制代碼 代碼如下:

[yejr@imysql]> explain select id from t_innodb_random where id = 13412\G
*************************** 1. row ***************************
id: 1
select_type: SIMPLE
table: t_innodb_random
type: ref
possible_keys: idx_id
key: idx_id
key_len: 4
ref: const
rows: 1
Extra: Using index

[yejr@imysql]> select id from t_innodb_random where id = 13412;
1 row in set (0.00 sec)

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

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

復(fù)制代碼 代碼如下:

[yejr@imysql]> explain select id from t_innodb_random where id = round(rand()*13241324)\G
*************************** 1. row ***************************
id: 1
select_type: SIMPLE
table: t_innodb_random
type: index
possible_keys: NULL
key: idx_id
key_len: 4
ref: NULL
rows: 393345
Extra: Using where; Using index

[yejr@imysql]> select id from t_innodb_random where id = round(rand()*13241324)\G
Empty set (0.26 sec)

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

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

復(fù)制代碼 代碼如下:

[yejr@imysql]> explain select id from t_innodb_random where id = round(rand()*(select max(id) from t_innodb_random))\G
*************************** 1. row ***************************
id: 1
select_type: PRIMARY
table: t_innodb_random
type: index
possible_keys: NULL
key: idx_id
key_len: 4
ref: NULL
rows: 393345
Extra: Using where; Using index
*************************** 2. row ***************************
id: 2
select_type: SUBQUERY
table: NULL
type: NULL
possible_keys: NULL
key: NULL
key_len: NULL
ref: NULL
rows: NULL
Extra: Select tables optimized away

[yejr@imysql]> select id from t_innodb_random where id = round(rand()*(select max(id) from t_innodb_random))\G
Empty set (0.27 sec)

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

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

復(fù)制代碼 代碼如下:

[yejr@imysql]> explain select id from t_innodb_random where id = (select round(rand()*(select max(id) from t_innodb_random)) as nid)\G
*************************** 1. row ***************************
id: 1
select_type: PRIMARY
table: t_innodb_random
type: index
possible_keys: NULL
key: idx_id
key_len: 4
ref: NULL
rows: 393345
Extra: Using where; Using index
*************************** 2. row ***************************
id: 3
select_type: SUBQUERY
table: NULL
type: NULL
possible_keys: NULL
key: NULL
key_len: NULL
ref: NULL
rows: NULL
Extra: Select tables optimized away

[yejr@imysql]> select id from t_innodb_random where id = (select round(rand()*(select max(id) from t_innodb_random)) as nid)\G
Empty set (0.27 sec)


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

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

復(fù)制代碼 代碼如下:

[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
*************************** 1. row ***************************
id: 1
select_type: PRIMARY
table: <derived2>
type: system
possible_keys: NULL
key: NULL
key_len: NULL
ref: NULL
rows: 1
Extra:
*************************** 2. row ***************************
id: 1
select_type: PRIMARY
table: t1
type: ref
possible_keys: idx_id
key: idx_id
key_len: 4
ref: const
rows: 1
Extra: Using where; Using index
*************************** 3. row ***************************
id: 2
select_type: DERIVED
table: NULL
type: NULL
possible_keys: NULL
key: NULL
key_len: NULL
ref: NULL
rows: NULL
Extra: No tables used

[yejr@imysql]> select id from t_innodb_random t1 join (select round(rand()*13241324) as id2) as t2 where t1.id = t2.id2\G
Empty set (0.00 sec)


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

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

復(fù)制代碼 代碼如下:

[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
*************************** 1. row ***************************
id: 1
select_type: PRIMARY
table: t_innodb_random
type: index
possible_keys: NULL
key: idx_id
key_len: 4
ref: NULL
rows: 393345
Extra: Using where; Using index
*************************** 2. row ***************************
id: 3
select_type: SUBQUERY
table: NULL
type: NULL
possible_keys: NULL
key: NULL
key_len: NULL
ref: NULL
rows: NULL
Extra: Select tables optimized away

[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
*************************** 1. row ***************************
id: 1301
1 row in set (0.00 sec)

可以看到,雖然執(zhí)行計劃也是全索引掃描,但是因為有了LIMIT 1,只需要找到一條記錄,即可終止掃描,所以效率還是很快的。

小結(jié):

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

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

復(fù)制代碼 代碼如下:

[yejr@imysql]> explain select id from t_innodb_random order by rand() limit 1000\G
*************************** 1. row ***************************
id: 1
select_type: SIMPLE
table: t_innodb_random
type: index
possible_keys: NULL
key: idx_id
key_len: 4
ref: NULL
rows: 393345
Extra: Using index; Using temporary; Using filesort

[yejr@imysql]> select id from t_innodb_random order by rand() limit 1000;
1000 rows in set (0.41 sec)


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

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

復(fù)制代碼 代碼如下:

[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
*************************** 1. row ***************************
id: 1
select_type: PRIMARY
table: t_innodb_random
type: index
possible_keys: NULL
key: idx_id
key_len: 4
ref: NULL
rows: 393345
Extra: Using where; Using index
*************************** 2. row ***************************
id: 3
select_type: SUBQUERY
table: NULL
type: NULL
possible_keys: NULL
key: NULL
key_len: NULL
ref: NULL
rows: NULL
Extra: Select tables optimized away

[yejr@imysql]> select id from t_innodb_random where id > (select rand() * (select max(id) from t_innodb_random) as nid) limit 1000\G
1000 rows in set (0.04 sec)


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

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

復(fù)制代碼 代碼如下:

[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
*************************** 1. row ***************************
id: 1
select_type: PRIMARY
table: <derived2>
type: system
possible_keys: NULL
key: NULL
key_len: NULL
ref: NULL
rows: 1
Extra:
*************************** 2. row ***************************
id: 1
select_type: PRIMARY
table: t1
type: range
possible_keys: idx_id
key: idx_id
key_len: 4
ref: NULL
rows: 196672
Extra: Using where; Using index
*************************** 3. row ***************************
id: 2
select_type: DERIVED
table: NULL
type: NULL
possible_keys: NULL
key: NULL
key_len: NULL
ref: NULL
rows: NULL
Extra: No tables used
*************************** 4. row ***************************
id: 3
select_type: SUBQUERY
table: NULL
type: NULL
possible_keys: NULL
key: NULL
key_len: NULL
ref: NULL
rows: NULL
Extra: Select tables optimized away

[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
1000 rows in set (0.00 sec)


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

綜上,想從MySQL數(shù)據(jù)庫中隨機取一條或者N條記錄時,最好把RAND()生成隨機數(shù)放在JOIN子查詢中以提高效率。
上面說了那么多的廢話,最后簡單說下,就是把下面這個SQL:

復(fù)制代碼 代碼如下:

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

改造成下面這個:
復(fù)制代碼 代碼如下:

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

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

相關(guān)文章

  • mysql索引學(xué)習(xí)教程

    mysql索引學(xué)習(xí)教程

    在mysql 中,索引可以分為兩種類型 hash索引和 btree索引。這篇文章主要介紹了mysql索引的相關(guān)知識,非常不錯,具有參考借鑒價值,感興趣的朋友一起看看吧
    2016-09-09
  • MySQL可重復(fù)讀隔離級別下開啟事務(wù)的問題解決

    MySQL可重復(fù)讀隔離級別下開啟事務(wù)的問題解決

    本文主要介紹了MySQL可重復(fù)讀隔離級別下開啟事務(wù)的問題解決,詳解在Repeatable?Read隔離級別下,mysql的快照生成時機的問題,感興趣的可以了解一下
    2024-07-07
  • 解析mysql中的auto_increment的問題

    解析mysql中的auto_increment的問題

    本篇文章是對mysql中的auto_increment的問題進行了詳細的分析介紹,需要的朋友參考下
    2013-06-06
  • MySQL新建用戶中的%到底包不包括localhost?

    MySQL新建用戶中的%到底包不包括localhost?

    操作MySQL的時候發(fā)現(xiàn),有時只建了%的賬號,可以通過localhost連接,有時候卻不可以,網(wǎng)上搜索也找不到滿意的答案,干脆手動測試一波
    2019-02-02
  • MySQL找出未提交事務(wù)信息的方法分享

    MySQL找出未提交事務(wù)信息的方法分享

    這篇文章主要給大家介紹了關(guān)于MySQL如何找出未提交事務(wù)信息的相關(guān)資料,文中通過示例代碼介紹的非常詳細,對大家學(xué)習(xí)或者使用MySQL具有一定的參考學(xué)習(xí)價值,需要的朋友們下面來一起學(xué)習(xí)學(xué)習(xí)吧
    2019-06-06
  • MySQL數(shù)據(jù)庫主從復(fù)制與讀寫分離

    MySQL數(shù)據(jù)庫主從復(fù)制與讀寫分離

    大家好,本篇文章主要講的是MySQL數(shù)據(jù)庫主從復(fù)制與讀寫分離,感興趣的同學(xué)趕快來看一看吧,對你有幫助的話記得收藏一下,方便下次瀏覽
    2021-12-12
  • MySQL 使用 Performance Schema 定位和解決慢 SQL 問題

    MySQL 使用 Performance Schema 定位和解決慢 

    本文介紹了如何使用MySQL的PerformanceSchema來定位和解決慢SQL查詢問題,通過啟用PerformanceSchema并分析相關(guān)的系統(tǒng)表,可以收集到詳細的性能數(shù)據(jù),從而識別出影響性能的SQL語句,優(yōu)化策略包括優(yōu)化查詢語句、調(diào)整數(shù)據(jù)庫配置等
    2025-02-02
  • MySQL數(shù)據(jù)庫事務(wù)隔離級別詳解

    MySQL數(shù)據(jù)庫事務(wù)隔離級別詳解

    這篇文章主要介紹了MySQL數(shù)據(jù)庫事務(wù)隔離級別詳解的相關(guān)資料,需要的朋友可以參考下
    2017-03-03
  • mysql字段為NULL索引是否會失效實例詳解

    mysql字段為NULL索引是否會失效實例詳解

    有很多人對null值是否走索引感覺很疑惑,所以下面這篇文章主要給大家介紹了關(guān)于mysql字段為NULL索引是否會失效的相關(guān)資料,文中通過示例代碼介紹的非常詳細,需要的朋友可以參考下
    2022-05-05
  • Linux7.6二進制安裝Mysql8.0.27詳細操作步驟

    Linux7.6二進制安裝Mysql8.0.27詳細操作步驟

    大家好,本篇文章主要講的是Linux7.6二進制安裝Mysql8.0.27詳細操作步驟,感興趣的同學(xué)快來看一看吧,希望對你起到幫助
    2021-11-11

最新評論

苏尼特右旗| 宁安市| 嵊州市| 阿克苏市| 区。| 博客| 本溪市| 依兰县| 互助| 昭觉县| 同德县| 象州县| 拉孜县| 巴东县| 长治市| 西乡县| 福建省| 临邑县| 乌拉特前旗| 凤城市| 潮安县| 彰武县| 广州市| 化德县| 双流县| 襄垣县| 石城县| 观塘区| 江北区| 隆林| 祁东县| 托里县| 上栗县| 平顺县| 周口市| 定西市| 土默特右旗| 天峨县| 谢通门县| 南漳县| 西昌市|