摘要:
我们有一个包含产品和用户评论的页面,每个产品上可能存在也可能不存在。一张表保存产品详细信息 (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
这是我整理的 View
select
products_master
.pop_sku
ASpop_sku
,products_master
.group_id
ASgroup_id
,products_master
.name
ASname
,products_master
.image_url
ASimage_url
,products_master
.last_updated
ASlast_updated
,products_master
.have_it_users
AShave_it_users
,products_master
.want_it_users
ASwant_it_users
,products_master
.adults_only
ASadults_only
,products_master
.reviewed_by
ASreviewed_by
,products_master
.donate_needed_qty
ASdonate_needed_qty
,products_master
.inspired_users
ASinspired_users
,products_master
.deal_users_up
ASdeal_users_up
,products_master
.deal_users_down
ASdeal_users_down
,products_master
.merchant
ASmerchant
,products_master
.merchant_logo
ASmerchant_logo
,products_master
.width
ASwidth
,products_master
.height
ASheight
,products_master
.added_by
ASadded_by
,products_master
.product_category_id
ASproduct_category_id
,comments
.comment
AScomment
,comments
.comment_date_time
AScomment_date_time
,comments
.comment_visibility
AScomment_visibility
,comments
.friend_user_id
ASfriend_user_id
,comments
.user_id
ASuser_id
,comments
.action_id
ASaction_id
,comments
.comment_id
AScomment_id
,shoppingfeed
.action_code
ASaction_code
,shoppingfeed
.action_date
ASaction_date
,shoppingfeed
.new_friend_id
ASnew_friend_id
,shoppingfeed
.question_id
ASquestion_id
,users
.first_name
ASfirst_name
,users
.last_name
ASlast_name
,users
.shopping_clout
ASshopping_clout
,users
.gender
ASgender
,users
.fb_resource_id
ASfb_resource_id
,comments
.thread_id
ASthread_id
,users
.wish_qty
ASwish_qty
,merchants
.logo
ASlogo
,merchants
.companyname
AScompanyname
,product_relationship
.desirability
ASdesirability
from (((((products_master
joinshoppingfeed
on((products_master
.pop_sku
=shoppingfeed
.sf_product_id
))) joincomments
on((products_master
.pop_sku
=comments
.sf_product_id
))) joinusers
on(((comments
.user_id
=users
.user_id
) and (comments
.user_id
=shoppingfeed
.user_id
)))) joinproduct_relationship
on((product_relationship
.user_id
=users
.user_id
))) joinmerchants
on(((products_master
.merchant
=merchants
.merchantid
) and (product_relationship
.sf_product_id
=products_master
.pop_sku
)))) order bycomments
.comment_date_time
最佳答案
- 尝试使用“内连接”或“左/右连接”代替“where”子句中的条件
- 检查您的主键和外键,如果它们在连接表中引用更多内容,请将其添加到您的表列中
- 如果表列在连接表中引用较多,则为它们创建索引(事实上,外键就是索引之一)
关于mysql - 查询优化(MySQL),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18627612/