瀏覽單個文章
student
Amateur Member
 
student的大頭照
 

加入日期: Jul 2005
文章: 36
想請問關於資料庫Query的事情..

想請問各位一個情況 , 當有兩個將近70萬筆資料的資料表, 需要從兩個資料表作關聯, 取出50~100筆的資料, 想請問哪種作法會比較好?
以效能 或是 系統架構 或是 程式可讀性來說


1. select a.id, sum(b.price) from a, b where a.id = b.a_id group by a.id

2. select a.id, b.price_total from a left join (select a_id, sum(b.price) as price_total from b group by a_id) b on a.id = b.a_id

3. q = db.query(select id from a)

while (not q.eof)
q2 = select sum(price) from b where a_id = & q.id
end while

小弟覺得 1, 2差異不大
但是3 應該是最慢的...

但是有可能因為 q2 的執行時間很短, 所以很多個 簡單的query 會比一個複雜的query來得快??
不好意思, 舉的例子 , 可能不夠明顯, 大家可以想像情況是更複雜, 或是資料筆數更多
     
      
__________________
讓我獨中大樂透頭獎
舊 2008-04-09, 11:54 PM #1
回應時引用此文章
student離線中