看以下例子:
Java代碼
* from
( * from b left join c on xx=xx left join d on xx=xx left join e on xx=xx)
as a where a.xx=xx
* from
( * from b left join c on xx=xx left join d on xx=xx left join e on xx=xx)
as a where a.xx=xx
由于a是一個很復雜的東西,關鍵a是別名出來的。
那這種寫法將會非常耗時。
但是如果將 * from b left join c on xx=xx left join d on xx=xx left join e on xx=xx放進一個臨時表,再從臨時表中加入where a.xx=xxsql 視圖中使用臨時表,性能將提高的非常明顯。
寫法如下:
Java代碼
* from b left join c on xx=xx left join d on xx=xx left join e on xx=xx into #
* from b left join c on xx=xx left join d on xx=xx left join e on xx=xx into #
因為臨時表用過后要刪除,所以考慮可以將整個過程放在一個存儲過程里,如下:
Sql代碼
寫了一個存儲過程對視圖進行分頁查詢,但數據增多后發現基效率低得要命,三萬多條數據要查詢一個半小時都沒出來,這不是要了命sql 視圖中使用臨時表,于是想到了索引,應用過后仍無濟于事。最后對sql進行分析和實踐中得出,使用臨時表可以大大加快視圖的查詢速度,見如下sql語句
性能超低的視圖分頁sql語句:
top 100 * from
ils where
( 1=1) and (payId not in
( top 100 payId from
ils where
( 1=1) order by payId desc))order by payId desc
使用臨時表提升性能的sql語句:
top 100 payId into #
from ils
order by payId desc
top 100 * from ils
where payId not in ( payId from # )
order by payId desc
drop table # ......
寫了一個存儲過程對視圖進行分頁查詢,但數據增多后發現基效率低得要命,三萬多條數據要查詢一個半小時都沒出來,這不是要了命,于是想到了索引,應用過后仍無濟于事。最后對sql進行分析和實踐中得出,使用臨時表可以大大加快視圖的查詢速度,見如下sql語句
性能超低的視圖分頁sql語句:
top 100 * from
ils where
( 1=1) and (payId not in
( top 100 payId from
ils where
( 1=1) order by payId desc))order by payId desc
使用臨時表提升性能的sql語句:
top 100 payId into #
from ils
order by payId desc
top 100 * from ils
where payId not in ( payId from # )
order by payId desc
drop table # ......