我有查询从数据库中计算零,四,八和十六的税,并在报告中查看它,它可以正常使用小型数据库但是当数据库获得大量记录时,它需要花费太多时间来处理和生成报告,所以我希望你检查一下并尝试解决问题,如果可以的话,提前谢谢。
这是我的查询;
SELECT sales_point.serial,sales_point.date,sales_point.tax,
ROUND(SUM(IFNULL((SELECT SUM(sales_point_transaction.total)
FROM sales_point_transaction
WHERE sales_point_transaction.serial_sales_point = sales_point.serial AND
sales_point_transaction.taxes = 0.0),0)),3) AS taxNo0,
ROUND(SUM(IFNULL((SELECT SUM(sales_point_transaction.total)
FROM sales_point_transaction
WHERE sales_point_transaction.serial_sales_point = sales_point.serial AND
sales_point_transaction.taxes = 0.04),0)),3) AS taxNo4,
ROUND(SUM(IFNULL((SELECT SUM(sales_point_transaction.total)
FROM sales_point_transaction
WHERE sales_point_transaction.serial_sales_point = sales_point.serial AND
sales_point_transaction.taxes = 0.08),0)),3) AS taxNo8,
ROUND(SUM(IFNULL((SELECT SUM(sales_point_transaction.total)
FROM sales_point_transaction
WHERE sales_point_transaction.serial_sales_point = sales_point.serial AND
sales_point_transaction.taxes = 0.16),0)),3) AS taxNo16,
ROUND(SUM(IFNULL((SELECT SUM(sales_point_transaction.total)
FROM sales_point_transaction
WHERE sales_point_transaction.serial_sales_point = sales_point.serial AND
sales_point_transaction.taxes = 0.0),0))
+SUM(IFNULL((SELECT SUM(sales_point_transaction.total)
FROM sales_point_transaction
WHERE sales_point_transaction.serial_sales_point = sales_point.serial AND
sales_point_transaction.taxes = 0.04),0))
+SUM(IFNULL((SELECT SUM(sales_point_transaction.total)
FROM sales_point_transaction
WHERE sales_point_transaction.serial_sales_point = sales_point.serial AND
sales_point_transaction.taxes = 0.08),0))
+SUM(IFNULL((SELECT SUM(sales_point_transaction.total)
FROM sales_point_transaction
WHERE sales_point_transaction.serial_sales_point = sales_point.serial AND
sales_point_transaction.taxes = 0.16),0))+ tax,3) AS total
FROM sales_point
WHERE sales_point.date between "2013-08-10" AND "2014-09-30"
GROUP BY serial
这是解释
+----+--------------------+-------------------------+-------+---------------------+---------+----
|id|select_type| table | type | possible_keys | key |key_len |ref |rows| Extra |
+----+--------------------+-------------------------+-------+---------------------+---------+----
|1|PRIMARY | sales_point | index | PRIMARY,userForeign | PRIMARY |4|NULL|9163| Using where |
|9|DEPENDENT SUBQUERY |sales_point_transaction|ALL| NULL|NULL| NULL| NULL|13528| Using where |
|8|DEPENDENT SUBQUERY |sales_point_transaction|ALL| NULL|NULL| NULL| NULL|13528| Using where |
|7|DEPENDENT SUBQUERY |sales_point_transaction|ALL| NULL|NULL| NULL| NULL|13528| Using where |
|6|DEPENDENT SUBQUERY |sales_point_transaction|ALL| NULL|NULL| NULL| NULL|13528| Using where |
|5|DEPENDENT SUBQUERY |sales_point_transaction|ALL| NULL|NULL| NULL| NULL|13528| Using where |
|4|DEPENDENT SUBQUERY |sales_point_transaction|ALL| NULL|NULL| NULL| NULL|13528| Using where |
|3|DEPENDENT SUBQUERY |sales_point_transaction|ALL| NULL|NULL| NULL| NULL|13528| Using where |
|2|DEPENDENT SUBQUERY |sales_point_transaction|ALL| NULL|NULL| NULL| NULL|13528| Using where |
+----+--------------------+-------------------------+-------+---------------------+---------+----
9 rows in set (0.00 sec)