繁体   English   中英

查询需要很长时间才能执行并使网站崩溃

[英]query taking long time to execute and crashing the site

我在magento应用程序(社区版)上有大约2.5步(250K)产品和2600个子类别。

询问

SELECT 1 status
     , e.entity_id
     , e.type_id
     , e.attribute_set_id
     , cat_index.position AS cat_index_position
     , e.name
     , e.description
     , e.short_description
     , e.price
     , e.special_price
     , e.special_from_date
     , e.special_to_date
     , e.cost
     , e.small_image
     , e.thumbnail
     , e.color
     , e.color_value
     , e.news_from_date
     , e.news_to_date
     , e.url_key
     , e.required_options
     , e.image_label
     , e.small_image_label
     , e.thumbnail_label
     , e.msrp_enabled
     , e.msrp_display_actual_price_type
     , e.msrp
     , e.tax_class_id
     , e.price_type
     , e.weight_type
     , e.price_view
     , e.shipment_type
     , e.links_purchased_separately
     , e.links_exist
     , e.open_amount_min
     , e.open_amount_max
     , e.custom_h1
     , e.awards
     , e.region
     , e.grape_type
     , e.food_match
     , e.udropship_vendor
     , e.upc_barcode
     , e.ean_barcode
     , e.mpn
     , e.size
     , e.author
     , e.format
     , e.pagination
     , e.publish_date
     , price_index.price
     , price_index.tax_class_id
     , price_index.final_price
     , IF(price_index.tier_price IS NOT NULL
     , LEAST(price_index.min_price
     , price_index.tier_price)
     , price_index.min_price) AS minimal_price
     , price_index.min_price
     , price_index.max_price
     , price_index.tier_price 
  FROM catalog_product_flat_1 e
  JOIN catalog_category_product_index cat_index 
    ON cat_index.product_id = e.entity_id 
   AND cat_index.store_id = 1 
   AND cat_index.visibility IN(2,4) 
   AND cat_index.category_id = 163
  JOIN catalog_product_index_price price_index 
    ON price_index.entity_id = e.entity_id 
   AND price_index.website_id = 1 
   AND price_index.customer_group_id = 0 
 GROUP 
    BY e.entity_id 
 ORDER 
    BY cat_index_position ASC
     , cat_index.position ASC 
 LIMIT 15;

每当访问此magento站点上的任何产品时,它都会在服务器上的/ tmp目录下创建大约10 GB的巨大数据。

如何解决此问题,请提出一些解决方案。

数据库大小为50 GB,服务器为nginx。

您正在滥用GROUP BY 请了解其工作原理。 MySQL中有一个错误功能,允许您滥用它。 不幸的是,滥用查询的问题很难解决。

从查询中很难推断出您要做什么。 当您处理如此大小的结果集时,了解您的意图会有所帮助。

您应该知道(如果尚未知道)以下形式的查询

 SELECT <<many columns>>
   FROM large_table
   JOIN another_large_table ON something
   JOIN another_large_table ON something
  ORDER BY some_arbitrary_column
  LIMIT some_small_number

效率极低,因为它们必须生成巨大的结果集,然后对整个对象进行排序,然后返回第一个结果。 排序操作带有整个结果集。 您可能会指示MySQL服务器对一两行或两行(数十个兆行)进行排序。

看起来您想要从最低的cat_index.position值开始的前15个结果。 因此,通过与调用cat_index的表的适当子集cat_index ,您可以使查询更快,如下所示:

SELECT 1 status, many_other_columns
  FROM catalog_product_flat_1 e
  JOIN (   /* join only with fifteen lowest eligible position values in cat_index */
     SELECT * 
       FROM catalog_category_product_index
      WHERE store_id = 1 
        AND visibility IN(2,4) 
        AND category_id = 163
      ORDER BY position ASC
      LIMIT 15
       ) AS cat_index ON cat_index.product_id = e.entity_id 
  JOIN catalog_product_index_price price_index 
             ON price_index.entity_id = e.entity_id 
            AND price_index.website_id = 1 
            AND price_index.customer_group_id = 0 
 GROUP BY e.entity_id     /*wrong!!*/
 ORDER BY cat_index_position ASC,   /* redundant!*/
          cat_index.position ASC 
 LIMIT 15;

值得一试。

您是否有足够的硬件资源来运行大型查询,还请更新服务器的硬件配置。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM