Slow mysql query using Doctrine - php

To filter the products that belongs to the selected filters, I wrote this query below. When i run this in Mysql, it is very fast. But when i'm trying to execute this query in Doctrine, it is very slow. Removing the group by gives me one result and than it is fast, but it has to be 11 results.
Even the more filters i add to this statement, the slower it gets.
Can anyone help me out with this problem?
SELECT DISTINCT p.id, p.brand_id, p.name, p.permalink, p.base_model,
p.ean, p.description, p.unit, p.price, p.offer_active, p.offer_price,
GROUP_CONCAT(DISTINCT m.url ORDER BY `m`.`order`, `p`.`id` SEPARATOR ",") AS url, pcm.category_id
FROM product p
LEFT JOIN product_category pc ON pc.product_id = p.id
LEFT JOIN media m ON p.id = m.product_id
INNER JOIN product_category pcm ON p.id = pcm.product_id AND pcm.main = 1 AND pcm.product_id IS NOT NULL
LEFT JOIN product_filter_value pfv1 ON p.id = pfv1.product_id
LEFT JOIN product_filter_value pfv2 ON p.id = pfv2.product_id
LEFT JOIN product_filter_value pfv3 ON p.id = pfv3.product_id
LEFT JOIN product_filter_value pfv4 ON p.id = pfv4.product_id
WHERE (pc.category_id = 23)
AND (p.active = 1)
AND (pfv1.filter_value_id IN (2))
AND (pfv2.filter_value_id IN (8))
AND (pfv3.filter_value_id IN (22))
AND (pfv4.filter_value_id IN (38))
GROUP BY 1
ORDER BY p.top desc

Related

my query returns duplicates

