I need to extract a small amount of data,
but the table is very large and has many indexed columns,
which makes the response time too slow
but, i do’nt want create new index.
because, table is not mine
Env
PostgreSQL 13 AWS RDS
Question
if PostgreSQL supports Index Scan DESC operations.
can i use it?
My expectation is that this query could be faster if it used an Index Scan Desc and Index Skip Scan
Query
SELECT
billingYearMonth AS billingYearMonth
, customerId AS customerId
, childCustomerId AS childCustomerId
, deptCustomerId AS deptCustomerId
, cloudSupplyCode AS cloudSupplyCode
, sum(usage) AS USAGE
FROM
bill_big_table A
WHERE
billingYearMonth = '202407'
AND cloudSupplyCode = '05'
AND KEYSORTORDER = 3
GROUP BY
billingYearMonth
, customerId
, childCustomerId
, deptCustomerId
, cloudSupplyCode
ORDER BY
billingYearMonth
, customerId
, childCustomerId
, deptCustomerId
, cloudSupplyCode
OFFSET 0 ROWS
FETCH NEXT 1000 ROWS ONLY
Index columns (Normal)
Order | Column Name | Condition |
---|---|---|
1 | billingyearmonth | = |
2 | customerid | |
3 | childcustomerid | |
4 | deptcustomerid | |
5 | cloudsupplyinvoiceid | |
6 | cloudsupplyinvoiceitemid | |
7 | keybillingitemid | |
8 | keysortorder | = |
9 | categorycode | |
10 | cloudsupplycode | = |
raw DATA
INVOICEYM = ‘202407’
keysortorder | cloudsupplycode | Rows | |
---|---|---|---|
1 | 1 | 114 | |
2 | 1 | 177 | |
3 | 1 | 579 | |
1 | 2 | 1,021 | |
2 | 2 | 3,908 | |
3 | 2 | 4,179 | |
1 | 3 | 13 | |
2 | 3 | 13 | |
3 | 3 | 13 | |
1 | 4 | 12,000 | |
2 | 4 | 3,113,648 | |
3 | 4 | 18,151,142 | |
1 | 5 | 150 | |
2 | 5 | 534 | |
3 | 5 | 828 | wanna this |
1 | 6 | 3 | |
2 | 6 | 3 | |
3 | 6 | 3 | |
1 | 7 | 3,601 | |
2 | 7 | 49,871 | |
3 | 7 | 74,491 | |
1 | 9 | 381 | |
2 | 9 | 2,394 | |
3 | 9 | 2,394 | |
1 | 10 | 29 | |
2 | 10 | 56 | |
3 | 10 | 56 | |
1 | 12 | 102 | |
2 | 12 | 2,505 | |
3 | 12 | 3,556 |
Table rows 339,172,200
Disk Space 412G
Rel Size 189G
EXPLAIN PLAN
Limit (cost=0.95..106420.59 rows=1000 width=42) (actual time=11842.405..1695652.907 rows=16 loops=1)
Output: billingyearmonth, customerid, childcustomerid, deptcustomerid, cloudsupplycode, (sum(usage))
Buffers: shared hit=78 read=1637059
I/O Timings: read=3288604.358
-> GroupAggregate (cost=0.95..7681370.57 rows=72180 width=42) (actual time=11842.403..1695652.889 rows=16 loops=1)
Output: billingyearmonth, customerid, childcustomerid, deptcustomerid, cloudsupplycode, sum(usage)
Group Key: a.billingyearmonth, a.customerid, a.childcustomerid, a.deptcustomerid, a.cloudsupplycode
Buffers: shared hit=78 read=1637059
I/O Timings: read=3288604.358
-> Index Scan using bill_big_table_pkey on public.bill_big_table a (cost=0.95..7679565.03 rows=72249 width=42) (actual time=6203.945..1695652.135 rows=828 loops=1)
Output: billingyearmonth, customerid, childcustomerid, deptcustomerid, cloudsupplyinvoiceid, cloudsupplyinvoiceitemid, cloudsupplyinvoicetype, keybillingitemid, keysortorder, categorycode, cloudsupplycode, categoryname, itemname, pricecurrency, originamount, exchangerate, exchangeratedate, totalkrwamount, billingtype, startdate, enddate, orderuserid, productcode, usage, usageunit, startdate_l, enddate_l, billingentity, tagresourceid, meteringtype
Index Cond: (((a.billingyearmonth)::text = '202407'::text) AND (a.keysortorder = 3) AND (a.cloudsupplycode = '05'::bpchar))
Buffers: shared hit=78 read=1637059
I/O Timings: read=3288604.358
Planning:
Buffers: shared hit=122 read=12
I/O Timings: read=11.781
Planning Time: 13.274 ms
Execution Time: 1695653.003 ms
BlackOrc is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.