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

MySQL下的RAND()優(yōu)化案例分析

 更新時(shí)間:2015年05月09日 09:58:47   投稿:goldensun  
這篇文章主要介紹了MySQL下的RAND()優(yōu)化案例,包括對JOIN查詢和子查詢的優(yōu)化,需要的朋友可以參考下

眾所周知,在MySQL中,如果直接 ORDER BY RAND() 的話,效率非常差,因?yàn)闀啻螆?zhí)行。事實(shí)上,如果等值查詢也是用 RAND() 的話也如此,我們先來看看下面這幾個(gè)SQL的不同執(zhí)行計(jì)劃和執(zhí)行耗時(shí)。
首先,看下建表DDL,這是一個(gè)沒有顯式自增主鍵的InnoDB表:

[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

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

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

1、常量等值檢索:

[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í)行計(jì)劃很不錯(cuò),是常量等值查詢,速度非??臁?/p>

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

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

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

[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í)行計(jì)劃依然是全索引掃描,執(zhí)行耗時(shí)也基本相當(dāng)。

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

[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í)行計(jì)劃也不好,執(zhí)行耗時(shí)較慢。

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

[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)

這時(shí)候執(zhí)行計(jì)劃就非常完美了,和最開始的常量等值查詢是一樣的了,執(zhí)行耗時(shí)也非常之快。
這種方法雖然很好,但是有可能查詢不到記錄,改造范圍查找,但結(jié)果LIMIT 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
*************************** 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í)行計(jì)劃也是全索引掃描,但是因?yàn)橛辛薒IMIT 1,只需要找到一條記錄,即可終止掃描,所以效率還是很快的。

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

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

[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)

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

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

[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)

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

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

[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行,這個(gè)方法是最快的。

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

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

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

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

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

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ù)去檢索了。

相關(guān)文章

  • MySQL服務(wù)器登陸故障ERROR 1820 (HY000)的解決方法

    MySQL服務(wù)器登陸故障ERROR 1820 (HY000)的解決方法

    這篇文章主要為大家詳細(xì)介紹了MySQL服務(wù)器登陸故障的解決方法,幫助大家解決ERROR 1820 (HY000)錯(cuò)誤,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2019-10-10
  • MySql查詢時(shí)間段的方法

    MySql查詢時(shí)間段的方法

    這篇文章主要介紹了MySql查詢時(shí)間段的方法,包括了傳統(tǒng)的針對時(shí)間字段的查詢方法與UNIX時(shí)間戳的查詢技巧,非常具有實(shí)用價(jià)值,需要的朋友可以參考下
    2014-12-12
  • MySQL 創(chuàng)建三張關(guān)系表實(shí)操

    MySQL 創(chuàng)建三張關(guān)系表實(shí)操

    這篇文章主要介紹了MySQL 創(chuàng)建三張關(guān)系表實(shí)操,文章說先創(chuàng)建學(xué)生表然后科目表和分?jǐn)?shù)表三張有著密切關(guān)系的表,下文實(shí)操分享需要的小伙伴可以參考一下
    2022-03-03
  • macOS 下的 MySQL 8.0.17 安裝與簡易配置教程圖解

    macOS 下的 MySQL 8.0.17 安裝與簡易配置教程圖解

    這篇文章主要介紹了macOS 下的 MySQL 8.0.17 安裝與簡易配置教程,本文圖文并茂給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2019-09-09
  • MySQL中year()和month()函數(shù)解析與輸出示例詳解

    MySQL中year()和month()函數(shù)解析與輸出示例詳解

    這篇文章主要介紹了MySQL中year()和month()函數(shù)解析與輸出,通過本文,我們詳細(xì)了解了MySQL中year()和month()函數(shù)的底層邏輯,它們能夠從日期或日期時(shí)間類型的數(shù)據(jù)中提取年份和月份,需要的朋友可以參考下
    2023-07-07
  • sql語句escape查詢數(shù)據(jù)中含通配字符[ %用法詳解

    sql語句escape查詢數(shù)據(jù)中含通配字符[ %用法詳解

    這篇文章主要為大家介紹了sql語句escape查詢數(shù)據(jù)中含通配字符[ %用法詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2023-08-08
  • 你的like語句為什么沒索引詳解

    你的like語句為什么沒索引詳解

    這篇文章主要給大家介紹了關(guān)于你的like語句為什么沒索引的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對大家學(xué)習(xí)或者使用mysql具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2018-10-10
  • windows下mysql 8.0.15 詳細(xì)安裝使用教程

    windows下mysql 8.0.15 詳細(xì)安裝使用教程

    這篇文章主要為大家詳細(xì)介紹了windows下mysql 8.0.15 詳細(xì)安裝使用教程,文中安裝步驟介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2019-08-08
  • MySQL和連接相關(guān)的timeout 的詳細(xì)整理

    MySQL和連接相關(guān)的timeout 的詳細(xì)整理

    這篇文章主要介紹了MySQL和連接相關(guān)的timeout 的詳細(xì)整理的相關(guān)資料,本文主要總結(jié)下和連接有關(guān)的timeout,需要的朋友可以參考下
    2017-08-08
  • 如何獲取SqlServer2005表結(jié)構(gòu)(字段,主鍵,外鍵,遞增,描述)

    如何獲取SqlServer2005表結(jié)構(gòu)(字段,主鍵,外鍵,遞增,描述)

    本篇文章是對如何獲取SqlServer2005表結(jié)構(gòu)(字段,主鍵,外鍵,遞增,描述)的方法進(jìn)行了詳細(xì)的分析介紹,需要的朋友參考下
    2013-06-06

最新評論

金山区| 阳原县| 华阴市| 公主岭市| 桃江县| 庐江县| 峡江县| 嘉兴市| 泽库县| 景谷| 密云县| 桑日县| 长治县| 金沙县| 建水县| 色达县| 铅山县| 阳信县| 遂溪县| 武山县| 金沙县| 郓城县| 江山市| 遵义县| 南昌县| 治县。| 阿瓦提县| 灌南县| 忻城县| 鸡西市| 临邑县| 聂荣县| 台中市| 太和县| 龙南县| 德格县| 瓮安县| 龙井市| 桓台县| 安仁县| 闻喜县|