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.

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

报告相同问题?

悬赏问题

  • ¥15 MATLAB代码补全插值
  • ¥15 Typegoose 中如何使用 arrayFilters 筛选并更新深度嵌套的子文档数组信息
  • ¥15 前后端分离的学习疑问?
  • ¥15 stata实证代码答疑
  • ¥50 husky+jaco2实现在gazebo与rviz中联合仿真
  • ¥15 dpabi预处理报错:Error using y_ExtractROISignal (line 251)
  • ¥15 在虚拟机中配置flume,无法将slave1节点的文件采集到master节点中
  • ¥15 husky+kinova jaco2 仿真
  • ¥15 zigbee终端设备入网失败
  • ¥15 金融监管系统怎么对7+4机构进行监管的