i'm using postgres 9.3 , wondering if there way speed particular query on large table. these tables:
table "public.frontend_prescription" column | type | modifiers -------------------+-------------------------+-------------------------------------------------------------------- id | integer | not null default nextval('frontend_prescription_id_seq'::regclass) presentation_code | character varying(15) | not null actual_cost | double precision | not null processing_date | date | not null pct_id | character varying(3) | not null indexes: "frontend_prescription_pkey" primary key, btree (id) "frontend_prescription_4e2e609b" btree (pct_id) "frontend_prescription_528f368c" btree (processing_date) "frontend_prescription_b9b2c7ab" btree (presentation_code) "frontend_prescription_cost_by_pres_code" btree (presentation_code, pct_id, actual_cost) "frontend_prescription_presentation_code_69403ee04fda6522_like" btree (presentation_code varchar_pattern_ops) "frontend_prescription_presentation_code_varchar_pattern_ops_idx" btree (presentation_code varchar_pattern_ops) table "public.frontend_pct" column | type | modifiers -------------------+-------------------------+----------- code | character varying(3) | not null name | character varying(200) | org_type | character varying(9) | not null
and query spending ccg organisations month, on particular presentation_code
:
select sum(frontend_prescription.actual_cost) val, frontend_prescription.pct_id row_id, frontend_prescription.processing_date date, frontend_pct.name row_name frontend_prescription, frontend_pct (presentation_code='0407041t0bbacac') , frontend_prescription.pct_id=frontend_pct.code , frontend_pct.org_type='ccg' group frontend_prescription.pct_id, frontend_pct.code, date order date, row_id
here results of explain (analyse, buffers)
on query: http://explain.depesz.com/s/yrr5
it looks slow part bitmap heap scan on frontend_prescription
. there way make faster? in particular, notice it's looping 211 times (once each pct
found in data).
the table has many millions of rows, suspect there isn't, wanted check see if there obvious do.
-- tables (indexes omitted) create table frontend_pct ( code varchar(3) not null primary key , name varchar(200) , org_type varchar(9) not null ); create table frontend_prescription ( id serial not null primary key , presentation_code varchar(15) not null , actual_cost double precision not null , processing_date date not null , pct_id varchar(3) not null references frontend_pct(code) ); -- rewritten query (shows flaws) explain analyze select pr.pct_id row_id , pr.processing_date zdate -- <<-- renamed this; "date" typename , pc.name row_name , sum(pr.actual_cost) val frontend_prescription pr join frontend_pct pc on pr.pct_id=pc.code , pc.org_type='ccg' pr.presentation_code='0407041t0bbacac' group pr.pct_id, pc.code, zdate -- ^^ ------ ^^ <- these same (maybe meant pc.name ??) -- (which functionally dependent on pc.code) order zdate, row_id -- <-- strange order; why not same "group by" ;
Comments
Post a Comment