Indexing in Mysql on multiple tables -
i have 4 tables, transaction table, customer table, products table & plant table.
i'm querying daily sale group product table item type, and, customer-wise sale on daily basis.
but these queries execution time high (my tran table 2m rows only)
how improve performance of db?
i created tables follows :
transaction table : create table `tran` ( `plant` char(4) not null collate 'utf8_bin', `tdate` date not null, `invno` varchar(10) null default null collate 'utf8_bin', `customer` char(8) not null collate 'utf8_bin', `icode` char(8) not null collate 'utf8_bin', `rqty` float(10,2) null default null, `fqty` float(10,2) null default null, `ramt` float(10,2) null default null, `taxamt` float(10,2) null default null, `tqty` float(10,2) null default null, `tamt` float(10,2) null default null ) collate='utf8_bin' engine=innodb row_format=compact ; customer details table : create table `cust` ( `slno` smallint(6) null default null, `aarea` char(2) null default null, `plant` char(4) null default null, `customer` char(8) not null collate 'utf8_bin', `cname` varchar(50) null default null, `doc` date null default null, `l1` varchar(25) null default null, `l2` varchar(25) null default null, `l3` varchar(35) null default null, `se` char(6) null default null, `sename` varchar(35) null default null ) collate='utf8_general_ci' engine=innodb row_format=compact ; products/materials table : create table `prod` ( `slno` int(3) null default null, `icode` char(8) not null, `igroup` char(8) null default null, `iname` char(50) null default null, `cat` char(5) null default null, `itype` char(20) null default null, `subtype` char(30) null default null, `norm` char(20) null default null, `sku` char(20) null default null, `icat` char(30) null default null, primary key (`icode`) ) collate='utf8_general_ci' engine=innodb row_format=compact ; plant table : create table `plant` ( `aarea` int(1) null default null, `aname` char(50) null default null, `mainplant` char(4) null default null, `plant` char(4) null default null, `pname` char(50) null default null, `shortname` char(50) null default null ) collate='utf8_general_ci' engine=innodb ; select query : select pc.mainplant,p.cat,p.itype, sum(if(year(tdate)=2015,tqty,0)) _2015, sum(if(year(tdate)=2016,tqty,0)) _2016 tran z left join plant pc on pc.plant=z.plant left join prod p on p.icode=z.icode left join cust c on c.customer=z.customer pc.mainplant = 'xxxx' group pc.mainplant,p.cat,p.itype;
- use
varchar
instead ofchar
unless string fixed width. - never use
float(m,n)
, either usefloat
"scientific" quantities ordecimal(m,n)
exact quantities, such money. - have
primary key
on every table, preferably using 'natural' pk column (or combination of columns) unique. - do not
left
unless right-hand table optional.
the first step in speeding query is
index(mainplant)
make rest of changes suggested, can make pass @ optimizing query. (the performance issue may solved adding index, plus pks.)
Comments
Post a Comment