Hello i have a query that selects products from a database, but for some reason it returns duplicates. The problem is in my joins i guess since in the product table there is no duplicate product. Here is my query:
$stmt=$dbh->prepare("SELECT
p.name,
p.slug,
p.id_product,
p.price,
pig.image,
c1.slug as ssubcat,
c2.slug as subcat,
c3.slug as cat
FROM
tbl_products p
INNER JOIN tbl_products_to_categories ptoc
ON ptoc.id_product = p.id_product
INNER JOIN tbl_catalog_categories c1
ON ptoc.id_category = c1.id_category
LEFT JOIN tbl_catalog_categories c2
ON c1.id_parent = c2.id_category
LEFT JOIN tbl_catalog_categories c3
ON c2.id_parent = c3.id_category
INNER JOIN tbl_products_images_gallery pig
ON pig.id_product = p.id_product
WHERE (c1.slug = :slug OR c2.slug = :slug OR c3.slug = :slug )
AND p.active = 1
AND p.quantity = 1
ORDER BY p.name ASC
LIMIT $start, $row_limit");

Mysql select from multiple tables based on like count

I have this problem with my sql statement and am not sure how to get around it.
I am using WordPress and the WTI Like Post plugin
Here is the query:
SELECT wpblog_posts.ID, wpblog_posts.post_title, wpblog_posts.post_excerpt, wpblog_posts.guid, wpblog_posts.post_author, wpblog_wti_like_post.value, wpblog_wti_like_post.post_id
FROM wpblog_posts, wpblog_wti_like_post
LEFT JOIN wpblog_term_relationships rel ON rel.object_id = wpblog_posts.ID
LEFT JOIN wpblog_term_taxonomy tax ON tax.term_taxonomy_id = rel.term_taxonomy_id
LEFT JOIN wpblog_terms t ON t.term_id = tax.term_id WHERE t.term_id = 165
AND wpblog_posts.post_type = 'post'
AND wpblog_wti_like_post.post_id = wpblog_posts.ID
ORDER BY wpblog_wti_like_post.value
LIMIT 20
Here is a screen shot of the wpblog_wti_like_post table:
So what i'm trying to do is select all posts from a category, and order those posts by wpblog_wti_like_post.value
Now in the category some posts are liked and disliked, and appear in the table wpblog_wti_like_post
but some posts don't appear in that table.
How would I:
a. Select all posts in that category
b. Order by wpblog_wti_like_post.value
I am completely stumped on how to accomplish this.
Cheers
UPDATE
This query selects all posts from a specific category and works fine, but the query above doesn't:
SELECT wpblog_posts.ID, wpblog_posts.post_title, wpblog_posts.post_excerpt, wpblog_posts.guid, wpblog_posts.post_author
FROM wpblog_posts
LEFT JOIN wpblog_term_relationships rel ON rel.object_id = wpblog_posts.ID
LEFT JOIN wpblog_term_taxonomy tax ON tax.term_taxonomy_id = rel.term_taxonomy_id
LEFT JOIN wpblog_terms t ON t.term_id = tax.term_id
WHERE t.term_id = 165
LIMIT 20
Tip: something like the following is more readable...
SELECT p.ID
, p.post_title
, p.post_excerpt
, p.guid
, p.post_author
FROM wpblog_posts p
LEFT
JOIN wpblog_term_relationships rel
ON rel.object_id = p.ID
LEFT
JOIN wpblog_term_taxonomy tax
ON tax.term_taxonomy_id = rel.term_taxonomy_id
LEFT
JOIN wpblog_terms t
ON t.term_id = tax.term_id
WHERE t.term_id = 165
LIMIT 20
Some observations on the above:
LEFT JOIN x... WHERE x = is the same as INNER JOIN x
There is no point LEFT JOINing tables from which you select no columns. As stated, the above is in fact an inner join, so this fact is for future reference only.
LIMIT without ORDER BY is fairly meaningless

MySQL join with specific WHERE clause

I have a simple database
With this query I get all users and number of products they have bought:
SELECT
c.id,
cd.name,
cd.surname,
COUNT(p.name) as bought
FROM shopping s
JOIN client c ON s.client_id = c.id
JOIN client_details cd ON c.id = cd.client_id
JOIN product p ON s.product_id = p.id
GROUP BY c.id;
It's pretty good, but I have to select all users and number of specific products bought. With this query I get it only if the user have bought at least one product:
SELECT
c.id,
cd.name,
cd.surname,
COUNT(p.name) as bought
FROM shopping s
JOIN client c ON s.client_id = c.id
JOIN client_details cd ON c.id = cd.client_id
JOIN product p ON s.product_id = p.id
WHERE p.name = 'sugar'
GROUP BY c.id;
I want to get the list with all users, and if specific user didn' buy specific product, there should be 0 on output.
Use condition aggregation:
SELECT c.id, cd.name, cd.surname,
MAX(p.name = 'sugar') as SugarFlag
FROM shopping s JOIN
client c
ON s.client_id = c.id JOIN
client_details cd
ON c.id = cd.client_id JOIN
product p
ON s.product_id = p.id
GROUP BY c.id;

How to get multidimensional array from yii 'CDbDataReader::readAll'

It is two tables in a database. The first tablse contains a list of products, and the second contains a list of options. For one product may be several options. I have built next query with yii query bulder
SELECT DISTINCT `p`.`id`,
`p`.`name`,
`pc`.`category_id`,
`c`.`name` AS `category_name`,
`ov`.`value_str` AS `option_value`
FROM `cms_product` `p`
LEFT JOIN `cms_product_category` `pc` ON p.id = pc.product_id
LEFT JOIN `cms_category` `c` ON c.id = pc.category_id
LEFT JOIN `cms_product_option_set` `pos` ON p.id = pos.product_id
LEFT JOIN `cms_option_variant` `ov` ON ov.id=pos.option_variant_id
ORDER BY `p`.`id` ASC LIMIT 100
But yii function 'CDbDataReader::readAll()' returns only one option for each product. If I execute this query in MySQL it works good. How I can get right result with yii
Use queryAll() method like below:
$sql="SELECT DISTINCT `p`.`id`,
`p`.`name`,
`pc`.`category_id`,
`c`.`name` AS `category_name`,
`ov`.`value_str` AS `option_value`
FROM `cms_product` `p`
LEFT JOIN `cms_product_category` `pc` ON p.id = pc.product_id
LEFT JOIN `cms_category` `c` ON c.id = pc.category_id
LEFT JOIN `cms_product_option_set` `pos` ON p.id = pos.product_id
LEFT JOIN `cms_option_variant` `ov` ON ov.id=pos.option_variant_id
ORDER BY `p`.`id` ASC LIMIT 100";
Then:
$result=Yii::app()->db->createCommand($sql)->queryAll();
This will return you an array with the result.

MYSQL customizable join and select

i want to use JOIN categories c ON c.id = i.category if post_type is 1 and i want to have c.title AS category_name, in SELECT otherwise JOIN categories c ON c.id = i.category and c.title AS category_name, must be not worked in query , i'm using case for join but my sql command is not correct. please help me. in all description my quastion means is how to change this below command to if post_type is 1 join must be not work
SELECT SQL_CALC_FOUND_ROWS
i. * ,
c.title AS category_name,
s.title AS status_title,
i.thumb_image,
CONCAT( u.name, ' ', u.family ) AS author
FROM contents i
LEFT JOIN categories c
ON i.category = CASE post_type WHEN 1 then c.id END
JOIN users u ON u.id = i.posted_by
JOIN status_topics s ON s.id = i.t_status
WHERE i.id = 2
I would always do the LEFT JOIN and put a condition on the column itself:
SELECT SQL_CALC_FOUND_ROWS
i. * ,
IF(i.post_type = 1, c.title, NULL) AS category_name,
s.title AS status_title,
i.thumb_image,
CONCAT( u.name, ' ', u.family ) AS author
FROM contents i
LEFT JOIN categories c ON i.category = c.id
JOIN users u ON u.id = i.posted_by
JOIN status_topics s ON s.id = i.t_status
WHERE i.id = 2

Categories