Combine 2 MySQL queries to improve performance - mysql

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

Related

SQL query works with one select but with two selects returns 0 rows

I wrote a sql syntax. When im using just one filter everything works just fine, but with more than one mysql returns 0 results. I would appreciate your help.
select
p.products_id, p.manufacturers_id, p.products_price, pd.products_name
from
products_description pd,
products_to_categories p2c,
products p left join manufacturers m on p.manufacturers_id = m.manufacturers_id
where
p.products_id in (SELECT p2pef.products_id
FROM products_to_products_extra_fields p2pef
WHERE
// filter no 1
(p2pef.products_extra_fields_id = '102' and p2pef.products_extra_fields_value = 'tak')
// filter no 2
and (p2pef.products_extra_fields_id = '94' and p2pef.products_extra_fields_value = 'tak')
// no xx
)
and p.products_status = '1'
and p.products_id = pd.products_id
and p.products_id = p2c.products_id
and pd.language_id = '4'
and p2c.categories_id = '26'
group by p.products_id
----edit
I think that i moved somehere, but this syntax returns just one record instead of many more:
select
p.products_id, pd.products_name
from
products_description pd,
products_to_categories p2c,
products p
where
EXISTS (SELECT p2pef.products_id
FROM products_to_products_extra_fields p2pef
WHERE p2pef.products_extra_fields_id = '78'
and p2pef.products_extra_fields_value = 'tak'
and p.products_id = p2pef.products_id )
and
EXISTS (SELECT p2pef.products_id
FROM products_to_products_extra_fields p2pef
WHERE p2pef.products_extra_fields_id = '94'
and p2pef.products_extra_fields_value = 'tak'
and p.products_id = p2pef.products_id )
and p.products_status = '1'
and p.products_id = pd.products_id
and p.products_id = p2c.products_id
and pd.language_id = '4'
and p2c.categories_id = '26'
group by p.products_id
This is the same query, I replaced the AND with the OR, give it a try
Edit: using information from the comments, then use and IN, it's way easier as the path you're taking
SELECT
p.products_id, p.manufacturers_id, p.products_price, pd.products_name
FROM
products_description pd,
products_to_categories p2c,
products p
LEFT JOIN manufacturers m ON p.manufacturers_id =
m.manufacturers_id
WHERE
p.products_id IN (SELECT p2pef.products_id
FROM products_to_products_extra_fields p2pef
WHERE
// filter no 1
(
p2pef.products_extra_fields_id IN ('94', '102')
AND p2pef.products_extra_fields_value = 'tak'
)
)
AND p.products_status = '1'
AND p.products_id = pd.products_id
AND p.products_id = p2c.products_id
AND pd.language_id = '4'
AND p2c.categories_id = '26'
GROUP BY p.products_id
It looks like you are trying to search for two different p2pef.products_extra_fields_id at the same time time. Try using OR between your filters instead of AND. Let me know if it helps you.

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()

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

How to verify two distinct values belong to a field in MySQL

I have the following MySQL query. As you can see in the last line, it asks for the filter_id to be either 51 or 8.
How can I modify this query to make sure the products that are returned are only products that have BOTH filter IDs.
What I did: I tried adding HAVING COUNT(DISTINCT pf.filter_id) >=2 based on other answers in this forum but it did not help.
SELECT COUNT(DISTINCT p.product_id) AS total
FROM ocb3l_product_to_category p2c
LEFT JOIN ocb3l_product_filter pf ON (p2c.product_id = pf.product_id)
LEFT JOIN ocb3l_product p ON (pf.product_id = p.product_id)
LEFT JOIN ocb3l_product_description pd ON (p.product_id = pd.product_id)
LEFT JOIN ocb3l_product_to_store p2s ON (p.product_id = p2s.product_id)
WHERE pd.language_id = '1'
AND p.status = '1'
AND p.date_available <= NOW()
AND p2s.store_id = '0'
AND p2c.category_id = '146'
AND pf.filter_id IN (51,8)
P.S. This query is used in an OpenCart shop to get the total number of products on the category pages.
The HAVING COUNT(DISTINCT pf.filter_id) >=2 clause will work, if you were to GROUP BY the product_id.
One approach is to use an inline view (derived table) so that this check can be performed for each product_id. The result from that can be joined to the other tables.
For example:
SELECT COUNT(DISTINCT fp.product_id) AS total
FROM (
SELECT p.product_id
FROM ocb3l_product p
JOIN ocb3l_product_filter pf
ON pf.product_id = p.product_id
AND pf.filter_id IN (51,8)
WHERE p.status = '1'
AND p.date_available <= NOW()
GROUP BY p.product_id
HAVING COUNT(DISTINCT pf.filter_id) = 2
) fp
JOIN ocb3l_product_to_category p2c
ON p2c.product_id = fp.product_id
AND p2c.category_id = '146'
JOIN ocb3l_product_to_store p2s
ON p2s.product_id = fp.product_id
AND p2s.store_id = '0'
JOIN ocb3l_product_description pd
ON pd.product_id = fp.product_id
AND pd.language_id = '1'
What you need to do is add another JOINed copy of the product_filter table like this:
SELECT COUNT(DISTINCT p.product_id) AS total
FROM ocb3l_product_to_category p2c
LEFT JOIN ocb3l_product_filter pf ON (p2c.product_id = pf.product_id)
LEFT JOIN ocb3l_product_filter pf2 ON (p2c.product_id = pf2.product_id) /*new*/
LEFT JOIN ocb3l_product p ON (pf.product_id = p.product_id)
LEFT JOIN ocb3l_product_description pd ON (p.product_id = pd.product_id)
LEFT JOIN ocb3l_product_to_store p2s ON (p.product_id = p2s.product_id)
WHERE pd.language_id = '1'
AND p.status = '1'
AND p.date_available <= NOW()
AND p2s.store_id = '0'
AND p2c.category_id = '146'
AND pf.filter_id = 51 /*used to be IN (51,8)*/
AND pf2.filter_id = 8 /*new*/

