如果它是按日期索引的,那么以下应该很快:
select distinct year(date), month(date)
from transaction_table tt;
否则,您可以创建一个感兴趣的月份列表,然后在where子句中进行比较:
select months.*
from (select to_date('2013-01-01', 'YYYY-MM-DD') as firstday, to_date('2013-01-31', 'YYYY-MM-DD') as lastday from dual union all
select to_date('2013-02-01', 'YYYY-MM-DD') as firstday, to_date('2013-02-28', 'YYYY-MM-DD') as lastday
) as months
where exists (select 1
from transaction_table tt
where tt.date between months.firstday and months.lastday
)
Usingexists应该强烈建议优化器使用索引。