概要:
各製品に存在する場合と存在しない場合がある、製品とユーザーのコメントのページがあります。1 つのテーブルは製品の詳細 (products_master) を保持し、1 つのテーブルは表示される製品を決定します (shoppingfeed)。クエリ #1 から各製品をループして出力すると、クエリ #2 が実行され、現在表示されている製品に関連するコメントがクエリ #1 から取得されます。
問題: これはすべて動作しますが、遅いです! これを 1 回実行できる 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)))) order bycomments.comment_date_time