Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/59.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_Performance_Left Join - Fatal编程技术网

MySQL左连接性能问题

MySQL左连接性能问题,mysql,performance,left-join,Mysql,Performance,Left Join,我对MySQL版本5.5的左连接性能一直存在一些问题。在所有情况下,我都能够通过使用联合和子选择重新构造查询来解决这个问题,我在《高性能MySQL》一书中看到了一些这样的例子。问题是这会导致非常混乱的查询 下面是生成完全相同结果的两个查询的示例。第一个查询大约比第二个查询慢两个数量级。第二个查询的可读性远远低于第一个查询 据我所知,这些类型的查询并没有因为索引错误而表现不佳。在所有情况下,当我重新构造查询时,它都可以正常运行。我也尝试过仔细查看索引,并使用提示,但没有效果 还有其他人遇到过类似的

我对MySQL版本5.5的左连接性能一直存在一些问题。在所有情况下,我都能够通过使用联合和子选择重新构造查询来解决这个问题,我在《高性能MySQL》一书中看到了一些这样的例子。问题是这会导致非常混乱的查询

下面是生成完全相同结果的两个查询的示例。第一个查询大约比第二个查询慢两个数量级。第二个查询的可读性远远低于第一个查询

据我所知,这些类型的查询并没有因为索引错误而表现不佳。在所有情况下,当我重新构造查询时,它都可以正常运行。我也尝试过仔细查看索引,并使用提示,但没有效果

还有其他人遇到过类似的MySQL问题吗?是否有我应该尝试调整的服务器参数?有没有人找到一种更干净的方法来解决这类问题

问题1 问题2
我不知道为什么第二个更快,你能给我们提供更多的细节吗?我想我可以提供更多关于这些查询中涉及的表的结构、大小、索引等的细节。然而,我担心我可能会浪费你的时间来研究这个特定问题的细节。对于这个特定的查询,我并不十分好奇,因为如果其他人遇到类似的问题,以及是否有更好的解决方法,我使用的模式是unions+Subselect替换左连接。尽管如此,如果您认为它是相关的,还有哪些其他细节最有帮助?请原谅,在寻找其他内容时,对这个问题的评论很晚才出现,但在这种情况下,第二个不是更有效,因为它正在进行内部连接,因此拒绝没有匹配vp或pol表记录的连接,第一种是包括那些没有匹配记录的记录,但在计算总和时必须处理结果为空的记录,并且在执行此聚合函数时可能有更多的记录
select
  i.id,
  sum(vp.measurement * pol.quantity_ordered) measurement_on_order
from items i
left join (vendor_products vp, purchase_order_lines pol, purchase_orders po) on
  vp.item_id = i.id and
  pol.vendor_product_id = vp.id and
  pol.purchase_order_id = po.id and
  po.received_at is null and
  po.closed_at is null
group by i.id

explain:
+----+-------------+-------+--------+-------------------------------+-------------------+---------+-------------------------------------+------+-------------+
| id | select_type | table | type   | possible_keys                 | key               | key_len | ref                                 | rows | Extra       |
+----+-------------+-------+--------+-------------------------------+-------------------+---------+-------------------------------------+------+-------------+
|  1 | SIMPLE      | i     | index  | NULL                          | PRIMARY           | 4       | NULL                                |  241 | Using index |
|  1 | SIMPLE      | po    | ref    | PRIMARY,received_at,closed_at | received_at       | 9       | const                               |    2 |             |
|  1 | SIMPLE      | pol   | ref    | purchase_order_id             | purchase_order_id | 4       | nutkernel_dev.po.id                 |    7 |             |
|  1 | SIMPLE      | vp    | eq_ref | PRIMARY,item_id               | PRIMARY           | 4       | nutkernel_dev.pol.vendor_product_id |    1 |             |
+----+-------------+-------+--------+-------------------------------+-------------------+---------+-------------------------------------+------+-------------+
select
  i.id,
  sum(on_order.measurement_on_order) measurement_on_order
from (
  (
    select
      i.id item_id,
      sum(vp.measurement * pol.quantity_ordered) measurement_on_order
    from purchase_orders po
    join purchase_order_lines pol on pol.purchase_order_id = po.id
    join vendor_products vp on pol.vendor_product_id = vp.id
    join items i on vp.item_id = i.id
    where
      po.received_at is null and po.closed_at is null
    group by i.id
  )
  union all
  (select id, 0 from items)
) on_order
join items i on on_order.item_id = i.id
group by i.id

explain:
+------+--------------+------------+--------+-------------------------------+--------------------------------+---------+-------------------------------------+------+----------------------------------------------+
| id   | select_type  | table      | type   | possible_keys                 | key                            | key_len | ref                                 | rows | Extra                                        |
+------+--------------+------------+--------+-------------------------------+--------------------------------+---------+-------------------------------------+------+----------------------------------------------+
|  1   | PRIMARY      | <derived2> | ALL    | NULL                          | NULL                           | NULL    | NULL                                | 3793 | Using temporary; Using filesort              |
|  1   | PRIMARY      | i          | eq_ref | PRIMARY                       | PRIMARY                        | 4       | on_order.item_id                    |    1 | Using index                                  |
|  2   | DERIVED      | po         | ALL    | PRIMARY,received_at,closed_at | NULL                           | NULL    | NULL                                |   20 | Using where; Using temporary; Using filesort |
|  2   | DERIVED      | pol        | ref    | purchase_order_id             | purchase_order_id              | 4       | nutkernel_dev.po.id                 |    7 |                                              |
|  2   | DERIVED      | vp         | eq_ref | PRIMARY,item_id               | PRIMARY                        | 4       | nutkernel_dev.pol.vendor_product_id |    1 |                                              |
|  2   | DERIVED      | i          | eq_ref | PRIMARY                       | PRIMARY                        | 4       | nutkernel_dev.vp.item_id            |    1 | Using index                                  |
|  3   | UNION        | items      | index  | NULL                          | index_new_items_on_external_id | 257     | NULL                                | 3380 | Using index                                  |
| NULL | UNION RESULT | <union2,3> | ALL    | NULL                          | NULL                           | NULL    | NULL                                | NULL |                                              |
+------+--------------+------------+--------+-------------------------------+--------------------------------+---------+-------------------------------------+------+----------------------------------------------+