[問題] 撈出後處理與撈出前處理

作者: qazsd ( )   2015-10-07 01:52:55
請教一下各位版大(文長,恕耐心觀看)
如果要算某資料表(repo)若干欄位的資料總數,
而該資料表有超過50個以上的欄位,
就以下兩種方式:
【方式1】
//用指定條件的sql(有100條)撈出資料,然後逐一將各筆資料放進datanum陣列裡
String[] sqlArray = new String[100];
sqlArray[0] = select count(*) from repo where 'type1' = '1';
sqlArray[1] = select count(*) from repo where 'type2' = '1';
..
..
sqlArray[99] = select count(*) from repo where 'type100' = '1';
PreparedStatement stmt;
ResultSet rs;
for (int i = 0; i < 100; i++) {
stmt = conn.prepareStatement(sqlArray[i].toString());
rs = stmt.executeQuery();
if (rs.next()) {
datanum[i] = rs.getInt(1);
}
}
【方式2】
//從資料庫用SQL一次撈出所有資料
select * from repo;
//再逐一判斷該筆資料的欄位是否符合指定條件
while(rs.next()){
if (rs.getString("type1"))=='1'
datanum[0]++;
if (rs.getString("type2"))=='1'
datanum[1]++;
..
..
if (rs.getString("type100"))=='1'
datanum[99]++;
}
最後兩者都會產生datanum[99]這個陣列。
想請教以上兩種方式就使用者而言哪種可以有會有較佳的回應速度?
又若條件變成為 "資料列有上萬筆" 或 "資料庫的欄位小於10個" 時,
上述兩種方式孰優孰劣還是不變嗎?
另外,資料表超過50個以上的欄位正規化後通常效率會比較好嗎?
謝謝!
作者: gmoz ( This can't do that. )   2015-10-07 14:25:00
同一台機器嗎
作者: qazsd ( )   2015-10-07 14:44:00
對 同一台機器
作者: adrianshum (Alien)   2015-10-08 08:10:00
什麼 DB? 有些 DB (e.g. Oracle) 有 Analytic func直接可以做到你想要的結果

Links booklink

Contact Us: admin [ a t ] ucptt.com