dongzhanlu0658 2017-10-18 11:29
浏览 221
已采纳

如何优化这个MySQL查询 - 在视图中? 性能调整

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:

enter image description here

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?

  • 写回答

1条回答 默认 最新

  • dtcuv8044 2017-10-18 18:14
    关注

    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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥35 平滑拟合曲线该如何生成
  • ¥100 c语言,请帮蒟蒻写一个题的范例作参考
  • ¥15 名为“Product”的列已属于此 DataTable
  • ¥15 安卓adb backup备份应用数据失败
  • ¥15 eclipse运行项目时遇到的问题
  • ¥15 关于#c##的问题:最近需要用CAT工具Trados进行一些开发
  • ¥15 南大pa1 小游戏没有界面,并且报了如下错误,尝试过换显卡驱动,但是好像不行
  • ¥15 自己瞎改改,结果现在又运行不了了
  • ¥15 链式存储应该如何解决
  • ¥15 没有证书,nginx怎么反向代理到只能接受https的公网网站