hi,
i have certain tables for products, time and the market data the structures of which are shown below.
CREATE TABLE product ( `id` mediumint( 11 ) NOT NULL auto_increment ,
`code` int( 11 ) NOT NULL ,
`name` varchar( 150 ) NOT NULL ,
`brand_name` varchar( 200 ) NOT NULL ,
`start_date` date default NULL ,
`end_date` date default NULL ,
`code1` varchar( 45 ) NOT NULL ,
`brand_name2` varchar( 45 ) NOT NULL ,
PRIMARY KEY ( `id` ) ,
KEY `brand_name` ( `brand_name` ) ) ENGINE = MyISAM DEFAULT CHARSET = latin1 ROW_FORMAT = COMPACT;
CREATE TABLE time (
`id` int( 11 ) NOT NULL AUTO_INCREMENT ,
`date` date NOT NULL ,
`month` int( 9 ) default NULL ,
`year` int( 4 ) default NULL ,
PRIMARY KEY ( `id` ) ,
KEY `month` ( `month` ) ,
KEY `year` ( `year` )
) ENGINE = MYISAM DEFAULT CHARSET = latin1 ROW_FORMAT = COMPACT;
CREATE TABLE mkt_data ( `id` int( 11 ) NOT NULL auto_increment ,
`time_id` int( 11 ) NOT NULL ,
`brand_id` int( 11 ) NOT NULL ,
`buy_qty` bigint( 20 ) NOT NULL ,
`buy_value` bigint( 20 ) NOT NULL ,
`sold_qty` bigint( 20 ) NOT NULL ,
`sold_value` bigint( 20 ) NOT NULL ,
PRIMARY KEY ( `id` ) ,
KEY `time_id` ( `time_id` , `brand_id` ) ,
KEY `time_id_2` ( `time_id` ) ,
KEY `brand_id` ( `brand_id` ) ,
) ENGINE = MyISAM DEFAULT CHARSET = latin1 ROW_FORMAT = COMPACT;
Now i have performance issues when i run a query which joins all these 3 tables.
SELECT product.code, SUM(mkt_data.buy_value) FROM mkt_data, time, product WHERE time.id=mkt_data.time_id AND product.id=mkt_data.brand_id AND (time.year=2009 AND time.month<=8) GROUP BY product.code ORDER BY product.code
The problem with this query is that its very slow.
it takes almost 7 s to execute. Also it causes the use of filesort and temporary.
id select_type table type possible_keys key key_len ref rows Extra
1 SIMPLE product ALL PRIMARY NULL NULL NULL 102 Using temporary; Using filesort
could anyone pls help me out as to how i can reduce the timing since the table size is also very less ( in a few thousands).
its really very important to me as i am not able to figure out how to move ahead with this?
i have certain tables for products, time and the market data the structures of which are shown below.
CREATE TABLE product ( `id` mediumint( 11 ) NOT NULL auto_increment ,
`code` int( 11 ) NOT NULL ,
`name` varchar( 150 ) NOT NULL ,
`brand_name` varchar( 200 ) NOT NULL ,
`start_date` date default NULL ,
`end_date` date default NULL ,
`code1` varchar( 45 ) NOT NULL ,
`brand_name2` varchar( 45 ) NOT NULL ,
PRIMARY KEY ( `id` ) ,
KEY `brand_name` ( `brand_name` ) ) ENGINE = MyISAM DEFAULT CHARSET = latin1 ROW_FORMAT = COMPACT;
CREATE TABLE time (
`id` int( 11 ) NOT NULL AUTO_INCREMENT ,
`date` date NOT NULL ,
`month` int( 9 ) default NULL ,
`year` int( 4 ) default NULL ,
PRIMARY KEY ( `id` ) ,
KEY `month` ( `month` ) ,
KEY `year` ( `year` )
) ENGINE = MYISAM DEFAULT CHARSET = latin1 ROW_FORMAT = COMPACT;
CREATE TABLE mkt_data ( `id` int( 11 ) NOT NULL auto_increment ,
`time_id` int( 11 ) NOT NULL ,
`brand_id` int( 11 ) NOT NULL ,
`buy_qty` bigint( 20 ) NOT NULL ,
`buy_value` bigint( 20 ) NOT NULL ,
`sold_qty` bigint( 20 ) NOT NULL ,
`sold_value` bigint( 20 ) NOT NULL ,
PRIMARY KEY ( `id` ) ,
KEY `time_id` ( `time_id` , `brand_id` ) ,
KEY `time_id_2` ( `time_id` ) ,
KEY `brand_id` ( `brand_id` ) ,
) ENGINE = MyISAM DEFAULT CHARSET = latin1 ROW_FORMAT = COMPACT;
Now i have performance issues when i run a query which joins all these 3 tables.
SELECT product.code, SUM(mkt_data.buy_value) FROM mkt_data, time, product WHERE time.id=mkt_data.time_id AND product.id=mkt_data.brand_id AND (time.year=2009 AND time.month<=8) GROUP BY product.code ORDER BY product.code
The problem with this query is that its very slow.
it takes almost 7 s to execute. Also it causes the use of filesort and temporary.
id select_type table type possible_keys key key_len ref rows Extra
1 SIMPLE product ALL PRIMARY NULL NULL NULL 102 Using temporary; Using filesort
could anyone pls help me out as to how i can reduce the timing since the table size is also very less ( in a few thousands).
its really very important to me as i am not able to figure out how to move ahead with this?
Comment