mysql - effective query on a table with over 1 million record -


i struggling following 2 queries. have 2 tables both on million records. first query runs 30 seconds , second 1 runs on 7 mins.

basically count class.id , scan_layers.id based on lot. effective way it?

select count(class.id), count(scan_layers.id), lot, verified class left join scan_layers on (class.scanlayer = scan_layers.id) group lot;

select count(class.id), count(distinct scan_layers.id), lot, verified class left join scan_layers on (class.scanlayer = scan_layers.id) group lot;

as explain it, both giving me same explain.

+----+-------------+--------------------+--------+---------------+------------------------+---------+----------------------------------+---------+----------------------------------------------+ | id | select_type | table              | type   | possible_keys | key                    | key_len | ref                              | rows    |                                        | +----+-------------+--------------------+--------+---------------+------------------------+---------+----------------------------------+---------+----------------------------------------------+ |  1 | simple      | class | index  | null          | defects_scan_layers_fk | 4       | null                             | 4417159 | using index; using temporary; using filesort | |  1 | simple      | scan_layers        | eq_ref | primary       | primary                | 4       | cdb.class.scanlayer |       1 |                                              | +----+-------------+--------------------+--------+---------------+------------------------+---------+----------------------------------+---------+----------------------------------------------+  create table `class` ( `id` int(10) unsigned not null auto_increment, `scanlayer` int(10) unsigned not null, `type` enum('regular','critical') not null default 'regular', primary key (`id`), unique key `defect_unique` (`scanlayer`), key `class_scan_layers_fk` (`scanlayer`), constraint `class_scan_layers_fk` foreign key (`scanlayer`) references `scan_layers` (`id`) on update cascade ) engine=innodb   create table `scan_layers` ( `id` int(10) unsigned not null auto_increment, `layerinfo` int(10) unsigned not null, `lot` varchar(45) not null default 'default', `physicalid` int(10) unsigned not null, `scanned` datetime default null, `scannedmachine` int(10) unsigned default null, `defectscount` int(10) unsigned not null default '0', `movescount` int(10) unsigned not null default '0', `verified` datetime default null, `verifiedmachine` int(10) unsigned default null, primary key (`id`), unique key `scanlayer_unique` (`layerinfo`,`lot`,`physicalid`), key `scan_layers_layer_infos_fk` (`layerinfo`), key `scan_layers_scanned_machines_fk` (`scannedmachine`), key `scan_layers_verified_machines_fk` (`verifiedmachine`), key `scan_layers_verified` (`verified`), key `scan_layers_lot` (`lot`), constraint `scan_layers_layer_infos_fk` foreign key (`layerinfo`) references `layer_infos` (`id`) on update cascade, constraint `scan_layers_scanned_machines_fk` foreign key (`scannedmachine`) references `machines` (`id`) on update cascade, constraint `scan_layers_verified_machines_fk` foreign key (`verifiedmachine`) references `machines` (`id`) on update cascade, ) engine=innodb  

thanks lot!


Comments

Popular posts from this blog

java.util.scanner - How to read and add only numbers to array from a text file -

rewrite - Trouble with Wordpress multiple custom querystrings -