MySQL子查询优化

时间:2017-01-13 11:02:25

标签: php mysql subquery query-optimization

我在这里有最少4个表:

销售:

id
has_discount
discount_is_percentage
discount_amount
**sale_date_time**
**order_status**

Sales_items:

id
**sales_id**
has_discount
discount_is_percentage
discount_amount
**product_id** (This can sometimes be null)
price_inc_vat_per_item
quantity
vat_rate
is_removed

Sales_payments:

id
**sales_id**
payment_amount
payment_change
payment_method

产品:

id
product_name

我有一个查询,可以动态计算折扣并报告。这在记录总数低于100-200k的情况下效果很好。但随着数量的增加,所用的时间非常慢。我猜这是因为我正在使用的子查询。任何人都可以了解这一点。每个表上都有一个client_id和outlet_id,可以将它们与系统中的其他用户区分开来。

目前这些表有1-3百万行,有问题的客户端有300k-600k。查询需要30秒以上。对于行数较少的其他行,甚至可以在亚秒内获得。有星星的是指数。如何改进查询以获得相同的预期结果?我现在的查询:

SELECT  DATE_FORMAT(CONVERT_TZ(sales.sale_date_time,'UTC','Europe/London'),
                '%l%p') as title, count(*) as total_sales, SUM(sales_items.quantity
                   ) as total_quantities,
        SUM(sales_items.price_before_line_discount) as price_before_line_discount,
        SUM(sales_items.price_before_line_discount-sales_items.line_discount) as price_after_line_discount,
        SUM(sales_items.vat_rated_sales) as vat_rated_sales_before_discount,
        SUM(sales_items.zero_rated_sales) as zero_rated_sales_before_discount,
        SUM(sales_items.total_vat_only) as total_vat_only_before_discount,
        SUM(sales_payments.payment_taken) as payment_taken, SUM(sales_items.line_discount) as total_line_discount,
        SUM(sales_payments.payment_cash) as payment_cash, SUM( CASE WHEN sales.has_discount=1
              AND  sales.discount_is_percentage=0 THEN sales.discount_amount WHEN sales.has_discount=1
              AND  sales.discount_is_percentage=1 THEN ((sales_items.price_before_line_discount-sales_items.line_discount)*sales.discount_amount/100) WHEN sales.has_discount=0 THEN 0 END 
           )as total_sales_discount,
        SUM( CASE WHEN sales.has_discount=1 THEN CASE WHEN discount_is_percentage=0 THEN (sales_items.vat_rated_sales*sales.discount_amount)/(sales_items.price_before_line_discount-sales_items.line_discount) WHEN discount_is_percentage=1 THEN (sales_items.vat_rated_sales*((sales_items.price_before_line_discount-sales_items.line_discount)*sales.discount_amount/100))/(sales_items.price_before_line_discount-sales_items.line_discount) END ELSE 0 END )as vat_rated_sales_discount,
        SUM( CASE WHEN sales.has_discount=1 THEN CASE WHEN discount_is_percentage=0 THEN (sales_items.zero_rated_sales*sales.discount_amount)/(sales_items.price_before_line_discount-sales_items.line_discount) WHEN discount_is_percentage=1 THEN ((sales_items.zero_rated_sales*((sales_items.price_before_line_discount-sales_items.line_discount)*sales.discount_amount/100))/(sales_items.price_before_line_discount-sales_items.line_discount)) END ELSE 0 END )as zero_rated_sales_discount,
        SUM( CASE WHEN sales.has_discount=1 THEN CASE WHEN discount_is_percentage=0 THEN (sales_items.total_vat_only*sales.discount_amount)/(sales_items.price_before_line_discount-sales_items.line_discount) WHEN discount_is_percentage=1 THEN (sales_items.total_vat_only*((sales_items.price_before_line_discount-sales_items.line_discount)*sales.discount_amount/100))/(sales_items.price_before_line_discount-sales_items.line_discount) END ELSE 0 END )as total_vat_only_discount
    FROM  `sales`
    left join  
    (
        SELECT  sales_id, SUM(quantity) as quantity, SUM(price_inc_vat_per_item*quantity) AS price_before_line_discount,
                SUM( CASE WHEN has_discount=1
                      AND  discount_is_percentage=0 THEN discount_amount WHEN has_discount=1
                      AND  discount_is_percentage=1 THEN ((price_inc_vat_per_item*quantity)*discount_amount/100) WHEN has_discount=0 THEN 0 END 
                   )as line_discount,
                SUM( CASE WHEN vat_rate>0 THEN CASE WHEN has_discount=1
                      AND  discount_is_percentage=0 THEN ((price_inc_vat_per_item*quantity)-discount_amount) WHEN has_discount=1
                      AND  discount_is_percentage=1 THEN ((price_inc_vat_per_item*quantity)-((price_inc_vat_per_item*quantity)*discount_amount/100)) WHEN has_discount=0 THEN (price_inc_vat_per_item*quantity) END ELSE 0 END 
                   )as vat_rated_sales,
                SUM( CASE WHEN vat_rate=0 THEN CASE WHEN has_discount=1
                      AND  discount_is_percentage=0 THEN ((price_inc_vat_per_item*quantity)-discount_amount) WHEN has_discount=1
                      AND  discount_is_percentage=1 THEN ((price_inc_vat_per_item*quantity)-((price_inc_vat_per_item*quantity)*discount_amount/100)) WHEN has_discount=0 THEN (price_inc_vat_per_item*quantity) END ELSE 0 END 
                   )as zero_rated_sales,
                SUM( CASE WHEN vat_rate>0 THEN CASE WHEN has_discount=1
                      AND  discount_is_percentage=0 THEN ((price_inc_vat_per_item*quantity)-discount_amount)-((price_inc_vat_per_item*quantity)-discount_amount)/(1+(vat_rate/100)) WHEN has_discount=1
                      AND  discount_is_percentage=1 THEN ((price_inc_vat_per_item*quantity)-((price_inc_vat_per_item*quantity)*discount_amount/100))-((price_inc_vat_per_item*quantity)-((price_inc_vat_per_item*quantity)*discount_amount/100))/(1+(vat_rate/100)) WHEN has_discount=0 THEN (price_inc_vat_per_item*quantity)-(price_inc_vat_per_item*quantity)/(1+(vat_rate/100)) END ELSE 0 END 
                   )as total_vat_only
            FROM  sales_items
            WHERE  client_id='0fe26d93-775f-440c-a119-13cbcb6cbc0c'
              AND  is_removed=0
            GROUP BY  sales_id 
    ) as sales_items  ON `sales`.`id` = `sales_items`.`sales_id`
    left join  
    (
        SELECT  sales_id, SUM(payment_amount-payment_change) payment_taken,
                SUM(CASE WHEN payment_method='CASH' THEN (payment_amount-payment_change) ELSE 0 END) as payment_cash
            FROM  sales_payments
            WHERE  client_id='0fe26d93-775f-440c-a119-1396c36cbc0c'
            GROUP BY  sales_id
    ) as sales_payments  ON `sales`.`id` = `sales_payments`.`sales_id`
    WHERE  `sales`.`client_id` = '0fe26d93-775f-440c-a119-1396c36cbc0c'
      and  `sales`.`outlet_id` = 'd5b74bdf-5cef-4455-bf99-13cbcb6cbc0c'
      and  `sales`.`order_status` = 'COMPLETED'
      and  `sale_date_time` >= '2016-01-28 00:00:00'
      and  `sale_date_time` <= '2016-11-28 23:59:00'
    GROUP BY  HOUR(CONVERT_TZ(sales.sale_date_time,'UTC','Europe/London'))
    ORDER BY  `sale_date_time` ASC

