我有一个查询,它基本上可以在特定日期范围内找到特定项目的 lastcost。
select first 1 lastcost from stock
where itemid = :itemid and date > :startdate and date < :enddate
order by date desc
此查询需要几秒钟才能完成数百万条记录,因为“>”不使用索引。如果我按年/月拆分查询并迭代直到它到达开始日期(假设每月有 1 百万条记录)会更快吗?
while (endate > startdate) do
begin
var_year = extract(year from :endate);
var_month = extract(month from :endate);
select first 1 lastcost from stock
where itemid = :itemid and year=:var_year and month=:var_month
order by date desc
enddate = dateadd (-1 month to enddate);
end
这几天我无法使用firebird,所以我自己也无法尝试。
提前致谢
问候,
雷纳尔迪