mysql大查询优化

时间:2015-02-23 19:34:11

标签: mysql sql performance optimization query-performance

我需要优化以下查询,最多需要10分钟才能运行。 执行解释它似乎在“table_3”表的所有350815行上运行,而对于所有其他表运行1。 使用指针的一般规则是什么?我应该考虑使用多维索引吗?我最初应该在JOINS,WHERE还是GROUP BY上使用它们,如果我没记错的话,那么应该有一个层次结构。另外,如果我对所有表都有1行但是一个(在解释表的行列中)我通常如何优化我的优化包括最后只有一行所有列而不是一行。 所有表平均从100k到1000k +行。

CREATE TABLE datab1.sku_performance
SELECT 
      table1.sku,
      CONCAT(table1.sku,' ',table1.fk_container ) as sku_container,
      table1.price as price,
      SUM( CASE WHEN ( table1.fk_table1_status = 82 
                    OR table1.fk_table1_status = 119 
                    OR table1.fk_table1_status = 124 
                    OR table1.fk_table1_status = 141 
                    OR table1.fk_table1_status = 131) THEN 1 ELSE 0 END)
            / COUNT( DISTINCT id_catalog_school_class) as qty_returned,
      SUM( CASE WHEN ( table1.fk_table1_status In (23,13,44,65,6,75,8,171,12,166)) 
                THEN 1 ELSE 0 END) 
            / COUNT( DISTINCT id_catalog_school_class) as qt,
      container.id_container as container_id,
      container.idden as container_idden,
      container.delivery_badge,
      catalog_school.id_catalog_school,
      LEFT(catalog_school.flight_fair,2) as departing_country,
      catalog_school.weight,
      catalog_school.flight_type,
      catalog_school.price,
      table_3.id_table_3,
      table_3.fk_catalog_brand,
      MAX( LEFT( table_3.note,3 )) AS supplier,
      GROUP_CONCAT( product_number, ' by ',FORMAT(catalog_school_class.quantity,0)  
          ORDER BY product_number ASC SEPARATOR ' + ') as supplier_prod,
      Sum( distinct( catalog_school_class.purch_pri * catalog_school_class.quantity)) AS final_purch_pri,
      catalog_groupp.idden as supplier_idden,
      catalog_category_details.id_catalog_category,
      catalog_category_details.cat1 as product_cat1,
      catalog_category_details.cat2 as product_cat2,
      COUNT( distinct catalog_school_class.id_catalog_school_class) as setinfo, 
      datab1.pageviewgrouped.pv as page_views, 
      Sum(distinct(catalog_school_class.purch_pri * catalog_school_class.quantity)) AS purch_pri, 
      container_has_table_3.position, 
      max( table1.created_at ) as last_order_date
   FROM
      table1
         LEFT JOIN container 
            ON table1.fk_container = container.id_container
         LEFT JOIN catalog_school 
            ON table1.sku = catalog_school.sku
            LEFT JOIN table_3 
               ON catalog_school.fk_table_3 = table_3.id_table_3
               LEFT JOIN container_has_table_3  
                  ON table_3.id_table_3 = container_has_table_3.fk_table_3
               LEFT JOIN datab1.pageviewgrouped 
                  on table_3.id_table_3 = datab1.pageviewgrouped.url
                  LEFT JOIN datab1.catalog_category_details 
                     ON datab1.catalog_category_details.id_catalog_category = table_3_has_catalog_minority.fk_catalog_category
               LEFT JOIN catalog_groupp 
                     ON table_3.fk_catalog_groupp = catalog_groupp.id_catalog_groupp
               LEFT JOIN table_3_has_catalog_minority 
                  ON table_3.id_table_3 = table_3_has_catalog_minority.fk_table_3
            LEFT JOIN catalog_school_class 
               ON catalog_school.id_catalog_school = catalog_school_class.fk_catalog_school
   WHERE
          table_3.status_ok = 1
      AND catalog_school.status = 'active'
      AND table_3_has_catalog_minority.is_primary = '1'
   GROUP BY 
      table1.sku, 
      table1.fk_container;

enter image description here

每个表

行:

.table1 960096 to 1.3mn rows
.container 9275 to 13000 rows
.catalog_school 709970 to 1 mn rows
.table_3 709970 to 1 mn rows
.container_has_table_3 709970 to 1 mn rows
.pageviewgrouped 500000 rows
.catalog_school_class 709970 to 1 mn rows
.catalog_groupp 3000 rows
.table_3_has_catalog_minority  709970 to 1 mn rows
.catalog_category_details 659 rows

1 个答案:

答案 0 :(得分:0)

要添加到单个注释中太多了,所以我将在此处添加并在以后根据需要进行调整...您到处都有LEFT JOIN,但您的WHERE子句特别限定了Table_3,Catalog_School和Table_3_has_catalog_minority中的字段。默认情况下,它们会将其更改为INNER JOIN。

关于你的where子句