更新

回答@ rick-james的问题

  • 我需要通过sale_date_time对其进行排序,这是一个日期时间字段。分组需要按小时报告。它还有几天,一年等等取决于所查询的时期。
  • 由于设计原因不得不使用UUID。整个数据库大约8GB,这四个表大部分都在这里。索引长度大于实际数据大小,因为我有很多外键约束。

它位于Amazon Aurora上,内存为15GB。

销售表:  0.5GB数据1.3GB索引

销售项目:  1.3GB数据3.2GB索引

销售付款:  0.5GB数据1.1GB索引

所有表格排序均为utf8_unicode_ci。

  • 它使用的是Aurora 5.6,即MySQL 5.6。这是解释选择。
  

ID select_type表类型possible_keys键key_len ref行过滤额外

     

1 PRIMARY sales ref sales_client_id_outlet_id_foreign,sales_client_id_index,sales_outlet_id_index,sales_sale_date_time_index,sales_order_status_index sales_client_id_index 108 const 5352使用   指数条件;用在哪里;使用临时;使用filesort

     

1 PRIMARY ref 108 MyDB.sales.id 10

     

1 PRIMARY ref 108 MyDB.sales.id 10

     

3 DERIVED sales_payments ref sales_payments_client_id_outlet_id_foreign,sales_payments_client_id_index sales_payments_client_id_outlet_id_foreign 108 const 5092使用索引条件;用在哪里;使用临时;使用filesort

     

2 DERIVED sales_items ref sales_items_client_id_outlet_id_foreign,sales_items_client_id_index sales_items_client_id_outlet_id_foreign 108 const 13340使用   指数条件;用在哪里;使用临时;使用filesort

     

2 DERIVED products eq_ref PRIMARY,products_id_unique PRIMARY 108 MyDB.sales_items.product_id 1

  • 可能会将结果存储在DB中并从中获取。唯一的问题是旧订单可以修改,如果发生这种情况,则需要重建总数。

是否有其他方法可以重写查询以获得所需的结果?

1 个答案:

答案 0 :(得分:0)

  • ORDER BYGROUP BY不必要时,需要额外的排序通行证。
  • 当数据大于可以缓存在RAM中的数据时,UUID非常低效。桌子有多大? `innodb_buffer_pool_size的价值是多少?你有多少RAM?
  • LEFT JOIN ( SELECT ... )在至少5.6之前非常低效。请提供EXPLAIN SELECT ...以查看它是否已优化。你在用什么版本?
  • 更糟糕的是LEFT JOIN ( SELECT ... ) LEFT JOIN ( SELECT ... )已添加:由于我没有看到“自动密钥”,这很糟糕。这让我想知道它是不是真的是MySQL 5.6。
  • 构建和维护“汇总表”可能是最终的答案。它可能包含PRIMARY KEY,包括client_id,outlet_id,order_status和sale_HOUR。
  • 子查询是否自行运行缓慢?如果是这样,请启动一个单独的问题,专注于子查询。请提供SHOW CREATE TABLE的输出;您对表的描述中缺少很多细节 - 索引,数据类型,大小,排序等。已添加:仍需要此;还有一些事情需要检查。一个可能的解决方案:CREATE TEMPORARY TABLELEFT JOIN SELECTs中的每一个do { try obj.thatThrowsError() } catch { print("Error on: line \(error.lineNumber)") print("Col \(error.column)") print("In file: \(error.sourceFileName)") print(error.message) } ;然后使用它们。