MySQL查询导致服务器崩溃?

发布于 2024-12-01 16:10:31 字数 519 浏览 2 评论 0原文

在我的 MySQL 数据库中,我有两个表:“orders”和“orders_lines”。订单包含有关客户、订单日期等的信息。 Orders_lines 包含有关订购产品的数据,例如product_id、价格、数量、税率等。

我目前正在构建一个搜索页面,我想在其中返回包含特定产品的订单。例如,我想返回product_id为2的所有订单。我想到了这样的查询:

SELECT * FROMordersWHEREorder_idIN(SELECTorder_idFROMorders_linesWHEREproduct_id=2)

但是当我执行时今天早上的查询我不得不打电话给托管提供商来终止 MySQL 进程,因为它太重了。从来没有听说过,也不知道为什么,现在我非常绝望。 orders 表包含大约 30.000 行,orders_lines 表大约包含 38.000 行。

欢迎就我的数据库崩溃的原因、如何更改查询以使其正常工作或如何实现显示包含特定产品的订单的目标提出任何建议!

亲切的问候,

马丁

In my MySQL database I have two tables: 'orders' and 'orders_lines'. Orders contains info about the customer, order date, and so on. Orders_lines contains data about the oredered products, like product_id, price, quantity, tax rate, and so on.

I'm currently building a search-page in which I'd like to return the orders that contain a specific product. For example, I'd like to return all orders with product_id 2. I thought of a query like:

SELECT * FROM orders WHERE order_id IN ( SELECT order_id FROM orders_lines WHERE product_id = 2 )

But when I executed the query this morning I had to call the hosting provider to terminate the MySQL processes because it was way to heavy. Never heard of it and could not figure out why, and I'm pretty desperate at the moment. The orders table contains about 30.000 rows, the orders_lines table about 38.000 rows.

Any suggestions why my database crashes, how to change the query to make it work correctly or how to achieve my goal of displaying orders that contain a certain product are welcome!

Kind regards,

Martijn

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

很酷不放纵 2024-12-08 16:10:31

可以使用JOIN(和GROUP BY)重写查询:

SELECT o.* 
FROM orders o
  INNER JOIN order_lines ol
    ON o.order_id = ol.order_id
WHERE ol.product_id = 2
GROUP BY o.order_id

或者最好 - 因为您只需要来自orders表的结果 - 使用EXISTS

SELECT * 
FROM orders AS o
WHERE EXISTS  
      ( SELECT * 
        FROM orders_lines AS ol
        WHERE ol.product_id = 2 
          AND ol.order_id = o.order_id
      )

但是在再次运行可能导致服务器故障的操作之前请检查您的索引和EXPLAIN

The query can be rewritten with JOIN (and GROUP BY):

SELECT o.* 
FROM orders o
  INNER JOIN order_lines ol
    ON o.order_id = ol.order_id
WHERE ol.product_id = 2
GROUP BY o.order_id

or preferably - as you want only results from the orders table - with EXISTS:

SELECT * 
FROM orders AS o
WHERE EXISTS  
      ( SELECT * 
        FROM orders_lines AS ol
        WHERE ol.product_id = 2 
          AND ol.order_id = o.order_id
      )

But check your indexes and the EXPLAIN before running again something that might cause your server to fault.

萌能量女王 2024-12-08 16:10:31

我不确定这是否是您所需要的,无论如何尝试:

SELECT DISTINCT o.* FROM orders o
INNER JOIN order_lines ol
ON o.order_id = ol.order_id
WHERE ol.product_id = 2

I'm not sure this is what you need, anyway try:

SELECT DISTINCT o.* FROM orders o
INNER JOIN order_lines ol
ON o.order_id = ol.order_id
WHERE ol.product_id = 2
锦爱 2024-12-08 16:10:31

据我所知,在这种情况下,内部联接(又名自然联接)是最好的方法。 “WHERE .. IN ..”往往是一项昂贵的操作。

SELECT DISTINCT o.* FROM orders o
INNER JOIN orders_lines ol ON  o.order_id = ol.order_id 
WHERE ol.product_id = 2

As far as I know is an inner join (aka naturual join) the best way to go in such cases. 'WHERE .. IN .. tends to be an expensive operation.

SELECT DISTINCT o.* FROM orders o
INNER JOIN orders_lines ol ON  o.order_id = ol.order_id 
WHERE ol.product_id = 2
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文