2017/11/27

Statistically compare performance : Step sum total and Aggregates sum total (CHT)


有時我們會需要加總 DataSet 裡的某個欄位資訊給使用者,讓使用者可以很快速的掌握全局。

加總的方法很多,在資料庫典型的作法是:

SELECT SUM(Field) FROM Table



在 Delphi 中,我們常用的作法是這樣:

var
  LCount: Integer;
begin
  LCount := 0;
  while not DataSet.Eof do
  begin
    LCount := LCount + DataSet.FieldByName('FieldName').AsInteger;
    DataSet.Next;
  end;
end;

而在 Memory Table 中,有個 Aggregates (總計) 的功能,可以做到 SQL 中 SUM, COUNT, AVG, MIN, MAX 等相同函式的效果。

那麼,哪一種的效能比較好呢?

測試條件 ——
測試筆數:17656 筆
測試回數:10 回,取平均時間
測試項目:【逐筆加總】和【Aggregates 加總】比較

底下是結果圖,單位是 ms (毫秒)


結論:
體感上實在是沒什麼差,就按自己喜歡的方式實作吧!

See also


沒有留言:

張貼留言