2014-03-02 54 views
1

我有用於生成應用程序的所有報表的臨時表語句。它執行的主要功能是計算給定銷售商品的小計和總額。提高生成報表的臨時表查詢的性能

如果必須計算大日期範圍/銷售數量,則最多可能需要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) 

回答

0

爲什麼不創建一個視圖&然後應用在這一觀點的一些指標改善性能?

+0

如果我創建一個視圖我怎麼能有一個動態的WHERE sale_time BETWEEN「2014-03-03 00:00:00」和「2014-03-03 23:59:59」 –

+0

此外,它似乎在視圖索引唐在MySQL中工作:(http://stackoverflow.com/questions/244226/is-it-possible-to-have-an-indexed-view-in-mysql –