views:

39

answers:

2

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  A: 

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.

Shiraz Bhaiji
+2  A: 
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.

HLGEM
Thank you very much. Thats it. =)
SurfingCat