Why is MySQL select count(*) so slow on fast subquery? - mysql

I have the following query that executes quite fast:
SELECT DISTINCT p.products_id,
p.products_image,
p.products_quantity,
p.products_status,
m.manufacturers_id,
p.products_date_added,
p.products_subimage1,
pd.products_name,
p.products_price,
p.products_length,
p.products_width,
p.products_height,
p.products_tax_class_id,
IF(s.status, s.specials_new_products_price, NULL) AS
specials_new_products_price,
IF(s.status, s.specials_new_products_price, p.products_price) AS
final_price,
IF(clearance_price < products_cost * 2.25,
clearance_price,
products_cost * 2.25) AS
sorting_price
FROM
(SELECT products_id ,
IF(clearance_price < products_cost * 2.25,
clearance_price,
products_cost * 2.25) AS
sorting_price
FROM `products`
ORDER BY products_id DESC)q,
products p
left join manufacturers m USING(manufacturers_id)
left join specials s
ON p.products_id = s.products_id
left join products_attributes pa
ON p.products_id = pa.products_id
left join products_options po
ON pa.options_id = po.products_options_id
left join products_options_values pov
ON pa.options_values_id = pov.products_options_values_id,
products_description pd,
categories c,
products_to_categories p2c
WHERE
q.products_id = p.products_id
AND q.sorting_price = sorting_price
AND
p.products_status = '1'
AND p.products_id = pd.products_id
AND pd.language_id = '1'
AND p.products_id = p2c.products_id
AND p2c.categories_id = c.categories_id
AND (( pd.products_name LIKE '%a%'
OR po.products_options_name LIKE '%a%'
OR pov.products_options_values_name LIKE '%a%'
OR pd.products_description LIKE '%a%' ))
When I wrap it inside a count(*) query, the new count query takes 10 to 15 times more time, very slow.
I wrap it like so:
SELECT count(*) as total from (
SELECT DISTINCT p.products_id,
p.products_image,
p.products_quantity,
p.products_status,
m.manufacturers_id,
p.products_date_added,
p.products_subimage1,
pd.products_name,
p.products_price,
p.products_length,
p.products_width,
p.products_height,
p.products_tax_class_id,
IF(s.status, s.specials_new_products_price, NULL) AS
specials_new_products_price,
IF(s.status, s.specials_new_products_price, p.products_price) AS
final_price,
IF(clearance_price < products_cost * 2.25,
clearance_price,
products_cost * 2.25) AS
sorting_price
FROM
(SELECT products_id ,
IF(clearance_price < products_cost * 2.25,
clearance_price,
products_cost * 2.25) AS
sorting_price
FROM `products`
ORDER BY products_id DESC)q,
products p
left join manufacturers m USING(manufacturers_id)
left join specials s
ON p.products_id = s.products_id
left join products_attributes pa
ON p.products_id = pa.products_id
left join products_options po
ON pa.options_id = po.products_options_id
left join products_options_values pov
ON pa.options_values_id = pov.products_options_values_id,
products_description pd,
categories c,
products_to_categories p2c
WHERE
q.products_id = p.products_id
AND q.sorting_price = sorting_price
AND
p.products_status = '1'
AND p.products_id = pd.products_id
AND pd.language_id = '1'
AND p.products_id = p2c.products_id
AND p2c.categories_id = c.categories_id
AND (( pd.products_name LIKE '%a%'
OR po.products_options_name LIKE '%a%'
OR pov.products_options_values_name LIKE '%a%'
OR pd.products_description LIKE '%a%' ))
) AS derivedtable1
Why does this happen? Is there any way to optimize this?
EDIT:
This is the EXPLAIN EXTENDED of the first query:
This is the EXPLAIN EXTENDED of the count (the second) query:
This is the PROFILING of the first query:
This is the PROFILING of the count (the second) query:

FROM pd
LEFT JOIN po ON ...
WHERE ( ... OR po.x LIKE '...' OR ... )
The semantics of LEFT say that it does not matter whether there is a matching row in po. Hence the LIKE has zero impact on the outcome of the query. Suggest you get rid of OR ... LIKE of any items in LEFT. Or maybe you did not really mean LEFT? That could make it run faster, but the result set could be different.
I see from the EXPLAINs that there is no good excuse for the difference in timing. Oh, how are you running the queries? The first one has 213K rows of output -- are you waiting for all of it to come back before stopping your stopwatch?

Related

Unknown column 'pov.products_options_id' in 'having clause' error in having mysql query

