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

mysql慢查詢優(yōu)化之從理論和實(shí)踐說明limit的優(yōu)點(diǎn)

 更新時(shí)間:2019年04月04日 09:24:12   作者:stpeace  
今天小編就為大家分享一篇關(guān)于mysql慢查詢優(yōu)化之從理論和實(shí)踐說明limit的優(yōu)點(diǎn),小編覺得內(nèi)容挺不錯的,現(xiàn)在分享給大家,具有很好的參考價(jià)值,需要的朋友一起跟隨小編來看看吧

很多時(shí)候, 我們預(yù)期查詢的結(jié)果最多是1條記錄數(shù)據(jù), 那么這個(gè)時(shí)候, 最好用上limit 1,  當(dāng)查到這條數(shù)據(jù)后, mysql會立即終止繼續(xù)查詢, 不進(jìn)行更多的無用查詢, 從而提升了效率。

我們來實(shí)際測試一下, 在一個(gè)擁有10萬的mysql表中, 查找lily的分?jǐn)?shù)(假設(shè)系統(tǒng)中只有1個(gè)lily, 而我們預(yù)期也只需要這條數(shù)據(jù))。為了顯示出時(shí)間的差別, 我并不對表的name字段建索引。

先看看表結(jié)構(gòu):

mysql> show create table tb_province;
+-------------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| Table    | Create Table                                                                                                                                                                                                                                                                                                                           |
+-------------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| tb_province | CREATE TABLE `tb_province` (
 `id` bigint(10) unsigned NOT NULL AUTO_INCREMENT,
 `name` varchar(32) NOT NULL,
 `score` int(10) unsigned DEFAULT '0',
 `x` int(10) unsigned DEFAULT '0',
 `x1` int(10) unsigned DEFAULT '0',
 `x2` int(10) unsigned DEFAULT '0',
 `x3` int(10) unsigned DEFAULT '0',
 `x4` int(10) unsigned DEFAULT '0',
 `x5` int(10) unsigned DEFAULT '0',
 `x6` int(10) unsigned DEFAULT '0',
 `x7` int(10) unsigned DEFAULT '0',
 `x8` int(10) unsigned DEFAULT '0',
 `x9` int(10) unsigned DEFAULT '0',
 `x10` int(10) unsigned DEFAULT '0',
 PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=124178 DEFAULT CHARSET=latin1 |
+-------------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
1 row in set (0.00 sec)

我們打開set profiling=1;的開關(guān),執(zhí)行mysql語句來對比:

mysql> select score from tb_province where name='lily';
+-------+
| score |
+-------+
|  100 |
+-------+
1 row in set (0.03 sec)

mysql> select score from tb_province where name='lily';
+-------+
| score |
+-------+
|  100 |
+-------+
1 row in set (0.03 sec)

mysql> select score from tb_province where name='lily';
+-------+
| score |
+-------+
|  100 |
+-------+
1 row in set (0.04 sec)

mysql> select score from tb_province where name='lily';
+-------+
| score |
+-------+
|  100 |
+-------+
1 row in set (0.02 sec)

mysql> select score from tb_province where name='lily';
+-------+
| score |
+-------+
|  100 |
+-------+
1 row in set (0.03 sec)

mysql> select score from tb_province where name='lily' limit 1;
+-------+
| score |
+-------+
|  100 |
+-------+
1 row in set (0.00 sec)

mysql> select score from tb_province where name='lily' limit 1;
+-------+
| score |
+-------+
|  100 |
+-------+
1 row in set (0.00 sec)

mysql> select score from tb_province where name='lily' limit 1;
+-------+
| score |
+-------+
|  100 |
+-------+
1 row in set (0.00 sec)

mysql> select score from tb_province where name='lily' limit 1;
+-------+
| score |
+-------+
|  100 |
+-------+
1 row in set (0.01 sec)

mysql> select score from tb_province where name='lily' limit 1;
+-------+
| score |
+-------+
|  100 |
+-------+
1 row in set (0.00 sec)

可見,我們針對是否采用limit 1進(jìn)行了5次對比測試, 來看看結(jié)果吧:

mysql> show profiles;
+----------+------------+---------------------------------------------------------+
| Query_ID | Duration  | Query                          |
+----------+------------+---------------------------------------------------------+
|    5 | 0.02686000 | select score from tb_province where name='lily'     |
|    6 | 0.02649050 | select score from tb_province where name='lily'     |
|    7 | 0.03413500 | select score from tb_province where name='lily'     |
|    8 | 0.02601350 | select score from tb_province where name='lily'     |
|    9 | 0.02785775 | select score from tb_province where name='lily'     |
|    10 | 0.00042300 | select score from tb_province where name='lily' limit 1 |
|    11 | 0.00043250 | select score from tb_province where name='lily' limit 1 |
|    12 | 0.00044350 | select score from tb_province where name='lily' limit 1 |
|    13 | 0.00053200 | select score from tb_province where name='lily' limit 1 |
|    14 | 0.00043250 | select score from tb_province where name='lily' limit 1 |
+----------+------------+---------------------------------------------------------+
14 rows in set, 1 warning (0.00 sec)

可見,采用limit 1后, mysql語句的效率確實(shí)提升很多。 當(dāng)表更大時(shí), 效率提升會更加明顯。 

我們已經(jīng)從理論和實(shí)踐的腳本都說明了limit的優(yōu)點(diǎn), 所以, 建議是:在可用limit的時(shí)候要用limit (當(dāng)然, 如果結(jié)果是多個(gè),肯定不能limit 1啊)

總結(jié)

以上就是這篇文章的全部內(nèi)容了,希望本文的內(nèi)容對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,謝謝大家對腳本之家的支持。如果你想了解更多相關(guān)內(nèi)容請查看下面相關(guān)鏈接

相關(guān)文章

  • 解決hibernate+mysql寫入數(shù)據(jù)庫亂碼

    解決hibernate+mysql寫入數(shù)據(jù)庫亂碼

    初次沒習(xí)hibernate,其中遇到問題在網(wǎng)上找的答案與大家共同分享!
    2009-07-07
  • 詳解用SELECT命令在MySQL執(zhí)行查詢操作的教程

    詳解用SELECT命令在MySQL執(zhí)行查詢操作的教程

    這篇文章主要介紹了詳解用SELECT命令在MySQL執(zhí)行查詢操作的教程,本文中還給出了基于PHP腳本的操作演示,需要的朋友可以參考下
    2015-05-05
  • 一文教你快速學(xué)會使用DDL對數(shù)據(jù)庫和表的操作

    一文教你快速學(xué)會使用DDL對數(shù)據(jù)庫和表的操作

    SQL是一種操作關(guān)系型數(shù)據(jù)庫的結(jié)構(gòu)化查詢語言,今天這篇文章將詳細(xì)講述數(shù)據(jù)定義語言DDL對數(shù)據(jù)庫和表的相關(guān)操作,有感興趣的同學(xué)跟著小編一起來學(xué)習(xí)吧
    2023-07-07
  • MySQL合并查詢結(jié)果的實(shí)現(xiàn)

    MySQL合并查詢結(jié)果的實(shí)現(xiàn)

    本文主要介紹了MySQL合并查詢結(jié)果的實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2023-03-03
  • 使用MYSQL TIMESTAMP字段進(jìn)行時(shí)間加減運(yùn)算問題

    使用MYSQL TIMESTAMP字段進(jìn)行時(shí)間加減運(yùn)算問題

    這篇文章主要介紹了使用MYSQL TIMESTAMP字段進(jìn)行時(shí)間加減運(yùn)算問題,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2023-02-02
  • mysql的存儲過程、游標(biāo) 、事務(wù)實(shí)例詳解

    mysql的存儲過程、游標(biāo) 、事務(wù)實(shí)例詳解

    這篇文章主要介紹了mysql的存儲過程、游標(biāo) 、事務(wù)實(shí)例詳解的相關(guān)資料,這里舉實(shí)例說明MySQL 存儲過程與游標(biāo)和事務(wù),需要的朋友可以參考下
    2017-08-08
  • Mysql連接join查詢原理知識點(diǎn)

    Mysql連接join查詢原理知識點(diǎn)

    在本文里我們給大家整理了一篇關(guān)于Mysql連接join查詢原理知識點(diǎn)文章,對此感興趣的朋友們可以學(xué)習(xí)下。
    2019-02-02
  • MySQL如何快速創(chuàng)建800w條測試數(shù)據(jù)表

    MySQL如何快速創(chuàng)建800w條測試數(shù)據(jù)表

    這篇文章主要介紹了MySQL如何快速創(chuàng)建800w條測試數(shù)據(jù)表,下面文章圍繞MySQL創(chuàng)建測試數(shù)據(jù)表的相關(guān)資料展開詳細(xì)內(nèi)容,具有一的的參考價(jià)值,需要的小伙伴可以參考一下
    2022-03-03
  • MySQL 存儲過程的基本用法介紹

    MySQL 存儲過程的基本用法介紹

    我們大家都知道MySQL 存儲過程是從 MySQL 5.0 開始逐漸增加新的功能。存儲過程在實(shí)際應(yīng)用中也是優(yōu)點(diǎn)大于缺點(diǎn)。不過最主要的還是執(zhí)行效率和SQL 代碼封裝。特別是 SQL 代碼封裝功能,如果沒有存儲過程。
    2010-12-12
  • MySQL索引操作命令詳解

    MySQL索引操作命令詳解

    這篇文章主要介紹了MySQL索引操作命令詳解,需要的朋友可以參考下
    2017-01-01

最新評論

宁都县| 寿宁县| 孝感市| 巩义市| 吉水县| 色达县| 满洲里市| 灵山县| 富顺县| 新密市| 渝中区| 阆中市| 平潭县| 离岛区| 海伦市| 大连市| 淳安县| 正定县| 大邑县| 镶黄旗| 平罗县| 屯留县| 屏边| 酒泉市| 金沙县| 永州市| 明溪县| 龙陵县| 交城县| 密山市| 江孜县| 旌德县| 德州市| 田林县| 留坝县| 齐河县| 保靖县| 九江市| 呈贡县| 潼南县| 遵义市|