简体   繁体   中英

How to optimize this MySQL Query - inside a view? Performance tuning

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?

There is such a thing as "over-normalization".

There are standard "country codes" that are 2-letters. Make them CHAR(2) CHARACTER SET ascii , instead of INT to another table. That cuts down that column from 4 bytes to 2. And gets rid of a lookup.

YEAR is a 2-byte datatype; don't bother normalizing such.

Consider turning status into a 1-byte ENUM instead of a lookup.

And there may be others.

VIEWs are syntactic sugar, and never enhance performance. Sometimes performance is significantly worse.

I don't think there is any use for GROUP BY in the first VIEW .

band_* tables seem to be many-to-many mapping tables. Their performance can be improved by following these tips .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM