Summary:
We have a page of products and users comments that may or may not exist on each product. One table holds the product details (products_master), one table determines which product will be displayed (shoppingfeed). As we loop and output each product from query #1, query #2 is executed to pull associated comments with the current product being displayed from query #1.
Problem: This all works, but it's SLOW! We are thinking if there is a way to combine this into one optimized query that we can execute once, and then loop through it... or any other ideas to make this faster.
Query #1
SELECT shoppingfeed.action_date,
products_master.name,
products_master.image_url,
products_master.pop_sku,
products_master.group_id,
products_master.lowest_price,
products_master.highest_price,
products_master.merchant,
products_master.width,
products_master.height
FROM products_master,
shoppingfeed
WHERE ( shoppingfeed.sf_product_id = products_master.pop_sku )
ORDER BY action_date DESC
LIMIT #offset#, #maxrow#
Query #2
SELECT DISTINCT comment,
comment_id,
comments.user_id,
comment_date_time,
comment_visibility,
comments.friend_user_id,
thread_id,
alias,
first_name,
last_name,
action_id,
group_id,
users.fb_resource_id,
gender,
acct_type,
ascore
FROM comments,
users,
products_master,
user_relationship
WHERE comments.sf_product_id = #feed_item.pop_sku#
AND comments.user_id = users.user_id
AND products_master.pop_sku = #feed_item.pop_sku#
AND ( ( comments.user_id = user_relationship.sf_id
AND user_relationship.user_id = #SESSION.user_id#
AND user_relationship.relationship_status = 3 )
OR ( comments.user_id = #session.user_id#
AND user_relationship.user_id = #SESSION.user_id#
AND user_relationship.relationship_status = 99 ) )
ORDER BY comment_date_time ASC
Here is the view I put together
select
products_master.pop_skuASpop_sku,products_master.group_idASgroup_id,products_master.nameASname,products_master.image_urlASimage_url,products_master.last_updatedASlast_updated,products_master.have_it_usersAShave_it_users,products_master.want_it_usersASwant_it_users,products_master.adults_onlyASadults_only,products_master.reviewed_byASreviewed_by,products_master.donate_needed_qtyASdonate_needed_qty,products_master.inspired_usersASinspired_users,products_master.deal_users_upASdeal_users_up,products_master.deal_users_downASdeal_users_down,products_master.merchantASmerchant,products_master.merchant_logoASmerchant_logo,products_master.widthASwidth,products_master.heightASheight,products_master.added_byASadded_by,products_master.product_category_idASproduct_category_id,comments.commentAScomment,comments.comment_date_timeAScomment_date_time,comments.comment_visibilityAScomment_visibility,comments.friend_user_idASfriend_user_id,comments.user_idASuser_id,comments.action_idASaction_id,comments.comment_idAScomment_id,shoppingfeed.action_codeASaction_code,shoppingfeed.action_dateASaction_date,shoppingfeed.new_friend_idASnew_friend_id,shoppingfeed.question_idASquestion_id,users.first_nameASfirst_name,users.last_nameASlast_name,users.shopping_cloutASshopping_clout,users.genderASgender,users.fb_resource_idASfb_resource_id,comments.thread_idASthread_id,users.wish_qtyASwish_qty,merchants.logoASlogo,merchants.companynameAScompanyname,product_relationship.desirabilityASdesirabilityfrom (((((products_masterjoinshoppingfeedon((products_master.pop_sku=shoppingfeed.sf_product_id))) joincommentson((products_master.pop_sku=comments.sf_product_id))) joinuserson(((comments.user_id=users.user_id) and (comments.user_id=shoppingfeed.user_id)))) joinproduct_relationshipon((product_relationship.user_id=users.user_id))) joinmerchantson(((products_master.merchant=merchants.merchantid) and (product_relationship.sf_product_id=products_master.pop_sku)))) order bycomments.comment_date_time
EXPLAINor get an execution plan, put the results in the question as well.