MySQL查询视图的速度问题

时间:2013-01-22 17:39:45

标签: mysql

我在各种解决方案中一直使用数据库,但从未真正设计过它们。因此,我对SQL的细节很新。我有一个包含许多表的数据库,但有两个主要表: 项目 - 引用许多其他表格,这些表格不随修订版本而变化 条目 - 引用许多其他表格,用于随修订版更改的数据 因此,每个项目都有一个或多个修订版(包含所有相关数据)。

我想用视图选择所有修订版(以及因此项目)的所有数据。我按如下方式构建了一个查询:

CREATE VIEW Single_Query
AS
SELECT  i.REF_CODE AS REF
gp.GROUP_NAME AS Group
v.VERSION_NUMBER AS Version
GROUP_CONCAT(DISTINCT(cy.COUNTRY_DESCRIPTION) SEPARATOR ', ') AS Country
ey.PUB_DATE AS Published
GROUP_CONCAT(DISTINCT(ct.CONTRIBUTOR_NAME) SEPARATOR ', ') AS Author
i.ISBN_CODE AS ISBN
GROUP_CONCAT(DISTINCT CONCAT(cn.COMPONENT_NUMBER, _utf8', ',cn.COMPONENT_DESCRIPTION) SEPARATOR '; ') AS Contents
ey.NOTES AS Notes
i.PRICE AS Price
cl.COLOUR_DESCRIPTION AS Colour
FROM entry AS ey
JOIN item AS i ON ey.ITEM_ID = i.ITEM_ID
JOIN group AS gp ON i.GROUP_ID = gp.GROUP_ID
JOIN version AS v ON ey.VERSION_ID = v.VERSION_ID
JOIN link_country_item AS lci ON i.ITEM_ID = lci.ITEM_ID
JOIN country AS cy ON lci.COUNTRY_ID = cy.COUNTRY_ID
LEFT JOIN link_entry_contributor AS lec ON ey.ENTRY_ID = lec.ENTRY_ID
LEFT JOIN contributor AS ct ON lec.CONTRIBUTOR_ID = ct.CONTRIBUTOR_ID
JOIN contents AS cn ON i.ITEM_ID = cn.ITEM_ID
JOIN colour AS cl ON ey.COLOUR_ID = cl.COLOUR_ID
GROUP BY REF_CODE, VERSION_NUMBER

这需要大约29秒才能完成,只有几个条目!但是,如果我运行一个脚本来为项目数据(以及所有引用的数据)和条目数据(以及所有引用的数据)创建临时表,那么从ITEM_ID上的单个JOIN中的2个临时表中选择整个批次,然后完成它需要不到半秒钟。

请注意,还有很多其他引用的表(为清楚起见,我省略了它们)。

如果有帮助,则item表引用组,设置(通过许多link_country_item表)和内容表。

条目表引用版本,贡献者(通过许多link_entry_contributor表)和颜色表。

问题是我不想一直使用临时表,因为我不能在视图中使用它们(或者至少我还没有发现如何)。有没有更优雅的方法在视图中拆分SQL,以便在这种情况下运行得更快?

非常感谢提前

伊恩

1 个答案:

答案 0 :(得分:0)

我稍微重新调整了查询​​顺序,并查看了有关REF_CODE的评论。所以你是对的,原始的索引答案不适合你。但是,如果项目表位于主要位置,那么如果它具有索引(REF_CODE,ITEM_ID)应该有助于提高性能,因为您正在使用表格启动,以及GROUP BY子句的相应索引。

CREATE VIEW Single_Query
AS
SELECT STRAIGHT_JOIN
      i.REF_CODE AS REF
      gp.GROUP_NAME AS Group
      v.VERSION_NUMBER AS Version
      GROUP_CONCAT(DISTINCT(cy.COUNTRY_DESCRIPTION) SEPARATOR ', ') AS Country
      ey.PUB_DATE AS Published
      GROUP_CONCAT(DISTINCT(ct.CONTRIBUTOR_NAME) SEPARATOR ', ') AS Author
      i.ISBN_CODE AS ISBN
      GROUP_CONCAT(DISTINCT CONCAT(cn.COMPONENT_NUMBER, _utf8', ',cn.COMPONENT_DESCRIPTION) SEPARATOR '; ') AS Contents
      ey.NOTES AS Notes
      i.PRICE AS Price
      cl.COLOUR_DESCRIPTION AS Colour
   FROM 
      item AS i 
         JOIN entry ey
            ON i.ITEM_ID = ey.ITEM_ID
            JOIN version AS v  
               ON ey.VERSION_ID = v.VERSION_ID
            JOIN colour AS cl 
               ON ey.COLOUR_ID = cl.COLOUR_ID
            LEFT JOIN link_entry_contributor AS lec 
               ON ey.ENTRY_ID = lec.ENTRY_ID
               LEFT JOIN contributor AS ct 
                  ON lec.CONTRIBUTOR_ID = ct.CONTRIBUTOR_ID
         JOIN group AS gp 
            ON i.GROUP_ID = gp.GROUP_ID
         JOIN link_country_item AS lci 
            ON i.ITEM_ID = lci.ITEM_ID
            JOIN country AS cy 
               ON lci.COUNTRY_ID = cy.COUNTRY_ID
         JOIN contents AS cn 
            ON i.ITEM_ID = cn.ITEM_ID
   GROUP BY 
      REF_CODE, 
      VERSION_NUMBER