MySQL數(shù)據(jù)庫下limit與join的優(yōu)化方案
以下的文章主要描述的是MySQL數(shù)據(jù)庫下limit與join的實(shí)際優(yōu)化方案,我們大家都知道其在實(shí)際中的應(yīng)用比例還是占為多數(shù)的,如果你對這一技術(shù),心存好奇的話,以下的文章將會揭開它的神秘面紗。
PHP中分頁肯定會使用到MySQL的limit,大部分對類似”select * from title where uid =** order by id desc limit m,n”很熟悉,也不是全部都能看出里面有什么不對,可是當(dāng)是在大數(shù)據(jù)量下操作呢,比如百萬類似”select * from title where uid =177 order by id desc limit 1234567,20″就會發(fā)現(xiàn)sql執(zhí)行的時間明顯變得很長,為什么呢?
先從MySQL數(shù)據(jù)庫的limit原理說起,使用limit m,n是時候,MySQL先掃描(m+n)條記錄,然后從m行開始取n行.比如上面的例子就是先掃描1234587條數(shù)據(jù),這樣的話sql能快嗎?這就要 求我們盡可能的減少m的值,甚至沒有m直接limit n這樣是sql.
看個例子:
- mysql> select id,substr(mobile from 1 for 7),time,cpid,linkid from cp_mo100227 where cpid=769 limit 888888,10;
- +———-+—————————–+———————+——+———————-+
- | id | substr(mobile from 1 for 7) | time | cpid | linkid |
- +———-+—————————–+———————+——+———————-+
- | 11535090 | 1353554 | 2010-02-24 21:07:48 | 769 | 21064905903309587933 |
- | 11535091 | 1353750 | 2010-02-24 21:07:48 | 769 | 21064912943389480033 |
- | 11535093 | 1353394 | 2010-02-24 21:07:48 | 769 | 21064912945389480075 |
- | 11535098 | 1343073 | 2010-02-24 21:07:50 | 769 | 21064905865309587977 |
- | 11535100 | 1369270 | 2010-02-24 21:07:51 | 769 | 21064926770369210194 |
- | 11535103 | 1355683 | 2010-02-24 21:07:51 | 769 | 21064912944389480113 |
- | 11535104 | 1368959 | 2010-02-24 21:07:51 | 769 | 21064902508384448468 |
- | 11535105 | 1365243 | 2010-02-24 21:07:51 | 769 | 21064905907309403124 |
- | 11535106 | 1362145 | 2010-02-24 21:07:52 | 769 | 21065002511384448497 |
- | 11535107 | 1369228 | 2010-02-24 21:07:52 | 769 | 21064902514384448437 |
- +———-+—————————–+———————+——+———————-+
- 10 rows in set (3.84 sec)
- mysql> select id,substr(mobile from 1 for 7),time,cpid,linkid from cp_mo100227 where cpid=769 and id>=11535090 limit 10;
- +———-+—————————–+———————+——+———————-+
- | id | substr(mobile from 1 for 7) | time | cpid | linkid |
- +———-+—————————–+———————+——+———————-+
- | 11535090 | 1353554 | 2010-02-24 21:07:48 | 769 | 21064905903309587933 |
- | 11535091 | 1353750 | 2010-02-24 21:07:48 | 769 | 21064912943389480033 |
- | 11535093 | 1353394 | 2010-02-24 21:07:48 | 769 | 21064912945389480075 |
- | 11535098 | 1343073 | 2010-02-24 21:07:50 | 769 | 21064905865309587977 |
- | 11535100 | 1369270 | 2010-02-24 21:07:51 | 769 | 21064926770369210194 |
- | 11535103 | 1355683 | 2010-02-24 21:07:51 | 769 | 21064912944389480113 |
- | 11535104 | 1368959 | 2010-02-24 21:07:51 | 769 | 21064902508384448468 |
- | 11535105 | 1365243 | 2010-02-24 21:07:51 | 769 | 21064905907309403124 |
- | 11535106 | 1362145 | 2010-02-24 21:07:52 | 769 | 21065002511384448497 |
- | 11535107 | 1369228 | 2010-02-24 21:07:52 | 769 | 21064902514384448437 |
- +———-+—————————–+———————+——+———————-+
- 10 rows in set (0.00 sec)
- mysql> select id,substr(mobile from 1 for 7),time,cpid,linkid from cp_mo100227 where cpid=769 and time>=’2010-02-24 21:07:48′ limit 10;
- +———-+—————————–+———————+——+———————-+
- | id | substr(mobile from 1 for 7) | time | cpid | linkid |
- +———-+—————————–+———————+——+———————-+
- | 11535090 | 1353554 | 2010-02-24 21:07:48 | 769 | 21064905903309587933 |
- | 11535091 | 1353750 | 2010-02-24 21:07:48 | 769 | 21064912943389480033 |
- | 11535093 | 1353394 | 2010-02-24 21:07:48 | 769 | 21064912945389480075 |
- | 11535098 | 1343073 | 2010-02-24 21:07:50 | 769 | 21064905865309587977 |
- | 11535100 | 1369270 | 2010-02-24 21:07:51 | 769 | 21064926770369210194 |
- | 11535103 | 1355683 | 2010-02-24 21:07:51 | 769 | 21064912944389480113 |
- | 11535104 | 1368959 | 2010-02-24 21:07:51 | 769 | 21064902508384448468 |
- | 11535105 | 1365243 | 2010-02-24 21:07:51 | 769 | 21064905907309403124 |
- | 11535106 | 1362145 | 2010-02-24 21:07:52 | 769 | 21065002511384448497 |
- | 11535107 | 1369228 | 2010-02-24 21:07:52 | 769 | 21064902514384448437 |
- +———-+—————————–+———————+——+———————-+
- 10 rows in set (0.01 sec)
例中數(shù)據(jù)表id是主鍵,time也建了索引,表中總數(shù)據(jù)約為240w行,其中cpid為769的數(shù)據(jù)量大約為90w條.這里面的id和時間可能會是不連續(xù)的.故不能直接得獲取id>m這樣操作
所以可以顯示 “1,2,3,4,5,末頁” 或是 “首頁,<<100,101,102,103 >>末頁”這樣,這樣可以極大的減少m值!
MySQL里面的join順便說一句就是,通常有點(diǎn)講究的是用小表去驅(qū)動大表,而由于MySQL join實(shí)現(xiàn)的原理就是做循環(huán)比如left join就是對左邊的數(shù)據(jù)進(jìn)行循環(huán)去驅(qū)動右邊的表,比如左邊是可能會有m條記錄匹配,右邊有n條記錄那么就是做m次循環(huán),每次掃描n行數(shù)據(jù),總掃面行數(shù)是 m*n行數(shù)據(jù).左邊返回的結(jié)果集的大小就決定了循環(huán)的次數(shù),故單純的用小表去驅(qū)動大表不一定的正確的。
小表的結(jié)果集可能也大于大表的結(jié)果集,所以寫 join的時候盡可能的先估計(jì)兩張表的可能結(jié)果集,用小結(jié)果集去驅(qū)動大結(jié)果集.值得注意的是在使用left/right join的時候,從表的條件應(yīng)寫在on之后,主表應(yīng)寫在where之后.否則MySQL數(shù)據(jù)庫會當(dāng)作普通的連表查詢!
【編輯推薦】