SELECT DISTINCT p.products_image,
p.products_quantity,
m.manufacturers_id,
p.products_id,
pd.products_name,
p.products_price,
p.products_tax_class_id,
p.products_price_sorter,
p.products_qty_box_status,
p.master_categories_id
FROM products p
LEFT JOIN manufacturers m USING(manufacturers_id)
LEFT JOIN products_description pd ON p.products_id = pd.products_id
JOIN products_to_categories p2c ON p.products_id = p2c.products_id
JOIN categories c ON p2c.categories_id = c.categories_id
LEFT JOIN meta_tags_products_description mtpd ON mtpd.products_id= p2c.products_id
AND mtpd.language_id = 2
JOIN products_attributes p2a ON p.products_id = p2a.products_id
JOIN products_options po ON p2a.options_id = po.products_options_id
JOIN products_options_values pov ON p2a.options_values_id = pov.products_options_values_id
WHERE (p.products_status = 1
AND pd.language_id = 2
AND ((pd.products_name LIKE '%t%'
OR p.products_model LIKE '%t%'
OR m.manufacturers_name LIKE '%t%'
OR (mtpd.metatags_keywords LIKE '%t%'
AND mtpd.metatags_keywords !='')
OR (mtpd.metatags_description LIKE '%t%'
AND mtpd.metatags_description !='')
OR pd.products_description LIKE '%t%')))
GROUP BY p.products_id
HAVING sum(pov.products_options_id IN (12, 20)
AND pov.products_options_values_id IN (91,82)) > 0
ORDER BY p.products_sort_order,
pd.products_name;
change HAVING
from
HAVING sum(pov.products_options_id IN (12, 20)
AND pov.products_options_values_id IN (91,82)) > 0
to
HAVING pov.products_options_id IN (12, 20)
AND pov.products_options_values_id IN (91,82)
Why you using > 0 ? you are using IN()

MySQL avg and left join

Is there a way to combine these two queries? Adding another left join didn't work.
select distinct
p.products_id,
p.products_image,
pd.products_name,
m.manufacturers_name,
p.manufacturers_id,
p.products_price,
p.products_tax_class_id,
IF(s.status, s.specials_new_products_price, NULL) as specials_new_products_price,
IF(s.status, s.specials_new_products_price, p.products_price) as final_price
from products_description pd,
products p
left join manufacturers m on p.manufacturers_id = m.manufacturers_id
left join specials s on p.products_id = s.products_id, products_to_categories p2c
where
p.products_status = '1'
and p.products_id = p2c.products_id
and pd.products_id = p2c.products_id
and pd.language_id = '1'
and p2c.categories_id = '17'
order by p.sort_order
//-------------------
select avg(reviews_rating) as average_rating from reviews where
products_id = '31' and reviews_status = '1'
The first one is getting product details and the second one is getting the average rating from reviews table.
You may try it-
SELECT p.products_id, p.products_image, pd.products_name, m.manufacturers_name, p.manufacturers_id, p.products_price, p.products_tax_class_id, IF(s.status, s.specials_new_products_price, NULL) AS specials_new_products_price, IF(s.status, s.specials_new_products_price, p.products_price) AS final_price, AVG(rvw.reviews_rating) AS average_ratings
FROM products p
JOIN products_to_categories p2c ON p.products_id = p2c.products_id
JOIN products_description pd ON pd.products_id = p2c.products_id
LEFT JOIN manufacturers m ON p.manufacturers_id = m.manufacturers_id
LEFT JOIN specials s ON p.products_id = s.products_id
LEFT JOIN reviews rvw ON rvw.products_id=p.products_id
WHERE p.products_status = '1' AND pd.language_id = '1' AND p2c.categories_id = '17'
GROUP BY p.products_id
ORDER BY p.sort_order;
Note: Always first mention all inner/comma join tables then left join.
How about just put the second query as select item in the first query?
select
distinct p.products_id, p.products_image, pd.products_name,
m.manufacturers_name, p.manufacturers_id, p.products_price, p.products_tax_class_id,
IF(s.status, s.specials_new_products_price, NULL) as specials_new_products_price,
IF(s.status, s.specials_new_products_price, p.products_price) as final_price,
(select avg(reviews_rating) as average_rating from reviews where products_id = '31' and reviews_status = '1') as average_rating
from products_description pd, products p left join manufacturers m on p.manufacturers_id = m.manufacturers_id left join specials s on p.products_id = s.products_id, products_to_categories p2c
where p.products_status = '1' and p.products_id = p2c.products_id and pd.products_id = p2c.products_id and pd.language_id = '1' and p2c.categories_id = '17'
order by p.sort_order
M no great in queries, but will give a try.
First, I would like to know if there is a relationship between the tables 'REVIEWS' and 'PRODUCT_DESCRIPTION'. If Yes, then your combined query could be :
select distinct p.products_id, p.products_image, pd.products_name, m.manufacturers_name, p.manufacturers_id, p.products_price, p.products_tax_class_id, IF(s.status, s.specials_new_products_price, NULL) as specials_new_products_price, IF(s.status, s.specials_new_products_price, p.products_price) as final_price , avg(rv.reviews_rating) as average_rating from products_description pd, reviews rv , manufacturers m, products p, specials s, products_to_categories p2c where pd.products_id = rv.products_id and p.manufacturers_id = m.manufacturers_id and p.products_id = s.products_id and p.products_status = '1' and p.products_id = p2c.products_id and pd.products_id = p2c.products_id and pd.language_id = '1' and p2c.categories_id = '17' and rv.products_id = '31' and rv.reviews_status = '1' order by p.sort_order;

