是一种优化大型mysql查询的方法吗?

n1bvdmb6  于 2021-06-21  发布在  Mysql
关注(0)|答案(2)|浏览(363)

这是我的问题,它是慢。。。我在寻找优化的方法。

SELECT p.id, 
       Group_concat(pc.cat_id)   AS groups, 
       p.code, 
       p.NAME, 
       p.price, 
       p.thumbnail, 
       p.image, 
       mc.queries                AS merch_queries, 
       mc.position               AS merch_position, 
       Group_concat(op.image)    AS option_images, 
       cf_RETAIL.value           AS custom_RETAIL, 
       cf_rating.value           AS custom_rating, 
       cf_reviews.value          AS custom_reviews, 
       cf_sku.value              AS custom_sku, 
       cf_brand.value            AS custom_brand, 
       cf_custom_thumbnail.value AS custom_custom_thumbnail 
FROM   s01_products AS p 
       LEFT JOIN s01_categoryxproduct AS pc 
              ON p.id = pc.product_id 
       LEFT JOIN (SELECT pv.product_id, 
                         pv.value 
                  FROM   s01_cfm_prodfields AS pf 
                         INNER JOIN s01_cfm_prodvalues AS pv 
                                 ON pf.id = pv.field_id 
                  WHERE  pf.code = 'RETAIL') AS cf_RETAIL 
              ON p.id = cf_RETAIL.product_id 
       LEFT JOIN (SELECT pv.product_id, 
                         pv.value 
                  FROM   s01_cfm_prodfields AS pf 
                         INNER JOIN s01_cfm_prodvalues AS pv 
                                 ON pf.id = pv.field_id 
                  WHERE  pf.code = 'rating') AS cf_rating 
              ON p.id = cf_rating.product_id 
       LEFT JOIN (SELECT pv.product_id, 
                         pv.value 
                  FROM   s01_cfm_prodfields AS pf 
                         INNER JOIN s01_cfm_prodvalues AS pv 
                                 ON pf.id = pv.field_id 
                  WHERE  pf.code = 'reviews') AS cf_reviews 
              ON p.id = cf_reviews.product_id 
       LEFT JOIN (SELECT pv.product_id, 
                         pv.value 
                  FROM   s01_cfm_prodfields AS pf 
                         INNER JOIN s01_cfm_prodvalues AS pv 
                                 ON pf.id = pv.field_id 
                  WHERE  pf.code = 'sku') AS cf_sku 
              ON p.id = cf_sku.product_id 
       LEFT JOIN (SELECT pv.product_id, 
                         pv.value 
                  FROM   s01_cfm_prodfields AS pf 
                         INNER JOIN s01_cfm_prodvalues AS pv 
                                 ON pf.id = pv.field_id 
                  WHERE  pf.code = 'brand') AS cf_brand 
              ON p.id = cf_brand.product_id 
       LEFT JOIN (SELECT pv.product_id, 
                         pv.value 
                  FROM   s01_cfm_prodfields AS pf 
                         INNER JOIN s01_cfm_prodvalues AS pv 
                                 ON pf.id = pv.field_id 
                  WHERE  pf.code = 'custom_thumbnail') AS cf_custom_thumbnail 
              ON p.id = cf_custom_thumbnail.product_id 
       LEFT JOIN (SELECT p.product_id          AS product_id, 
                         Group_concat(q.query) AS queries, 
                         Min(p.position)       AS position 
                  FROM   s01_srch_merchandisingproduct AS p 
                         LEFT JOIN s01_srch_merchandisingquery AS q 
                                ON q.id = p.query_id 
                  GROUP  BY p.product_id) AS mc 
              ON p.id = mc.product_id 
       LEFT JOIN s01_options AS op 
              ON p.id = op.product_id 
                 AND op.image <> '' 
WHERE  p.active = 1 
GROUP  BY p.id

谢谢你的帮助!
更新的表架构:


**s01_categoryxproduct**

cat_id, 
product_id, 
disp_order

**s01_products**

id      
catcount    
agrpcount   
pgrpcount   
disp_order  
code            
name            
thumbnail       
image           
price       
cost        
descrip         
weight      
taxable     
active      
sku         
cancat_id   
page_id     
page_title      
dt_created  
dt_updated

**s01_CFM_ProdValues**

field_id, 
product_id, 
value, 
value_long

**s01_CFM_ProdFields**

id, 
code, 
name, 
group_id, 
fieldtype, 
info, 
facet

**s01_Options**

id, 
product_id, 
attr_id, 
disp_order, 
code, 
prompt, 
price, 
cost, 
weight, 
image

**s01_SRCH_MerchandisingProduct**

id, 
product_id, 
query_id, 
position

**s01_SRCH_MerchandisingQuery**

id, 
query
vu8f3i0k

vu8f3i0k1#

SELECT p.id, 
           Group_concat(pc.cat_id)   AS groups, 
           p.code, 
           p.NAME, 
           p.price, 
           p.thumbnail, 
           p.image, 
           mc.queries                AS merch_queries, 
           mc.position               AS merch_position, 
           Group_concat(op.image)    AS option_images, 
           IF(pf.code = 'RETAIL',pv.value , NULL)         AS custom_RETAIL,
           IF(pf.code = 'rating',pv.value , NULL)         AS custom_rating, 
           IF(pf.code = 'reviews',pv.value , NULL)         AS custom_reviews, 
           IF(pf.code = 'brand',pv.value , NULL)         AS custom_brand, 
           IF(pf.code = 'custom_thumbnail',pv.value , NULL)         AS custom_custom_thumbnail, 
           pvr.value           AS custom_rating, 
    FROM   s01_products AS p 
    LEFT JOIN s01_categoryxproduct AS pc ON p.id = pc.product_id 
    LEFT JOIN s01_cfm_prodfields AS cf_RETAIL ON p.id = cf_RETAIL.product_id 
    LEFT JOIN s01_cfm_prodvalues AS pv ON cf_RETAIL.id = pv.field_id
    LEFT JOIN (SELECT p.product_id          AS product_id, 
                         Group_concat(q.query) AS queries, 
                         Min(p.position)       AS position 
                  FROM   s01_srch_merchandisingproduct AS p 
                         LEFT JOIN s01_srch_merchandisingquery AS q 
                                ON q.id = p.query_id 
                  GROUP  BY p.product_id) AS mc 
              ON p.id = mc.product_id 
       LEFT JOIN s01_options AS op 
              ON p.id = op.product_id 
                 AND op.image <> ''
    WHERE  p.active = 1
    GROUP  BY p.id`;
dbf7pr2w

dbf7pr2w2#

“过度规范化”和eav是问题所在。
将prodfields和prodvalues放在同一个表中。将它们分成两个表会导致性能消耗开销。
有关优化eav表的详细信息:http://mysql.rjweb.org/doc.php/index_cookbook_mysql#speeding_up_wp_postmeta
更多关于eav为何不好的讨论:http://mysql.rjweb.org/doc.php/eav

相关问题