java - Optimize mysql query that take long execution time about 4 minutes -
select nl.ndc, formulary_status bh.webdav_formulary_detail wfd inner join bh.payer_map pm on wfd.payer_map_id = pm.payer_map_id inner join bh.ndc_lookup nl on wfd.product_id = nl.uid pm.payer_id ='p00000000001001' , pm.formulary_id='01244' , nl.ndc in ('16590061572' , '35356078830' , '35356078860' , '35356078890' , '49999085690' , '54868381500' , '54868381501' , '54868381503' , '54868381504' , '54868381505' , '59011044010' , '59011044020' , '63629377401' , '63629377402' , '63629377403');
the below mysql tables myisam engine
show create table webdav_formulary_detail; create table webdav_formulary_detail ( product_id mediumint(8) unsigned not null, formulary_status char(2) not null, file_iid smallint(5) unsigned not null default '0', payer_map_id smallint(5) unsigned not null, key payer_map_id (payer_map_id), key product_id (product_id) ) engine=myisam default charset=latin1 create table ndc_lookup ( uid mediumint(8) unsigned not null, ndc char(11) not null default '0', primary key (uid), key uid (uid), key ndc (ndc) ) engine=myisam default charset=latin1 create table payer_map ( payer_map_id smallint(5) unsigned not null, payer_id varchar(80) default null, formulary_id varchar(50) default null, alternate_id varchar(50) default null, primary key (payer_map_id), key payer_map_id (payer_map_id), key payer_id (payer_id), key formulary_id (formulary_id), key alternate_id (alternate_id) ) engine=myisam default charset=latin1
how can optimize above mysql query improve execution time less half minute ?
where pm.payer_id ='p00000000001001' , pm.formulary_id='01244'
begs composite index on payer_map
: index(payer_id, formulary_id)
(or opposite order). better switch innodb or create 'covering' index: index(payer_id, formulary_id, payer_map_id)
.
- the optimizer start table best filtering possibilities -- pm.
index
gave make efficient. - the next table must
wfd
, usingpayer_map_id
.wfd
has index that. - finally
nl
come in usinguid
. again there pretty index. actually,index(uid, ndc)
better because of being "covering" index. or switching innodb because of 'clustering' ofprimary key
.
always provide explain select...;
see optimizer doing.
unrelated optimization:
primary key (payer_map_id), key payer_map_id (payer_map_id),
a primary key
unique key
key
, hence latter totally redundant; drop
it. ditto uid
.
my cookbook has more on how create indexes select
.
Comments
Post a Comment