Slow performance of combined MySQL queries

I have this query with multiple subqueries which runs quite slow.
SELECT DISTINCT pav.products_options_values_id,
pav.products_options_values_name,
pav.products_options_values_sort_order
FROM products_stock ps,
products_options_values pav,
(
SELECT DISTINCT pa.products_id,
pov.products_options_values_id,
pov.products_options_values_name,
pa.options_values_price,
pa.price_prefix
FROM products_attributes pa,
products_options_values pov,
(
SELECT DISTINCT p.products_image,
p.products_quantity,
p.products_status,
m.manufacturers_id,
p.products_id,
p.products_date_added,
p.products_subimage1,
pd.products_name,
p.products_price,
p.products_length,
p.products_width,
p.products_height,
p.products_tax_class_id,IF(s.status, s.specials_new_products_price, NULL) AS specials_new_products_price,
IF(s.status, s.specials_new_products_price, p.products_price) AS final_price,
IF(p.clearance_price < p.products_cost*2.25, p.clearance_price, p.products_cost*2.25) AS sorting_price
FROM products p
LEFT JOIN manufacturers m
using (manufacturers_id)
LEFT JOIN specials s
ON p.products_id = s.products_id
LEFT JOIN products_attributes pa
ON p.products_id = pa.products_id
LEFT JOIN products_options po
ON pa.options_id = po.products_options_id
LEFT JOIN products_options_values pov
ON pa.options_values_id = pov.products_options_values_id ,
products_description pd,
categories c,
products_to_categories p2c
WHERE p.products_status = '1'
AND p.products_id = pd.products_id
AND pd.language_id = '1'
AND p.products_id = p2c.products_id
AND p2c.categories_id = c.categories_id
AND ((
pd.products_name LIKE '%a%'
OR po.products_options_name LIKE '%a%'
OR pov.products_options_values_name LIKE '%a%'
OR pd.products_description LIKE '%a%') )
ORDER BY p.products_id DESC) m
WHERE m.products_id = pa.products_id
AND pa.options_id = 1
AND pa.options_values_id = pov.products_options_values_id
AND pov.language_id = '1'
GROUP BY pov.products_options_values_id
ORDER BY pov.products_options_values_sort_order ASC) q
WHERE q.products_id = ps.products_id
AND ps.products_stock_attributes = concat('1-', pav.products_options_values_id)
AND ps.products_stock_quantity > 0
ORDER BY pav.products_options_values_sort_order ASC
This is EXPLAIN result:
Been trying to optimize it for hours, but I probably misread the EXPLAIN information because no matter what I do, it doesn't seem to make it faster, so I am reaching out for help from the experts here.
What can be the cause of it becoming so slow and what should I do to make it fast?
First, I cleaned up the query using consistent JOIN clauses vs comma listed tables. Next, your inner-most query has an order by which apparently is not being utilized to any benefit. Order by will typically cause big delay in queries if it cant be optimized.
SELECT DISTINCT
pav.products_options_values_id,
pav.products_options_values_name,
pav.products_options_values_sort_order
FROM
products_stock ps
JOIN products_options_values pav
ON ps.products_stock_attributes = concat('1-', pav.products_options_values_id)
AND ps.products_stock_quantity > 0
JOIN ( SELECT DISTINCT
pa.products_id,
pov.products_options_values_id,
pov.products_options_values_name,
pa.options_values_price,
pa.price_prefix
FROM
products_attributes pa
JOIN products_options_values pov
ON pa.options_values_id = pov.products_options_values_id
AND pov.language_id = '1'
JOIN ( SELECT DISTINCT
p.products_image,
p.products_quantity,
p.products_status,
p.manufacturers_id,
p.products_id,
p.products_date_added,
p.products_subimage1,
pd.products_name,
p.products_price,
p.products_length,
p.products_width,
p.products_height,
p.products_tax_class_id,
IF(s.status, s.specials_new_products_price, NULL )
AS specials_new_products_price,
IF(s.status, s.specials_new_products_price, p.products_price )
AS final_price,
IF( p.clearance_price < p.products_cost * 2.25, p.clearance_price, p.products_cost * 2.25 )
AS sorting_price
FROM
products p
JOIN products_description pd
ON p.products_id = pd.products_id
AND pd.language_id = '1'
THIS SECTION CAN BE REMOVED JOIN products_to_categories p2c
AND p.products_id = p2c.products_id
JOIN categories c
ON p2c.categories_id = c.categories_id
LEFT JOIN manufacturers m
UP TO THIS LINE ON P.manufacturers_id = m.manufacturers_id
LEFT JOIN specials s
ON p.products_id = s.products_id
LEFT JOIN products_attributes pa
ON p.products_id = pa.products_id
LEFT JOIN products_options po
ON pa.options_id = po.products_options_id
LEFT JOIN products_options_values pov
ON pa.options_values_id = pov.products_options_values_id,
WHERE
p.products_status = '1'
AND ( pd.products_name LIKE '%a%'
OR pd.products_description LIKE '%a%'
OR po.products_options_name LIKE '%a%'
OR pov.products_options_values_name LIKE '%a%' )
ORDER BY
p.products_id DESC) m
WHERE
pa.products_id = m.products_id
AND pa.options_id = 1
GROUP BY
pov.products_options_values_id
ORDER BY
pov.products_options_values_sort_order ASC) q
ON ps.products_id = q.products_id
ORDER BY
pav.products_options_values_sort_order ASC
You have a join to your categories table, but not pulling any values or other criteria to return columns. I have thus removed as it is unnecessary. WITH that gone, I then looked at your Products_To_Categories table AND that too has no bearing in the query other than an extra join not being used anywhere else and thus removed. The Manufacturers table is also not required as all you are getting is the manufacturer's ID, which exists on the products table, so yet ANOTHER table not required
Now on to indexes. I would look at the following indexes for your tables
table index
products ( products_status, products_id )
products_description ( products_id, language_id )
products_to_categories ( products_id, categories_id )
categories ( categories_id )
manufacturers ( manufacturers_id )
specials ( products_id, status, specials_new_products_price )
products_attributes ( products_id, options_id, options_values_id )
products_options ( products_options_id, products_options_name )
products_options_values ( products_options_values_id, products_options_values_name )
This is just a first pass at the review. Now I want to take it one additional step to possibly remove a layer of nested queries. Your INNER-MOST query does a join to "products_options_values" but ONLY for Language ID = 1. Why would you not add that column criteria to the inner query. Then add the pov.products_options_values_id and pov.products_options_values_name columns to your inner query and language ID to the inner query and you don't need to re-join to them again. They can be referenced via the "m." alias of the inner-most query. vs the rejoin.
Similarly, your inner-most query does a LEFT JOIN to the products_attributes, but outside the "m" alias query result applies a WHERE clause ONLY of options_id = 1. This doesn't make sense. Why not just add that too to the inner-most query. Eliminate the extra levels would probably help a lot as you are limiting down a pull of all records if you are only looking for things like Language ID = 1, or Options_ID = 1.
Again, this formatted query IS the same context as yours, just structured slightly different and shorter indentation to see better the context and nested requirements.
FEEDBACK FROM CHAT
select
PQ.*
from
( SELECT STRAIGHT_JOIN DISTINCT
p.products_id,
p.products_image,
p.products_quantity,
p.products_status,
p.manufacturers_id,
p.products_date_added,
p.products_subimage1,
pd.products_name,
p.products_price,
p.products_length,
p.products_width,
p.products_height,
pov.products_options_values_id,
pov.products_options_values_name,
pov.language_id,
pov.products_options_values_sort_order,
p.products_tax_class_id,
IF(s.status, s.specials_new_products_price, NULL)
AS specials_new_products_price,
IF(s.status, s.specials_new_products_price, p.products_price)
AS final_price,
IF(p.clearance_price < p.products_cost*2.25, p.clearance_price, p.products_cost*2.25)
AS sorting_price
FROM
products_attributes pa
JOIN products_options_values pov
ON pa.options_values_id = pov.products_options_values_id
AND pov.language_id = '1'
LEFT JOIN products_options po
ON pa.options_id = po.products_options_id
JOIN products p
ON pa.products_id = p.products_id
AND p.products_status = '1'
JOIN products_description pd
ON p.products_id = pd.products_id
AND pd.language_id = '1'
JOIN products_to_categories p2c
ON p.products_id = p2c.products_id
JOIN categories c
ON p2c.categories_id = c.categories_id
LEFT JOIN manufacturers m
using (manufacturers_id)
LEFT JOIN specials s
ON p.products_id = s.products_id
WHERE
pa.options_id = '1'
AND ( pd.products_name LIKE '%a%'
OR po.products_options_name LIKE '%a%'
OR pov.products_options_values_name LIKE '%a%'
OR pd.products_description LIKE '%a%')) PQ
order by
PQ.Products_id
Also, note, I removed the final "Order by" clause

