='2015-02-0100:00:00'anda.macin(selectmacfromInst_User_MacbwheredoTime<='2015-" />

日韩久久久精品,亚洲精品久久久久久久久久久,亚洲欧美一区二区三区国产精品 ,一区二区福利

一個sql的優化

系統 2135 0
原文: 一個sql的優化

目的:為了查詢某天某個服務器上的登錄id的個數
?
剛開始編寫的sql:
select count(a.mac) logusers from Log_MacLogin_All a ?
? ? ? ? ? ? ? where isMoNi != 1?
? ? ? ? ? ? ? ? ? ? ? ?and loginTime <= '2015-02-01 23:59:59' and loginTime >= '2015-02-01 00:00:00'
? ? ? ? ? ? ? ? ? ? ? ?and a.mac in (select mac from Inst_User_Mac b
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? where doTime <= '2015-01-30 23:59:59'?
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? and doTime >= '2015-01-30 00:00:00 ? and serverKey='p1s1' );
?執行時間為:33s
對于這個sql,首先的優化點在于"in",因為測試發現in條件里面的數據差不多將近萬以上的數據..
?
第一次優化:把in改為exists后,效率有一點的提升
select count(mac) logusers from Log_MacLogin_All a ?
? ? ? ? ? ? ? where isMoNi != 1?
? ? ? ? ? ? ? ? ? ? ? ?and loginTime <= '2015-02-01 23:59:59' and loginTime >= '2015-02-01 00:00:00'
? ? ? ? ? ? ? ? ? ? ? ?and exists (select mac from Inst_User_Mac b
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? where doTime <= '2015-01-30 23:59:59'?
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? and doTime >= '2015-01-30 00:00:00 ? and serverKey='p1s1'
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? and a.mac = b.mac );
執行時間為:26s
?
第二次優化:在網上查了下,有的說用join會快一些。把exists改為join試試
select count(a.mac) logusers from Log_MacLogin_All ? a ?? inner join?
(select mac from Inst_User_Mac ??where doTime <= '2015-01-30 23:59:59'?
? ? ? ? ? ? ? ? ? ? ? ?and doTime >= '2015-01-30 00:00:00 ?? and serverKey='p1s1' ? ) ? ?b ?
? ? ? ? ? ? ? ? ? ? ? ? on a.mac = b.mac?
? ? ? ?where a.isMoNi != 1?
? ? ? ? ? ? ? ? ? ? ? ?and a.loginTime <= '2015-02-01 23:59:59' and a.loginTime >= '2015-02-01 00:00:00';
執行時間為2.6s,性能有了明顯的提高啊
?
第三次優化:把>= <= 改為between and 這樣會減少把數據查出來后的計算操作
select count(a.mac) logusers from Log_MacLogin_All ? a ?? inner join?
(select mac from Inst_User_Mac ??where doTime between??'2015-01-30 00:00:00 ?
? ? ? ? ? ? ? ? ? ? ? ?and??'2015-01-30 23:59:59'?? and serverKey='p1s1' ? ) ? ?b ?
? ? ? ? ? ? ? ? ? ? ? ? on a.mac = b.mac?
? ? ? ?where a.isMoNi != 1?
? ? ? ? ? ? ? ? ? ? ? ?and a.loginTime between?
?'2015-02-01 00:00:00' ? and? '2015-02-01 23:59:59';
執行時間為2.4s,提升了0.2s
?
第四次優化:假如要查某一天的數據不如直接date_format();
select count(a.mac) logusers from Log_MacLogin_All?? a?? inner join?
(select mac from Inst_User_Mac?? where DATE_FORMAT(doTime,'%Y%m%d')='20150130' and serverKey='p1s1'? )??? b??
??????????????????????? on a.mac = b.mac?
?????? where a.isMoNi != 1? and? DATE_FORMAT(a.loginTime,'%Y%m%d') = '20150201';
執行時間為2.36s,又提升了一點點..

一個sql的優化


更多文章、技術交流、商務合作、聯系博主

微信掃碼或搜索:z360901061

微信掃一掃加我為好友

QQ號聯系: 360901061

您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點擊下面給點支持吧,站長非常感激您!手機微信長按不能支付解決辦法:請將微信支付二維碼保存到相冊,切換到微信,然后點擊微信右上角掃一掃功能,選擇支付二維碼完成支付。

【本文對您有幫助就好】

您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描上面二維碼支持博主2元、5元、10元、自定義金額等您想捐的金額吧,站長會非常 感謝您的哦!!!

發表我的評論
最新評論 總共0條評論
主站蜘蛛池模板: 新巴尔虎右旗| 金秀| 乌兰浩特市| 平乐县| 东宁县| 广安市| 轮台县| 万州区| 资中县| 上蔡县| 长治县| 永昌县| 乾安县| 金川县| 盘山县| 噶尔县| 沭阳县| 韶山市| 铜川市| 右玉县| 莲花县| 宣恩县| 西畴县| 循化| 大方县| 湟中县| 精河县| 景泰县| 同仁县| 比如县| 万宁市| 边坝县| 醴陵市| 长汀县| 大田县| 博野县| 正安县| 墨江| 石阡县| 平顺县| 沅陵县|