WHERE
          table_3.status_ok = 1
      AND catalog_school.status = 'active'
      AND table_3_has_catalog_minority.is_primary = '1'

根据这些标准,哪个表/列的结果最小。例如:Table_3.Status_ok = 1可能有500k记录,但table_3_has_catalog_minority.is_primary可能只有65k,catalog_school.status ='active'可能有430k。

此外,您的某些列不符合他们来自的表格。您能否请确认...例如“id_catalog_school_class”和“product_number”

有时,改变表的顺序,熟悉数据组成,在MySQL中添加“STRAIGHT_JOIN”关键字可以提高性能。这是我过去曾经使用gov't合同和赠款数据库的工作,拥有2000多万条记录并加入了大约15个以上的查找表。它从挂起服务器到在不到2小时内完成查询。考虑到我正在处理的数据量,这实际上是个好时机。

在解析了这个问题后,为了便于阅读,我重新构建了一些,为表引用添加了别名并更改了查询的顺序并有一些建议的索引。为了帮助查询,我尝试将Catalog_School表移动到第一个位置并添加了STRAIGHT_JOIN。索引首先基于STATUS来匹配WHERE子句,然后我包含了SKU,因为它是GROUP BY的第一个元素,然后是用于连接到后续表的其他列。通过在索引中包含这些列,它可以在不必转到原始数据的情况下限定连接。

通过将组更改为Catalog_School.SKU而不是table_1.SKU,可以使用catalog_school中的索引来帮助优化它。自catalog_school.sku = table_1.sku的连接以来,它是相同的值。我还为table_1和table_3添加了索引引用,这些引用也是建议 - 再次预先限定联接,而无需转到表的原始数据页。

我很想知道你的数据的最终表现(更好或更差)。

TABLE             INDEX ON...
catalog_school    ( status, sku, fk_table_3, id_catalog_school )
table_1           ( sku, fk_container )      
table_3           ( id_table_3, status_ok, fk_catalog_groupp )

SELECT STRAIGHT_JOIN
      CS.sku,
      CONCAT(CS.sku,' ',T1.fk_container ) as sku_container,
      T1.price as price,
      SUM( CASE WHEN ( T1.fk_table1_status IN ( 82, 119, 124, 141, 131) 
                THEN 1 ELSE 0 END)
            / COUNT( DISTINCT CSC.id_catalog_school_class) as qty_returned,
      SUM( CASE WHEN ( T1.fk_table1_status In (23,13,44,65,6,75,8,171,12,166)) 
                THEN 1 ELSE 0 END) 
            / COUNT( DISTINCT CSC.id_catalog_school_class) as qt,
      CS.id_catalog_school,
      LEFT(CS.flight_fair,2) as departing_country,
      CS.weight,
      CS.flight_type,
      CS.price,
      T3.id_table_3,
      T3.fk_catalog_brand,
      MAX( LEFT( T3.note,3 )) AS supplier,
      C.id_container as container_id,
      C.idden as container_idden,
      C.delivery_badge,
      GROUP_CONCAT( product_number, ' by ',FORMAT(CSC.quantity,0)  
          ORDER BY product_number ASC SEPARATOR ' + ') as supplier_prod,
      Sum( distinct( CSC.purch_pri * CSC.quantity)) AS final_purch_pri,
      CGP.idden as supplier_idden,
      CCD.id_catalog_category,
      CCD.cat1 as product_cat1,
      CCD.cat2 as product_cat2,
      COUNT( distinct CSC.id_catalog_school_class) as setinfo, 
      PVG.pv as page_views, 
      Sum(distinct(CSC.purch_pri * CSC.quantity)) AS purch_pri, 
      CHT3.position, 
      max( T1.created_at ) as last_order_date
   FROM
      catalog_school  CS

         JOIN table1 T1
            ON CS.sku = T1.sku
            LEFT JOIN container C
               ON T1.fk_container = C.id_container

         LEFT JOIN catalog_school_class  CSC
            ON CS.id_catalog_school = CSC.fk_catalog_school

         JOIN table_3  T3
            ON CS.fk_table_3 = T3.id_table_3
            JOIN table_3_has_catalog_minority T3HCM
               ON T3.id_table_3 = T3HCM.fk_table_3
               LEFT JOIN datab1.catalog_category_details  CCD
                  ON T3HCM.fk_catalog_category = CCD.id_catalog_category

            LEFT JOIN container_has_table_3  CHT3
               ON T3.id_table_3 = CHT3.fk_table_3

            LEFT JOIN datab1.pageviewgrouped  PVG
               on T3.id_table_3 = PVG.url

            LEFT JOIN catalog_groupp  CGP
               ON T3.fk_catalog_groupp = CGP.id_catalog_groupp
   WHERE
          CS.status = 'active'
      AND T3.status_ok = 1
      AND T3HCM.is_primary = '1'
   GROUP BY 
      CS.sku, 
      T1.fk_container;