概括:
我们有一个产品页面和用户评论,每个产品可能存在也可能不存在。一张表保存产品详细信息(products_master),一张表确定将显示哪个产品(shoppingfeed)。当我们从查询 #1 循环和输出每个产品时,执行查询 #2 以提取与从查询 #1 显示的当前产品相关的评论。
问题:这一切都有效,但速度很慢!我们正在考虑是否有一种方法可以将其组合成一个优化的查询,我们可以执行一次,然后循环遍历它......或任何其他想法来加快速度。
查询 #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#
查询 #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
这是我放在一起的视图
选择
products_master。pop_sku作为pop_sku,products_master。group_id作为group_id,products_master。name作为name,products_master。image_url作为image_url,products_master。last_updated作为last_updated,products_master。have_it_users作为have_it_users,products_master。want_it_users作为want_it_users,products_master。adults_only作为adults_only,products_master。reviewed_by作为reviewed_by,products_master。donate_needed_qty作为donate_needed_qty,products_master。inspired_users作为inspired_users,products_master。deal_users_up作为deal_users_up,products_master。deal_users_down作为deal_users_down,products_master。merchant作为merchant,products_master。merchant_logo作为merchant_logo,products_master。width作为width,products_master。height作为height,products_master。added_by作为added_by,products_master.product_category_id作为product_category_id,comments。comment作为comment,comments。comment_date_time作为comment_date_time,comments。comment_visibility作为comment_visibility,comments。friend_user_id作为friend_user_id,comments。user_id作为user_id,comments。action_id作为action_id,comments。comment_id作为comment_id,shoppingfeed。action_code作为action_code,shoppingfeed。action_date作为action_date,shoppingfeed。new_friend_id作为new_friend_id,shoppingfeed。question_id作为question_id,users。first_name作为first_name,users。last_name作为last_name,users。shopping_clout作为shopping_clout,users。gender作为gender,users。fb_resource_id作为fb_resource_id,comments。thread_id,thread_id_users.wish_qty作为wish_qty,merchants。logo作为logo,merchants。companyname作为companyname,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)))) 按comments.comment_date_time