Hi there,
I need some help seeing if I can clean up this query to be faster. Currently, it table about 6.5 seconds and if I can speed it up, I'd love that.
3 tables.
Styles - This table is a list of styles and each of these styles needs to be listed on a page with some other data from the ensuing tables.
Important table fields.
Style_id, Style_name
Approximate Records: 120
Beers - This tables lists all the beers, breweries associated with them and the beer's respective style.
Important table fields.
Beer_id, Style_id
Approximate Records: 25K
Beer_Consumed - This table lists all beers consumed and also has a field for the style_id listed in Styles.
Important table fields.
consumed_id,style_id
Approximate Records: 50K
What I'd like to do is create a MySQL query that will list each style_name while also listing the number of beers for that style and how many consumed beers for that style in the same result set.
Here's what I have that taking 6.5 seconds.
SELECT style_name,
(
SELECT COUNT(beer_id)
FROM Beer
WHERE Beer.style_id=style.style_id
)
AS beers,
(
SELECT COUNT(consumed_id)
FROM Beer_Consumed
WHERE Beer_Consumed.style_id=Style.style_id
)
AS consumed
FROM Style ;
I need some help seeing if I can clean up this query to be faster. Currently, it table about 6.5 seconds and if I can speed it up, I'd love that.
3 tables.
Styles - This table is a list of styles and each of these styles needs to be listed on a page with some other data from the ensuing tables.
Important table fields.
Style_id, Style_name
Approximate Records: 120
Beers - This tables lists all the beers, breweries associated with them and the beer's respective style.
Important table fields.
Beer_id, Style_id
Approximate Records: 25K
Beer_Consumed - This table lists all beers consumed and also has a field for the style_id listed in Styles.
Important table fields.
consumed_id,style_id
Approximate Records: 50K
What I'd like to do is create a MySQL query that will list each style_name while also listing the number of beers for that style and how many consumed beers for that style in the same result set.
Here's what I have that taking 6.5 seconds.
SELECT style_name,
(
SELECT COUNT(beer_id)
FROM Beer
WHERE Beer.style_id=style.style_id
)
AS beers,
(
SELECT COUNT(consumed_id)
FROM Beer_Consumed
WHERE Beer_Consumed.style_id=Style.style_id
)
AS consumed
FROM Style ;