i have a database with some n:m relation tables and i am using a very big query to combine all those tables. Let's take a look firstly at my database:
I created two Views to compare performance.
First View:
CREATE VIEW `band_page1` AS (
SELECT maid, band_name, band_logo, band_img,
(SELECT countries.country_name from countries WHERE band_info.id_country = countries.id) as country,
(SELECT locations.location_name from locations WHERE band_info.id_location = locations.id) as location,
(SELECT status.status_name from status WHERE band_info.id_status = status.id) as status,
(SELECT founding.fyear from founding WHERE band_info.id_founding = founding.id) as founding,
(SELECT active.ayear from active WHERE band_info.id_active = active.id) as active,
(SELECT GROUP_CONCAT(DISTINCT genres.genre_name ORDER BY genres.genre_name)
FROM genres LEFT JOIN band_genres ON band_genres.id_genre = genres.id
WHERE band_genres.id_band = band_info.maid) AS genre,
(SELECT GROUP_CONCAT(DISTINCT themes.theme_name ORDER BY themes.theme_name)
FROM themes LEFT JOIN band_themes ON band_themes.id_theme = themes.id
WHERE band_themes.id_band = band_info.maid) AS themes,
(SELECT GROUP_CONCAT(DISTINCT labels.label_name ORDER BY labels.label_name)
FROM labels LEFT JOIN band_labels ON band_labels.id_label = labels.id
WHERE band_labels.id_band = band_info.maid) AS label
FROM band_info
GROUP BY band_info.maid);
Second View:
CREATE VIEW `band_page2` AS (
SELECT band_info.maid, band_info.band_name, band_info.band_logo, band_info.band_img,
(SELECT countries.country_name from countries WHERE band_info.id_country = countries.id) as country,
(SELECT locations.location_name from locations WHERE band_info.id_location = locations.id) as location,
(SELECT status.status_name from status WHERE band_info.id_status = status.id) as status,
(SELECT founding.fyear from founding WHERE band_info.id_founding = founding.id) as founding,
(SELECT active.ayear from active WHERE band_info.id_active = active.id) as active,
GROUP_CONCAT(DISTINCT genres.genre_name ORDER BY genres.genre_name) AS genre,
GROUP_CONCAT(DISTINCT themes.theme_name ORDER BY themes.theme_name) AS themes,
GROUP_CONCAT(DISTINCT labels.label_name ORDER BY labels.label_name) AS label
FROM band_info
LEFT JOIN band_genres ON band_genres.id_band = band_info.maid
LEFT JOIN band_themes ON band_themes.id_band = band_info.maid
LEFT JOIN band_labels ON band_labels.id_band = band_info.maid
LEFT JOIN genres ON genres.id = band_genres.id_genre
LEFT JOIN themes ON themes.id = band_themes.id_theme
LEFT JOIN labels ON labels.id = band_labels.id_label
GROUP BY band_info.maid);
When i go to phpmyadmin and open a view it takes 3 seconds on both views to show the query result. I am using this view to make a query like the following:
SELECT * FROM band_page1 where maid = '$id';
With php it feels like it is taking ages until i get a result back. It is not only taking 3 seconds it takes even much longer. My question is, how can i optimize my queries?