提高临时表查询的性能以生成报告

时间:2014-03-02 19:28:31

标签: mysql sql

我有以下临时表语句,用于为应用程序生成所有报告。它执行的主要功能是计算给定销售项目的小计和总计。

如果必须计算较大的日期范围/销售数量,则最多可能需要10秒才能运行。您能想出一种更有效的方法来计算报告吗? (它可能涉及/不涉及临时表)

以下是一个示例查询:

CREATE TEMPORARY TABLE phppos_sales_items_temp (SELECT phppos_items.item_id, phppos_sales_items.line, sale_time,
phppos_sales_items.sale_id, payment_type, quantity_purchased, item_cost_price, item_unit_price, 
(item_unit_price*quantity_purchased-item_unit_price*quantity_purchased*discount_percent/100) as subtotal,  
(item_unit_price*quantity_purchased-item_unit_price*quantity_purchased*discount_percent/100)+(item_unit_price*quantity_purchased-item_unit_price*quantity_purchased*discount_percent/100)*(SUM(CASE WHEN cumulative != 1 THEN percent ELSE 0 END)/100) 
+(((item_unit_price*quantity_purchased-item_unit_price*quantity_purchased*discount_percent/100)*(SUM(CASE WHEN cumulative != 1 THEN percent ELSE 0 END)/100) + (item_unit_price*quantity_purchased-item_unit_price*quantity_purchased*discount_percent/100)) *
(SUM(CASE WHEN cumulative = 1 THEN percent ELSE 0 END))/100) as total
FROM phppos_sales_items 
INNER JOIN phppos_sales ON phppos_sales_items.sale_id=phppos_sales.sale_id 
INNER JOIN phppos_items ON phppos_sales_items.item_id=phppos_items.item_id 
LEFT OUTER JOIN phppos_suppliers ON phppos_items.supplier_id=phppos_suppliers.person_id 
LEFT OUTER JOIN phppos_sales_items_taxes ON phppos_sales_items.sale_id=phppos_sales_items_taxes.sale_id and phppos_sales_items.item_id=phppos_sales_items_taxes.item_id and phppos_sales_items.line=phppos_sales_items_taxes.line 
WHERE sale_time BETWEEN "2014-03-03 00:00:00" and "2014-03-03 23:59:59" and 
phppos_sales.location_id='1' and phppos_sales.store_account_payment=0 
GROUP BY sale_id, item_id, line)

涉及的表格:

mysql> describe phppos_items;
+-----------------------+----------------+------+-----+---------+----------------+
| Field                 | Type           | Null | Key | Default | Extra          |
+-----------------------+----------------+------+-----+---------+----------------+
| name                  | varchar(255)   | NO   | MUL | NULL    |                |
| category              | varchar(255)   | NO   | MUL | NULL    |                |
| supplier_id           | int(11)        | YES  | MUL | NULL    |                |
| item_number           | varchar(255)   | YES  | UNI | NULL    |                |
| product_id            | varchar(255)   | YES  | UNI | NULL    |                |
| description           | varchar(255)   | NO   |     | NULL    |                |
| tax_included          | int(1)         | NO   |     | 0       |                |
| cost_price            | decimal(23,10) | NO   |     | NULL    |                |
| unit_price            | decimal(23,10) | NO   |     | NULL    |                |
| promo_price           | decimal(23,10) | YES  |     | NULL    |                |
| start_date            | date           | YES  |     | NULL    |                |
| end_date              | date           | YES  |     | NULL    |                |
| reorder_level         | decimal(23,10) | YES  |     | NULL    |                |
| item_id               | int(10)        | NO   | PRI | NULL    | auto_increment |
| allow_alt_description | tinyint(1)     | NO   |     | NULL    |                |
| is_serialized         | tinyint(1)     | NO   |     | NULL    |                |
| image_id              | int(10)        | YES  | MUL | NULL    |                |
| override_default_tax  | int(1)         | NO   |     | 0       |                |
| is_service            | int(1)         | NO   |     | 0       |                |
| deleted               | int(1)         | NO   | MUL | 0       |                |
+-----------------------+----------------+------+-----+---------+----------------+
20 rows in set (0.00 sec)