Combine 2 MySQL queries to improve performance

I have the following 2 queries.
The first is finding some product_ids based on some joins. It's running fast.
SELECT Group_concat(DISTINCT( p.products_id )) AS comma_separated
FROM products p
left join specials s
ON p.products_id = s.products_id
left join products_to_categories p2c
ON p.products_id = p2c.products_id
left join products_description pd
ON p.products_id = pd.products_id
inner join products_attributes pa
ON p.products_id = pa.products_id
WHERE p.products_status = '1'
AND Date_sub(Curdate(), interval 7000 day) <= p.products_date_added
ORDER BY p.products_id DESC
After that, the following query is executed and use all the products_ids found in the first query:
SELECT DISTINCT pov.products_options_values_id,
pov.products_options_values_name,
pa.options_values_price,
pa.price_prefix
FROM products_attributes pa,
products_options_values pov
WHERE Find_in_set(pa.products_id,
'**Long list of comma separated products_ids found in the first query**')
AND pa.options_id = 1
AND pa.options_values_id = pov.products_options_values_id
AND pov.language_id = '1'
GROUP BY pov.products_options_values_id
ORDER BY pov.products_options_values_sort_order ASC
The list of products_ids in the Find_in_set clause is very long and making this query take quite a long time to execute.
Is there any way of using joins or other way to rewrite the second query or combine both to improve performance?
Try something like this
SELECT DISTINCT pov.products_options_values_id,
pov.products_options_values_name,
pa.options_values_price,
pa.price_prefix
FROM products_attributes pa,
products_options_values pov,
(SELECT p.products_id
FROM products p
left join specials s
ON p.products_id = s.products_id
left join products_to_categories p2c
ON p.products_id = p2c.products_id
left join products_description pd
ON p.products_id = pd.products_id
inner join products_attributes pa
ON p.products_id = pa.products_id
WHERE p.products_status = '1'
AND Date_sub(Curdate(), interval 7000 day) <= p.products_date_added
GROUP BY p.products_id
) t
WHERE t.products_id = pa.products_id
AND pa.options_id = 1
AND pa.options_values_id = pov.products_options_values_id
AND pov.language_id = '1'
GROUP BY pov.products_options_values_id
ORDER BY pov.products_options_values_sort_order ASC

