Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/140.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
具有依赖子查询的MySQL查询-如何提高速度?_Mysql - Fatal编程技术网

具有依赖子查询的MySQL查询-如何提高速度?

具有依赖子查询的MySQL查询-如何提高速度?,mysql,Mysql,我有一个问题,我正试图加快速度。我确信有一种方法可以提高效率,但我缺乏知识 订单-显示当前收到的每个订单一行800000行 Query_time: 528.107584 Lock_time: 0.000652 Rows_sent: 1388 Rows_examined: 422442417 orders_financials-显示与当前2000000行订单关联的每个金融交易的一行 Query_time: 528.107584 Lock_time: 0.000652 Rows_sent:

我有一个问题,我正试图加快速度。我确信有一种方法可以提高效率,但我缺乏知识

订单-显示当前收到的每个订单一行800000行

Query_time: 528.107584  Lock_time: 0.000652 Rows_sent: 1388  Rows_examined: 422442417
orders_financials-显示与当前2000000行订单关联的每个金融交易的一行

Query_time: 528.107584  Lock_time: 0.000652 Rows_sent: 1388  Rows_examined: 422442417
orders\u financials表将包含每个订单的多行,交易类别显示在financialType中

因此,例如,对于一个订单,在orders\u financials中可能有5行

financialType = 14: value = 10.00
financialType = 12: value = 7.00
financialType = 18: value = 2.50
financialType = 15: value = 0.30
financialType = 17: value = 7.50
对于每个订单,将有不同数量的数据。随着时间的推移,更多的数据被添加到系统中,新行被添加到订单和财务中

我试图做的是运行一个查询,允许我为每个订单获取一行,并为这一行计算几个类别的金融交易的总数

以下查询应提供customerID 279下的所有订单:

select 
orders.orderID, 
customers.username, 
(select group_concat(financialRef) from orders_financials where orders_financials.orderID=orders.orderID and financialType IN (17) ) ref,
(select sum(amount) from orders_financials where orders_financials.orderID=orders.orderID and financialType IN (17) ) costs_cost,
(select sum(amount) from orders_financials where orders_financials.orderID=orders.orderID and financialType IN (12,13) ) costs_exp,
(select sum(amount) from orders_financials where orders_financials.orderID=orders.orderID and financialType IN (14) ) costs_sell,
(select sum(amount) from orders_financials where orders_financials.orderID=orders.orderID and financialType IN (15) ) costs_sell_out,                                       
(select sum(amount) from orders_financials where orders_financials.orderID=orders.orderID and financialType IN (1,2,3,9,11) ) extras_in,
(select sum(amount) from orders_financials where orders_financials.orderID=orders.orderID and financialType IN (4,5) ) extras_out,
(select sum(amount) from orders_financials where orders_financials.orderID=orders.orderID and financialType IN (20) ) extras_back,
(select sum(amount) from orders_financials where orders_financials.orderID=orders.orderID and financialType IN (18) ) insurance_in,
(select sum(amount) from orders_financials where orders_financials.orderID=orders.orderID and financialType IN (10,6) ) insurance_out,
(select sum(amount) from orders_financials where orders_financials.orderID=orders.orderID and financialType IN (19) ) insurance_back                                            
from orders 
left join customers on orders.customerID=customers.customerID 
where customers.customerID='279'
order by orderID
仅供参考,订单表中有18263条记录,其中customerID=279

你会发现12或13的财务类型可以加起来计算成本支出。同样,1、2、3、9、11都是额外的

最终结果是一个很好的查询,包含大量数据,但它提供了每个订单的详细概述。在where子句中,我可以按客户、服务、日期范围等进行搜索

我的问题是,这个查询需要很长时间才能运行。我必须以一种非常低效的方式来做,因为查询的每个元素都很快

从慢速日志中,我可以看到它正在迭代422000000行

Query_time: 528.107584  Lock_time: 0.000652 Rows_sent: 1388  Rows_examined: 422442417
必须有一个更有效的方法,但我很难看到

编辑-以下是对上述查询的解释:

