感覺好久沒有寫sql語句了...
今天需要寫一個統計某一天的資料時,考慮到字段的内容格式是: 2018-12-18 09:36:23
我開始寫的是:
select count(id) as reCount from t1 where dateValue(atTime)=" & sqlDate(theDate)
資料量在1萬行記錄的時候,盡然需要 3 秒+時間才能完成。
等所有代碼完工後,我試着使用 Between 語句:
select count(id) as reCount from t1 where atTime Between " & sqlDate(theDate) & " and " & sqlDate(theDate & " 23:59:59")
sql語句執行的時間感覺不到 1 秒,0.5 秒左右就完工了。
結論:
我一開始使用 dateValue(字段) 是錯誤的,這個不科學,太耗時了。
改成 between 後,速度提升很明顯。