mysql - 查询优化(MySQL)

标签 mysql sql

摘要:

我们有一个包含产品和用户评论的页面,每个产品上可能存在也可能不存在。一张表保存产品详细信息 (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 AS pop_sku,products_master.group_id AS group_id,products_master.name AS name,products_master.image_url AS image_url,products_master.last_updated AS last_updated,products_master.have_it_users AS have_it_users,products_master.want_it_users AS want_it_users,products_master.adults_only AS adults_only,products_master.reviewed_by AS reviewed_by,products_master.donate_needed_qty AS donate_needed_qty,products_master.inspired_users AS inspired_users,products_master.deal_users_up AS deal_users_up,products_master.deal_users_down AS deal_users_down,products_master.merchant AS merchant,products_master.merchant_logo AS merchant_logo,products_master.width AS width,products_master.height AS height,products_master.added_by AS added_by,products_master.product_category_id AS product_category_id,comments.comment AS comment,comments.comment_date_time AS comment_date_time,comments.comment_visibility AS comment_visibility,comments.friend_user_id AS friend_user_id,comments.user_id AS user_id,comments.action_id AS action_id,comments.comment_id AS comment_id,shoppingfeed.action_code AS action_code,shoppingfeed.action_date AS action_date,shoppingfeed.new_friend_id AS new_friend_id,shoppingfeed.question_id AS question_id,users.first_name AS first_name,users.last_name AS last_name,users.shopping_clout AS shopping_clout,users.gender AS gender,users.fb_resource_id AS fb_resource_id,comments.thread_id AS thread_id,users.wish_qty AS wish_qty,merchants.logo AS logo,merchants.companyname AS companyname,product_relationship.desirability AS desirability from (((((products_master join shoppingfeed on((products_master.pop_sku = shoppingfeed.sf_product_id))) join comments on((products_master.pop_sku = comments.sf_product_id))) join users on(((comments.user_id = users.user_id) and (comments.user_id = shoppingfeed.user_id)))) join product_relationship on((product_relationship.user_id = users.user_id))) join merchants on(((products_master.merchant = merchants.merchantid) and (product_relationship.sf_product_id = products_master.pop_sku)))) order by comments.comment_date_time

最佳答案

  1. 尝试使用“内连接”或“左/右连接”代替“where”子句中的条件
  2. 检查您的主键和外键,如果它们在连接表中引用更多内容,请将其添加到您的表列中
  3. 如果表列在连接表中引用较多,则为它们创建索引(事实上,外键就是索引之一)

关于mysql - 查询优化(MySQL),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18627612/

相关文章:

mysql - SQL:选择其他查询未选择的所有记录

mysql - MATLAB My​​SQL ODBC 连接

java - JSP - 连接并将数据插入 MySQL 数据库

mysql - SQL-通过比较字段值在重复行之间进行选择

mysql - SQL:搜索/替换,但仅在值第一次出现在记录中时

带有选择和硬编码值的 SQL 插入

mysql - 检查数字是否在由分隔符分隔的字符串中

mysql - 遇到需要满足多个条件的 MySQL Join 问题

sql - 在 Laravel 查询中一起使用 find() 和 with()

sql - 如何在 SQL Server 2012 集群上创建尽可能快的数据库,同时牺牲任何持久性