0

I'm trying to query my database for all my products and when I do that it's crashing the server and I can't get the information

SELECT categories.category_id, 
       categories.category_name, 
       categories.category_name_fr, 
       subcategories.subcategory_id, 
       subcategories.subcategory_name, 
       subcategories.subcategory_name_fr, 
       suppliers.supplier_id, 
       suppliers.supplier_name, 
       products.product_id, 
       products.product_name, 
       products.product_name_fr, 
       products.product_url 
FROM   categories, 
       subcategories, 
       suppliers, 
       products 
WHERE  products.product_active = 1 
ORDER  BY categories.category_id ASC 

I tried putting a LIMIT 0, 1 and it STILL crashed...

4
  • 3
    Define "crash"; does it crash the MySQL process? Does the PHP page return an error? What error_reporting level? What about the database? How "big" is it in term of records? And why aren't you using ("explicit") JOIN syntax? You have left out all relevant information for us to be able to help. All we can do now is guess... Commented Mar 5, 2013 at 22:54
  • Crash as in the site and everything on the server goes down and I have to wait for it to reboot. I have about 700 product records. How would I make the JOIN? I'm really new to mysql. Commented Mar 5, 2013 at 22:56
  • try select * FROM ... Commented Mar 5, 2013 at 23:00
  • 2
    @user1681542 That would do nothing to help the query and would in fact probably make it much worse depending on how many additional fields there are in these tables. Commented Mar 5, 2013 at 23:08

1 Answer 1

2

First, you should explicitly specify your joins and join conditions, as right now you are getting a Cartesian product of all the tables, which is likely not what you want.

You should list the products table first amongst the joined tables, as this is the table where your WHERE clause filtering is in effect. So something like:

SELECT categories.category_id, categories.category_name, 
    categories.category_name_fr, subcategories.subcategory_id,
    subcategories.subcategory_name, subcategories.subcategory_name_fr,
    suppliers.supplier_id, suppliers.supplier_name, products.product_id,
    products.product_name, products.product_name_fr, products.product_url
FROM products
INNER JOIN categories ON products.[some field] = categories.[some field]
INNER JOIN subcategories ON categories.[some field] = subcategories.[some field]
INNER JOIN suppliers ON products.[some field] = suppliers.[some field]
WHERE products.product_active = 1
ORDER BY categories.category_id ASC

Second, you need to make sure you have an index on each of products.product_active and categories.category_id as well as any other columns used in the join conditions.

Third, just as an FYI, a LIMIT condition would do nothing to help the query execution time here as you still need to perform the join, the filter, and the sort before deciding to limit the result set to just a subset of the items. The only place where it would help would be in processing through the result set (which would obviously happen much quicker).

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.