+----+--------------------+-------------------+-------+-----------------------+---------------+---------+---------------------------+--------+--------------------------+
| id | select_type        | table             | type  | possible_keys         | key           | key_len | ref                       | rows   | Extra                    |
+----+--------------------+-------------------+-------+-----------------------+---------------+---------+---------------------------+--------+--------------------------+
|  1 | PRIMARY            | customers         | const | PRIMARY               | PRIMARY       | 4       | const                     |      1 |                          |
|  1 | PRIMARY            | orders            | ref   | customerID            | customerID    | 5       | const                     |  24802 | Using where; Using index |
| 12 | DEPENDENT SUBQUERY | orders_financials | ref   | orderID,financialType | financialType | 4       | const                     |      1 | Using where              |
| 11 | DEPENDENT SUBQUERY | orders_financials | range | orderID,financialType | financialType | 4       | NULL                      |      2 | Using where              |
| 10 | DEPENDENT SUBQUERY | orders_financials | ref   | orderID,financialType | financialType | 4       | const                     |  49717 | Using where              |
|  9 | DEPENDENT SUBQUERY | orders_financials | ref   | orderID,financialType | financialType | 4       | const                     |      1 | Using where              |
|  8 | DEPENDENT SUBQUERY | orders_financials | range | orderID,financialType | financialType | 4       | NULL                      |      2 | Using where              |
|  7 | DEPENDENT SUBQUERY | orders_financials | range | orderID,financialType | financialType | 4       | NULL                      |      5 | Using where              |
|  6 | DEPENDENT SUBQUERY | orders_financials | ref   | orderID,financialType | financialType | 4       | const                     |    139 | Using where              |
|  5 | DEPENDENT SUBQUERY | orders_financials | ref   | orderID,financialType | orderID       | 4       | p4db_admin.orders.orderID | 236338 | Using where              |
|  4 | DEPENDENT SUBQUERY | orders_financials | ref   | orderID,financialType | orderID       | 4       | p4db_admin.orders.orderID | 236338 | Using where              |
|  3 | DEPENDENT SUBQUERY | orders_financials | ref   | orderID,financialType | financialType | 4       | const                     |  99966 | Using where              |
|  2 | DEPENDENT SUBQUERY | orders_financials | ref   | orderID,financialType | financialType | 4       | const                     |  99966 | Using where              |
+----+--------------------+-------------------+-------+-----------------------+---------------+---------+---------------------------+--------+--------------------------+
13 rows in set (0.00 sec)
还添加订单上的索引\u financials.financialType

还添加订单上的索引\u financials.financialType


让我们解释一下这个问题。MySQL中的子查询通常是一个非常缓慢和痛苦的事情,所以如果你打算使用它们很多时候考虑切换到MiRADB。你在表上有哪些索引?尝试在OrthID、FieldType、FielalReF、金额上添加一个复合索引,然后你的查询和@ 412443的重写都显示了我们对该查询的解释。MySQL中的子查询通常是一个非常缓慢和痛苦的事情,所以如果你打算使用它们很多时候考虑切换到MiRADB。你在表上有哪些索引?试着在OrthID、FieldType、FielalReF、金额上添加一个复合索引,然后查询和重写“@ 49 1243”只是一个问题。在orders\u financials中,特定orderID可能有10行。其中一个是financialType 10,其对应的CarrierVoiceID为12345。我如何添加一个WHERE查询,从orders\u financial获取所有orderID,其中financialType=10,CarrierVoiceID=12345,然后在我的表中显示所有发现的orderID,并显示动态计算的总计?我在从orders\u financials中选择orderID时使用了:a.orderID,其中CarrierVoiceId='12345',但这需要很长时间,我想有更好的方法@491243在a.orderID=c.orderID和c.CarrierVoiceId='12345'上左连接orders\u financials c,这就是你想要的吗?不确定。如果我从orders\u financials运行SELECT orderID,其中financialType='17'和CarrierVoiceID='230',那么我会得到1864条记录。我只想让那些1864个订单ID成为被查询的订单ID,然后得到各种成本组的总数。希望这有意义@491243再思考一个问题。在orders\u financials中,特定orderID可能有10行。其中一个是financialType 10,其对应的CarrierVoiceID为12345。我如何添加一个WHERE查询,从orders\u financial获取所有orderID,其中financialType=10,CarrierVoiceID=12345,然后在我的表中显示所有发现的orderID,并显示动态计算的总计?我在从orders\u financials中选择orderID时使用了:a.orderID,其中CarrierVoiceId='12345',但这需要很长时间,我想有更好的方法@491243在a.orderID=c.orderID和c.CarrierVoiceId='12345'上左连接orders\u financials c,这就是你想要的吗?不确定。如果我从orders\u financials运行SELECT orderID,其中financialType='17'和CarrierVoiceID='230',那么我会得到1864条记录。我只想让那些1864个订单ID成为被查询的订单ID,然后得到各种成本组的总数。希望这有意义@491243