Better MySQL Query Performance -



Better MySQL Query Performance -

i want sum of qtys of item# grouped months, query takes long (15 - 20) seconds fetch.

-total rows: 1495873 -total fetched rows: 9 - 12

the relation between 2 tables (invoice_header , invoice_detail) (one many) invoice_header header of invoice, totals. linked invoice_detail using location id (loc_id) , invoice number (invo_no), each location has own serial number. invoice detail contains details of each invoice.

is there's improve way enhance performance of query, here it's:

select sum(invoice_detail.qty) qty, month(invoice_header.date) month invoice_detail bring together invoice_header on invoice_detail.invo_no = invoice_header.invo_no , invoice_detail.loc_id = invoice_header.loc_id invoice_detail.item_id = {$itemid} grouping month(invoice_header.date) order month(invoice_header.date)

explain:

invoice_header table structure:

create table `invoice_header` ( `invo_type` varchar(1) not null, `invo_no` int(20) not null auto_increment, `invo_code` varchar(50) not null, `date` date not null, `time` time not null, `cust_id` int(11) not null, `loc_id` int(3) not null, `cash_man_id` int(11) not null, `sales_man_id` int(11) not null, `ref_invo_no` int(20) not null, `total_amount` decimal(19,2) not null, `tax` decimal(19,2) not null, `discount_amount` decimal(19,2) not null, `net_value` decimal(19,2) not null, `split` decimal(19,2) not null, `qty` int(11) not null, `payment_type_id` varchar(20) not null, `comments` varchar(255) not null, primary key (`invo_no`,`loc_id`) ) engine=innodb auto_increment=20286 default charset=utf8

invoice_detail table structure:

create table `invoice_detail` ( `invo_no` int(11) not null, `loc_id` int(3) not null, `serial` int(11) not null, `item_id` varchar(11) not null, `size_id` int(5) not null, `qty` int(11) not null, `rtp` decimal(19,2) not null, `type` tinyint(1) not null, primary key (`invo_no`,`loc_id`,`serial`), key `item_id` (`item_id`), key `size_id` (`size_id`), key `invo_no` (`invo_no`), key `serial` (`serial`) ) engine=innodb default charset=utf8

how long take next sql?

select count(*) invoice_detail invoice_detail.item_id = {$itemid}

if sql takes 15-20 seconds, should add together index on field item_id on inovice_detail table.

the invoice_header had primary key on bring together columns of invo_no , loc_id, don't need add together other indexes on invoice_header table. if add together index on fields invo_no, loc_id , date, may enhance little performance index scan.

mysql performance fetch enhancement

Comments

Popular posts from this blog

Delphi change the assembly code of a running process -

json - Hibernate and Jackson (java.lang.IllegalStateException: Cannot call sendError() after the response has been committed) -

C++ 11 "class" keyword -