Use limit in sub query

I have 4 tables
product
status
orders
orders_products.
Using following query I get the proper result but it is not in correct order i.e. result gets by sub query.I want result order as sub query result.
SELECT
p.products_id, p.products_image, p.products_tax_class_id,
pd.products_name, if(s.status, s.specials_new_products_price,
p.products_price) as products_price
FROM
products p LEFT JOIN specials s
ON p.products_id = s.products_id, products_description pd
WHERE
p.products_status = '1'
AND p.products_id = pd.products_id
AND pd.language_id = '2'
AND p.products_id in (
SELECT * from (
SELECT
distinct(op.products_id)
FROM
orders_products op,orders o
WHERE
op.orders_id=o.orders_id
AND o.customers_id='27'
ORDER BY
o.date_purchased
DESC LIMIT 0,10
) AS temptable
)
The outer SELECT has an ORDER BY pd.products_name. That's what should apply, and only that.
The sub-query ORDER BY shouldn't actually matter. You should get the same result removing it.
If you want a different sort order from pd.products_name, you have to ORDER BY the column required. In your case, you need to move orders.date_purchased to the main part of the query so you can use it to sort. Inverting the query, i.e. starting with what you want and relating everything else:
SELECT MAX(o.date_purchased) AS date_purchased, DISTINCT (op.products_id) AS products_id
p.products_id, p.products_image, p.products_tax_class_id, p.products_price
pd.products_name,
IF (s.status, s.specials_new_product_price, p.products_price) AS products_price
FROM orders o
LEFT JOIN orders_products op USING (orders_id)
LEFT JOIN products p USING (products_id)
LEFT JOIN products_description pd USING (products_id)
LEFT JOIN specials s USING (products_id)
WHERE o.customers_id = '27'
AND p.products_status = 1
AND pd.language_id = 2