SQL 内部联接:数据库卡住
我几天前发布了这个问题,但我没有准确解释我想要什么。 我再次提出更好表述的问题: 为了澄清我的问题,我添加了一些新信息:
我有一个带有 MyISAM 表的 MySQL 数据库。两个相关表格是:(
* orders_products: orders_products_id, orders_id, product_id, product_name, product_price, product_name, product_model, final_price, ...
* products: products_id, manufacturers_id, ...
有关表格的完整信息,请参阅屏幕截图产品(屏幕截图)和屏幕截图orders_products(屏幕截图))
现在我想要的是这个: - 获取所有订购了Manufacturer_id = 1产品的订单。以及该订单(Manufacturer_id = 1)产品的产品名称。按订单分组。
到目前为止我所做的是:
SELECT
op.orders_id,
p.products_id,
op.products_name,
op.products_price,
op.products_quantity
FROM orders_products op , products p
INNER JOIN products
ON op.products_id = p.products_id
WHERE p.manufacturers_id = 1 AND
p.orders_id > 10000
p.orders_id > 10000 用于测试仅获取几个 order_id。但是,即使该查询有效,也需要很长时间才能执行。 sql server有两次卡住了。错误在哪里?
I postet this question a few days ago but I didn't explain exactly what I want.
I ask the question better formulated again:
To clarify my problem I added some new information:
I got an MySQL DB with MyISAM tables. The two relevant tables are:
* orders_products: orders_products_id, orders_id, product_id, product_name, product_price, product_name, product_model, final_price, ...
* products: products_id, manufacturers_id, ...
(for full information about the tables see screenshot products (Screenshot) and screenshot orders_products (Screenshot))
Now what I want is this: - Get all Orders who ordered products with manufacturers_id = 1. And the product name of the product of this order (with manufacturers_id = 1). Grouped by orders.
What I did so far is this:
SELECT
op.orders_id,
p.products_id,
op.products_name,
op.products_price,
op.products_quantity
FROM orders_products op , products p
INNER JOIN products
ON op.products_id = p.products_id
WHERE p.manufacturers_id = 1 AND
p.orders_id > 10000
p.orders_id > 10000 for testing to get only a few order_id's. But thies query takes much time to get executed if it even works. Two times the sql server stucked. Where is the mistake?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您在产品表上同时具有隐式连接和显式连接(请注意不要再使用隐式连接语法,这是一种非常糟糕的编程实践)并且查看代码,我怀疑您正在获得交叉连接。
YOu had both implicit and explict joins on the products table (Make a note to never again use the implicit join syntax, it is a very bad programmin practice) and looking at the code, I suspect you were getting a cross join.
如果没有任何索引,则选择可能会生成许多表扫描。
您可能会返回一个非常大的结果集,这可能会填满临时区域,这可能会导致卡住。
If you do not have any indexes then the select may be generating many table scans.
It may be that you are getting back a very large result set, that could fill up the temp area, which could explain getting stuck.