我在这里有最少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的问题
它位于Amazon Aurora上,内存为15GB。
销售表: 0.5GB数据1.3GB索引
销售项目: 1.3GB数据3.2GB索引
销售付款: 0.5GB数据1.1GB索引
所有表格排序均为utf8_unicode_ci。
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
是否有其他方法可以重写查询以获得所需的结果?
答案 0 :(得分:0)
ORDER BY
与GROUP BY
不必要时,需要额外的排序通行证。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 TABLE
与LEFT 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)
}
;然后使用它们。