mysql> describe phppos_sales_items;
+--------------------+----------------+------+-----+--------------+-------+
| Field              | Type           | Null | Key | Default      | Extra |
+--------------------+----------------+------+-----+--------------+-------+
| sale_id            | int(10)        | NO   | PRI | 0            |       |
| item_id            | int(10)        | NO   | PRI | 0            |       |
| description        | varchar(255)   | YES  |     | NULL         |       |
| serialnumber       | varchar(255)   | YES  |     | NULL         |       |
| line               | int(3)         | NO   | PRI | 0            |       |
| quantity_purchased | decimal(23,10) | NO   |     | 0.0000000000 |       |
| item_cost_price    | decimal(23,10) | NO   |     | NULL         |       |
| item_unit_price    | decimal(23,10) | NO   |     | NULL         |       |
| discount_percent   | int(11)        | NO   |     | 0            |       |
+--------------------+----------------+------+-----+--------------+-------+
9 rows in set (0.00 sec)

mysql> describe phppos_suppliers;
+----------------+--------------+------+-----+---------+-------+
| Field          | Type         | Null | Key | Default | Extra |
+----------------+--------------+------+-----+---------+-------+
| person_id      | int(10)      | NO   | MUL | NULL    |       |
| company_name   | varchar(255) | NO   |     | NULL    |       |
| account_number | varchar(255) | YES  | UNI | NULL    |       |
| deleted        | int(1)       | NO   | MUL | 0       |       |
+----------------+--------------+------+-----+---------+-------+
4 rows in set (0.01 sec)

mysql> describe phppos_sales_items_taxes;
+------------+---------------+------+-----+---------+-------+
| Field      | Type          | Null | Key | Default | Extra |
+------------+---------------+------+-----+---------+-------+
| sale_id    | int(10)       | NO   | PRI | NULL    |       |
| item_id    | int(10)       | NO   | PRI | NULL    |       |
| line       | int(3)        | NO   | PRI | 0       |       |
| name       | varchar(255)  | NO   | PRI | NULL    |       |
| percent    | decimal(15,3) | NO   | PRI | NULL    |       |
| cumulative | int(1)        | NO   |     | 0       |       |
+------------+---------------+------+-----+---------+-------+
6 rows in set (0.00 sec)

mysql> describe phppos_sales;
+-------------------------+--------------+------+-----+-------------------+----------------+
| Field                   | Type         | Null | Key | Default           | Extra          |
+-------------------------+--------------+------+-----+-------------------+----------------+
| sale_time               | timestamp    | NO   |     | CURRENT_TIMESTAMP |                |
| customer_id             | int(10)      | YES  | MUL | NULL              |                |
| employee_id             | int(10)      | NO   | MUL | 0                 |                |
| comment                 | text         | NO   |     | NULL              |                |
| show_comment_on_receipt | int(1)       | NO   |     | 0                 |                |
| sale_id                 | int(10)      | NO   | PRI | NULL              | auto_increment |
| payment_type            | varchar(255) | YES  |     | NULL              |                |
| cc_ref_no               | varchar(255) | NO   |     | NULL              |                |
| auth_code               | varchar(255) | YES  |     |                   |                |
| deleted_by              | int(10)      | YES  | MUL | NULL              |                |
| deleted                 | int(1)       | NO   | MUL | 0                 |                |
| suspended               | int(1)       | NO   |     | 0                 |                |
| store_account_payment   | int(1)       | NO   |     | 0                 |                |
| location_id             | int(11)      | NO   | MUL | NULL              |                |
+-------------------------+--------------+------+-----+-------------------+----------------+
14 rows in set (0.01 sec)

1 个答案:

答案 0 :(得分:0)

为什么不创建一个视图&然后在该视图上应用一些索引以提高性能?