查詢語句優化

2022-04-09 17:58:29 字數 1063 閱讀 2706

原查詢:select tid, fid, subject, author, authorid from dz_forum_thread where authorid in (select uid from dz_common_member where groupid in (1, 3, 23)) and dateline > 1395663240 and highlight <> 40 (用時10s)

優化後:select tid, fid, subject, author, authorid from dz_common_member as m, dz_forum_thread as t where m.uid=t.authorid and m.groupid in (1,3,23) and t.dateline > 1395663240 and t.highlight <> 40

在同時取1000條記錄的情況下

select tid, fid, subject, author, authorid

from dz_common_member as m, dz_forum_thread as t

where m.uid = t.authorid

and m.groupid

in ( 1, 3, 23 )

and t.dateline > 1305663240

and t.highlight <>40

limit 1000

用時0.0148s

select tid, fid, subject, author, authorid

from dz_common_member as m, dz_forum_thread as t

where m.uid = t.authorid

and (m.groupid=1 or m.groupid=3 or m.groupid=23)

and t.dateline >1305663240

and t.highlight <>40

limit 1000

用時0.0122s

總結:1. 盡量不要使用子查詢

2. 如果in的數目固定且比較少,可以用or替換

MYSQL查詢語句優化

一 日期查詢優化 在mysql中速度最慢的不是in查詢,而是date format以及from unixtime兩個函式的日期時間轉換,執行時間可能超過兩秒,造成 巨卡 用php的函式代替mysql的函式來完成將會大大的縮減時間 今日 from unixtime lastplaytime,y m d...

SQL查詢語句優化

sql查詢語句優化的使用方法 查詢語句的優化是sql效率優化的乙個方式,可以通過優化sql語句來盡量使用已有的索引,避免全表掃瞄,從而提高查詢效率。最近在對專案中的一些sql進行優化,總結整理了一些方法。1 在表中建立索引,優先考慮where group by使用到的字段。2 盡量避免使用selec...

MySQL查詢語句優化

想要對一條查詢語句進行優化,首先要對其進行分析,mysql提供了這個機制,可以通過explain sql或者desc sql的語法去獲取mysql對某一條語句的執行計畫 mysql優化之後的 explain的用法這裡就不再贅述了,在另外一篇文章中有詳細的解讀.對一條sql的優化可以分為兩部分,第一部...