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

Nat*_*raj 0 mysql nginx magento database-performance

我在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;
Run Code Online (Sandbox Code Playgroud)

每当访问该magento站点上的任何产品时,它在服务器上的/ tmp目录下创建了一个大约10 GB的大数据.

我该如何解决这个问题请提出一些解决方案.

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

O. *_*nes 5

你在滥用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
Run Code Online (Sandbox Code Playgroud)

可能是非常低效的,因为他们必须生成一个巨大的结果集,然后对整个事物进行排序,然后返回第一个结果.排序操作带有整个结果集.您可以指示MySQL服务器对一两行(几十个megarows)进行排序.

看起来您希望前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;
Run Code Online (Sandbox Code Playgroud)

值得一试.

  • 我不同意op.why这个答案是不被接受的? (2认同)