azzazzello
Technical User
Here are the details.
1) Here are 2 tables
2) There are about 150 entries per date in currency table
3) There are around 300,000-400,000 entries per date in globalPnL table
Where I run the following query, it returns 2-3 times slower than the same query without joining the currency table. Can someone suggest what may be done to alleviate this?
I realize that if I create an index of g.date,g.currency it would help, but the table is rather large, this is just part of the select (with other tables being joined that have nothing to do with currency), and I am trying to stay away from creating massive indexes.
I suspect that the fact that c.rate_currency is a char(3) and g.currency is varchar(8) might be contributing to this, but it's a guess.
Also, can someone suggest something other than "explain" that actually breaks down the query to its components and shows how much time what took after running it?
Thank you!
1) Here are 2 tables
Code:
mysql> show create table globalPnL;
| globalPnL | CREATE TABLE `globalPnL` (
`date` date NOT NULL,
`symbol` varchar(32) NOT NULL,
`account_id` int(16) NOT NULL,
`currency` varchar(8) NOT NULL default 'usd',
`pnl` float(13,4) NOT NULL default '0.0000',
`update_time` timestamp NOT NULL default CURRENT_TIMESTAMP on update CURRENT_TIMESTAMP,
PRIMARY KEY (`date`,`symbol`,`account_id`),
KEY `date_acct_symbol_inx` (`date`,`account_id`,`symbol`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1
Code:
mysql> show create table currency_rates;
| currency_rates | CREATE TABLE `currency_rates` (
`rate_date` date NOT NULL,
`rate_currency` char(3) NOT NULL,
`rate` float(13,4) NOT NULL default '0.0000',
PRIMARY KEY (`rate_date`,`rate_currency`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1 |
2) There are about 150 entries per date in currency table
3) There are around 300,000-400,000 entries per date in globalPnL table
Where I run the following query, it returns 2-3 times slower than the same query without joining the currency table. Can someone suggest what may be done to alleviate this?
Code:
SELECT g.date,g.account_id,sum(pnl * (case when c.rate is not NULL then c.rate else 1 end)) as convertedpnl from globalPnL g
LEFT JOIN currency_rates c on
g.date = c.rate_date and
g.currency = c.rate_currency
where date between 20110401 and 20110419
group by date,account_id
I realize that if I create an index of g.date,g.currency it would help, but the table is rather large, this is just part of the select (with other tables being joined that have nothing to do with currency), and I am trying to stay away from creating massive indexes.
I suspect that the fact that c.rate_currency is a char(3) and g.currency is varchar(8) might be contributing to this, but it's a guess.
Also, can someone suggest something other than "explain" that actually breaks down the query to its components and shows how much time what took after running it?
Thank you!