我有一個表productHistory優化查詢SQL使用join
productHistory (id_H , id_product , name , tsInsert);
我想從表中得到productHistory的最後一個產品在給予期間(開始,結束): tsInsert必須開始之間結束。
我可以這樣做:
select max(id_H)
from productHistory
where tsInsert>=:start and tsInsert <=:end
group by id_product;
然後從productHistory全部選擇其中ID_H在前面的選擇。
此查詢非常繁重,有沒有其他解決方案使用正確的連接爲例子?
你的說法是好得不能再好。我所能建議的「優化」是在表格中添加適當的索引。你使用的是什麼DBMS?你可以發佈查詢計劃嗎? –
「非常重」是否意味着執行時間會很長? – athabaska