0

I have two mysql tables like bellow:

table_category
-----------------
id | name | type    
1  | A    | Cloth     
2  | B    | Fashion    
3  | C    | Electronics    
4  | D    | Electronics

table_product
------------------
id | cat_cloth | cat_fashion | cat_electronics    
1  | 1         | 2           | 3    
1  | NULL      | 2           | 4

Here cat_cloth, cat_fashion, cat_electronics is ID from table_category

It is better to have another table for category type but I need a quick solution for now.

I want to get list of categories with total number of products. I wrote following query:

SELECT table_category.*, table_product.id, COUNT(table_product.id) as count
FROM table_category
LEFT JOIN table_product` ON table_category.id = table_product.cat_cloth 
    OR table_category.id = table_product.cat_fashion 
    OR table_category.id = table_product.cat_electronis
GROUP BY table_product.id
ORDER BY table_product.id ASC

Question: The sql I wrote it works but I have more then 14K categories and 50K products and the sql works very slow. I added index for cat_* ids but no improvement. My question how can I optimize this query?

I found the query takes 3-4 minutes to process the volume of data I mentioned. I want to reduce the execution time.

Best Regards

7
  • Performance questions should include EXPLAIN ANALYZE and some information about table size, index, current time performance, desire time, etc. Slow is a relative term and we need a real value to compare. MySQL Also Please read How-to-Ask Commented Dec 8, 2016 at 13:09
  • @Mike Andrew - can you please post some sample data on sqlfiddle.com to test the queries ? Commented Dec 8, 2016 at 13:13
  • @e4c5 I am not sure how to benchmark but I found the query takes 3-4 minutes to process the amount of data I mentioned. I want to reduce the execution time Commented Dec 8, 2016 at 13:27
  • so post that in your question, along with the output of explain and preferable the show create tables. Commented Dec 8, 2016 at 13:29
  • 1
    I have voted to close this since you have made no effort to provide required information despite two requests from me and one from @BerndBuffen Commented Dec 8, 2016 at 13:35

1 Answer 1

1

As far as I can say every "OR" either in "ON" or "WHERE" part is very cost expensive. It will sound very stupid but I would recommend you to make 3 separate small selects combined together with UNION ALL. This we do with similar problems both in mysql and postgresql and in some cases when we got "resources exceeded" we had to do it also for bigquery. So it is very stupid and you will have more work but it certainly works and it is much quicker in producing results then many "OR"s.

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

1 Comment

yes it is expensive and I am looking for a solution for this. But UNION ALL too expensive regarding my scenario. I tried UNION ALL too before posting and it takes tame multiple by 3 then current execution time. Thanks for your reply

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.