【问题标题】:query taking long time to execute and crashing the site查询需要很长时间才能执行并使网站崩溃
【发布时间】:2014-06-13 14:01:09
【问题描述】:

我在 magento 应用程序(社区版)上有大约 2.5 lachs (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。

【问题讨论】:

  • 运行您描述的查询,但将 EXPLAIN EXTENDED 放在它前面。输出是什么。此外,尝试摆脱 ORDER BY 并使用 EXPLAIN EXTENDED 运行查询。也发布该输出。
  • 在没有任何聚合函数的情况下,我认为使用 GROUP BY 子句是不合适的,并且会提供误导性结果。
  • 您的 MySQL 配置是否针对 Magento 进行了优化?见magentospeedup.com/2013/05/31/…
  • 您使用的是平面类别表吗?

标签: mysql magento nginx database-performance


【解决方案1】:

您误用了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 服务器对一千万行或两行(数十个兆行)进行排序。

您似乎希望前 15 个结果从最低的 cat_index.position 值开始。因此,您可以通过加入您称为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;

值得一试。

【讨论】:

  • 我不同意 op。为什么这个答案不被接受?
  • 感谢您的宝贵支持。我已转发给我的开发者。
【解决方案2】:

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

【讨论】:

猜你喜欢
  • 2018-07-06
  • 1970-01-01
  • 2016-04-22
  • 2021-12-25
  • 2016-06-17
  • 1970-01-01
  • 1970-01-01
  • 2021-06-15
  • 2011-01-17
相关资源
最近更新 更多