Need help with a mysql query

I've developed a mysql query that tells me how many times each product was purchased over a specified timeframe. I'm trying to figure out how to join this with my pageview tracking table so I can calculate the conversion rate for each product. Here is the original query:
SELECT SUM( op.products_quantity ) AS num_sold, SUM( op.final_price * op.products_quantity ) AS total_sales, p.products_model, pd.products_name, p.products_id
FROM orders_products AS op
JOIN products AS p
JOIN products_description AS pd
JOIN orders as o
WHERE p.products_id = op.products_id
AND p.products_id = pd.products_id
AND op.orders_id = o.orders_id
AND o.date_purchased BETWEEN '2011-01-15' AND '2011-04-15"'
GROUP BY p.products_id
ORDER BY total_sales DESC
I have another query that gives me the page views per product:
SELECT pv.products_id, count( pv.timestamp )
FROM products_visits AS pv
WHERE pv.timestamp
BETWEEN '2011-01-15'
AND '2011-04-17'
GROUP BY products_id
The caveat is that the views data has just started being collected, so we want return results even if a given product_id is not in the views table, but is in the purchases table.
How do I combine those queries into a single query?
You need a LEFT OUTER JOIN against a subquery. I think this will do the job:
SELECT
SUM( op.products_quantity ) AS num_sold,
SUM( op.final_price * op.products_quantity ) AS total_sales,
p.products_model, pd.products_name,
p.products_id,
visits.visits
FROM orders_products AS op
JOIN products AS p
JOIN products_description AS pd
JOIN orders as o
LEFT OUTER JOIN (
SELECT pv.products_id, count( pv.timestamp ) AS visits
FROM products_visits AS pv
WHERE pv.timestamp BETWEEN '2011-01-15' AND '2011-04-17'
GROUP BY products_id
) visits ON p.products_id = visits.products_id
WHERE p.products_id = op.products_id
AND p.products_id = pd.products_id
AND op.orders_id = o.orders_id
AND o.date_purchased BETWEEN '2011-01-15' AND '2011-04-15"'
GROUP BY p.products_id
ORDER BY total_sales DESC
You will need to use a LEFT JOIN: "first" you get all products and their sales data, and then you join the views data, if it exists.
try this:
SELECT p.products_id,
count( pv.timestamp ) AS views,
p.products_model AS model,
SUM( op.products_quantity ) AS num_sold,
SUM( op.final_price * op.products_quantity ) AS total_sales,
pd.products_name
FROM products AS p
LEFT JOIN products_visits AS pv
ON pv.products_id = p.products_id
AND pv.timestamp BETWEEN '2011-01-15' AND '2011-04-17'
JOIN orders_products AS op
ON p.products_id = op.products_id
JOIN products_description AS pd
ON p.products_id = pd.products_id
JOIN orders as o
ON op.orders_id = o.orders_id
AND o.date_purchased BETWEEN '2011-01-15' AND '2011-04-17'
GROUP BY p.products_id
ORDER BY total_sales DESC