
MySQL query optimization
- or -
Post a project like this- Posted:
- Proposals: 3
- Remote
- #1649353
- Awarded
Description
I have two tables:
CREATE TABLE `share_prices` (
`price_id` int(10) unsigned NOT NULL AUTO_INCREMENT,
`price_date` date NOT NULL,
`company_id` int(10) NOT NULL,
`high` decimal(20,2) DEFAULT NULL,
`low` decimal(20,2) DEFAULT NULL,
`close` decimal(20,2) DEFAULT NULL,
PRIMARY KEY (`price_id`),
UNIQUE KEY `price_date` (`price_date`,`company_id`),
KEY `company_id` (`company_id`),
KEY `price_date_2` (`price_date`)
) ENGINE=InnoDB AUTO_INCREMENT=368586 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
And,
CREATE TABLE `rating_lookup` (
`rating_id` int(11) NOT NULL,
`start_date` date DEFAULT NULL,
`start_price` decimal(10,2) DEFAULT NULL,
`broker_id` int(11) DEFAULT NULL,
`company_id` int(11) DEFAULT NULL,
`end_date` date DEFAULT NULL,
PRIMARY KEY (`rating_id`),
KEY `idx_rating_lookup_company_id` (`company_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
This is the current query:
SELECT broker_id, count(rating_id)
FROM (
SELECT rating_lookup.*,
share_prices.company_id as correct_company,
share_prices.price_date,
max(high) as peak_gain,
( ( ( max(high) - rating_lookup.start_price ) / rating_lookup.start_price ) * 100 ) as percent_gain
FROM rating_lookup, share_prices
WHERE share_prices.price_date > rating_lookup.start_date
AND share_prices.price_date 5
) correct
GROUP BY broker_id
Currently this query takes 0.391 sec (duration) / 10.438 sec (fetch)
The objective is:
To get the total amount of correct ratings per broker_id
A correct rating is defined as a rating that has a reached + 5% since its start_price.

John C.
100% (1)New Proposal
Login to your account and send a proposal now to get this project.
Log inClarification Board Ask a Question
-
How many records are in the db to take that amount of time?
John C.20 Jul 201718,000 rows in ratings_lookup
370,000 rows in share_prices -
you can modify
"WHERE share_prices.price_date > rating_lookup.start_date
AND share_prices.price_date 5"
to
WHERE hare_prices.price_date 5 AND share_prices.price_date > rating_lookup.start_date
and further more you can create index on share prices based on price_date
John C.20 Jul 2017I have realised there is a section wrong in the brief. The current query is this - sorry:
SELECT broker_id, count(rating_id)
FROM (
SELECT rating_lookup.*,
share_prices.company_id as correct_company,
share_prices.price_date,
max(high) as peak_gain,
( ( ( max(high) - rating_lookup.start_price ) / rating_lookup.start_price ) * 100 ) as percent_gain
FROM rating_lookup, share_prices
WHERE share_prices.price_date > rating_lookup.start_date
AND share_prices.price_date < IFNULL (end_date, curdate())
HAVING percent_gain > 5
) correct
GROUP BY broker_id
-
Do you have sample data? Since the project is about query optimization, it´s key to know what is your goal in terms of a target speed, so having similar data size that you expect to have in your system is important.
John C.20 Jul 2017Im on my mobile at the moment but i will post some in the morning