SQL Inner Join : DB stuck

|▌冷眼眸甩不掉的悲伤 提交于 2019-12-24 17:24:58

问题


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?


回答1:


SELECT 
op.orders_id, 
p.products_id, 
op.products_name, 
op.products_price, 
op.products_quantity 
FROM orders_products op   
INNER JOIN products p
ON op.products_id = p.products_id 
WHERE p.manufacturers_id = 1 AND 
p.orders_id > 10000 

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.




回答2:


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.



来源:https://stackoverflow.com/questions/2484518/sql-inner-join-db-stuck

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!