Sql tstzrange的查询所包含的性能不佳
我有两张桌子: 账户交易:Sql tstzrange的查询所包含的性能不佳,sql,postgresql,indexing,range,postgresql-9.3,Sql,Postgresql,Indexing,Range,Postgresql 9.3,我有两张桌子: 账户交易: +-------------------------------+--------------------------+------------------------+ | Column | Type | Modifiers | +-------------------------------+--------------------------+----
+-------------------------------+--------------------------+------------------------+
| Column | Type | Modifiers |
+-------------------------------+--------------------------+------------------------+
| id | integer | not null |
| account_id | bigint | not null |
| created | timestamp with time zone | not null default now() |
| transaction_type | text | not null |
| amount | numeric(5,2) | not null |
| external_reference_id | character varying(60) | |
+-------------------------------+--------------------------+------------------------+
索引:
"idx_account_transaction_created" btree (created)
报告期:
+------------+--------------------------+-----------+
| Column | Type | Modifiers |
+------------+--------------------------+-----------+
| month | text | |
| created | timestamp with time zone | |
| date_range | tstzrange | |
+------------+--------------------------+-----------+
我想获得上一个报告期的所有交易记录。下面是两个产生相同结果的查询,但一个执行seq扫描,另一个可以使用idx_account_transaction_创建的索引
explain select count(*) from account_transaction where created <@ (select date_range from reporting_period order by created desc limit 1);
+----------------------------------------------------------------------------------------+
| QUERY PLAN |
+----------------------------------------------------------------------------------------+
| Aggregate (cost=4214.81..4214.82 rows=1 width=0) |
| InitPlan 1 (returns $0) |
| -> Limit (cost=13.20..13.20 rows=1 width=40) |
| -> Sort (cost=13.20..13.60 rows=800 width=40) |
| Sort Key: reporting_period.created |
| -> Seq Scan on reporting_period (cost=0.00..12.40 rows=800 width=40) |
| -> Seq Scan on account_transaction (cost=0.00..4200.81 rows=1602 width=0) |
| Filter: (created <@ $0) |
+----------------------------------------------------------------------------------------+
(8 rows)
explain select count(*) from account_transaction where created >= '2014-06-01' and created <= '2014-06-30 23:59:59.999999';
+------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| QUERY PLAN |
+------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| Aggregate (cost=2640.54..2640.54 rows=1 width=0) |
| -> Index Only Scan using idx_account_transaction_created on account_transaction (cost=0.08..2605.77 rows=69535 width=0) |
| Index Cond: ((created >= '2014-06-01 00:00:00+00'::timestamp with time zone) AND (created <= '2014-06-30 23:59:59.999999+00'::timestamp with time zone)) |
+------------------------------------------------------------------------------------------------------------------------------------------------------------------+
(3 rows)
解释从创建限额的账户交易中选择计数(*)(成本=13.20..13.20行=1宽度=40)|
|->排序(成本=13.20..13.60行=800宽=40)|
|排序键:已创建报告周期|
|->报告期内的顺序扫描(成本=0.00..12.40行=800宽度=40)|
|->按账户扫描交易(成本=0.00..4200.81行=1602宽度=0)|
|筛选器:(已创建='2014-06-01',已创建索引仅使用在帐户交易上创建的idx帐户交易扫描(成本=0.08..2605.77行=69535宽度=0)|
|索引条件:((创建>='2014-06-01 00:00:00+00'::带时区的时间戳)和(创建操作员
SELECT count(*) AS ct
FROM (
SELECT lower(date_range) AS ts_from, upper(date_range) AS ts_to
FROM reporting_period
ORDER BY created DESC
LIMIT 1
) r
JOIN account_transaction a ON a.created >= r.ts_from
AND a.created < r.ts_to
;
CHECK (lower_inc(date_range) AND NOT upper_inc(date_range))