0

我的 SQL 查询更新了数据库中的所有股票,但它的工作效率不是很高,有时我会收到 504 超时错误。代码工作正常。我怎样才能让它更好地工作。

PS:请忽略缺少准备好的语句,我稍后会添加它们。

有关表格的一些信息(Wordpress Woocommerce 插件默认表格):

wp_posts:此表包括帖子。(帖子可以是产品或产品变体。例如产品是蝴蝶 T 恤,产品变体是蝴蝶 T 恤红色大号)。

wp_postmeta:此表包含有关帖子的元信息。例如,如果产品变体有库存,或者它是什么颜色,或者它是什么尺寸。

  //This array gives, which products are there, and their respective categories.
  $allProducts = array("Fermuarlı Kapşonlu Sweatshirt" => "'2653','2659'","Kapşonlu Sweatshirt" => "'2646','2651'","Sweatshirt" => "'2644','2650'","Kadın Tişört" => "'2654','2656'","Atlet" => "'2655','2657'","Tişört" => "'2643','2304'");

  //Below arrays gives information about, which product variations are out of stock.
  $tisort_OutOfStock =array();
  $atlet_OutOfStock =array("all_colors"=>"'3xl','4xl','5xl'");
  $kadin_tisort_OutOfStock =array("all_colors"=>"'xxl','3xl','4xl','5xl'");
  $sweatshirt_OutOfStock =array("beyaz"=>"'xxl','3xl','4xl','5xl'","kirmizi"=>"'xxl','3xl','4xl','5xl'","bordo"=>"'5xl'","antrasit"=>"'5xl'");
  $kapsonlu_sweatshirt_OutOfStock =array("gri-kircilli"=>"'5xl'");
  $fermuarli_kapsonlu_sweatshirt_OutOfStock =array("gri-kircilli"=>"'5xl'","siyah"=>"'5xl'");

  //Reset stocks before updating.
  $resetStocks = "UPDATE wp_postmeta set meta_value = 'instock' where meta_key = '_stock_status'";
  $wpdb->query($resetStocks);
  echo "Stoklar are reseted<br>";

  //Foreach product, foreach color, update if product doesn't have stock.
  foreach( $allProducts as $key => $urun ){

    switch ($key) {
    case "Kadın Tişört": $tempArray = $kadin_tisort_OutOfStock; break;
    case "Fermuarlı Kapşonlu Sweatshirt": $tempArray = $fermuarli_kapsonlu_sweatshirt_OutOfStock; break;
    case "Kapşonlu Sweatshirt": $tempArray = $kapsonlu_sweatshirt_OutOfStock; break;
    case "Sweatshirt": $tempArray = $sweatshirt_OutOfStock; break;
    case "Atlet": $tempArray = $atlet_OutOfStock; break;
    case "Tişört": $tempArray = $tisort_OutOfStock; break;
    }

    foreach( $tempArray as $color => $size ){

      $query = "UPDATE wp_postmeta set meta_value = 'outofstock' where meta_key = '_stock_status' and post_id in
      (
      select post_id from (select * from wp_postmeta) AS X where meta_key = 'attribute_pa_beden' and meta_value in (".$size.")
      and post_id in (select post_id from (select * from wp_postmeta) AS Y where meta_key = 'attribute_pa_renk' and ((meta_value = '".$color."') OR ('".$color."' = 'all_colors')))
      and post_id in (select id from wp_posts where post_type = 'product_variation' and post_parent in (select object_id FROM wp_term_relationships where term_taxonomy_id in (".$urun.")))
      )";

      global $wpdb;
      $updatedRowCount = $wpdb->query($query);
    }
  }
4

2 回答 2

2

首先处理密钥SELECT

        SELECT  post_id
            from  
            (
                SELECT  *
                    from  wp_postmeta
            ) AS X
            where  meta_key = 'attribute_pa_beden'
              and  meta_value in (".$size.")
              and  post_id in (
                SELECT  post_id
                    from  
                    (
                        SELECT  *
                            from  wp_postmeta
                    ) AS Y
                    where  meta_key = 'attribute_pa_renk'
                      and  ((meta_value = '".$color."')
                              OR  ('".$color."' = 'all_colors'))
                          )
              and  post_id in (
                SELECT  id
                    from  wp_posts
                    where  post_type = 'product_variation'
                      and  post_parent in (
                        SELECT  object_id
                            FROM  wp_term_relationships
                            where  term_taxonomy_id in (".$urun."))) 
                      )";

是的,我知道您需要从 中“隐藏” wp_postmetaUPDATE wp_postmeta但我们可以重新安排事情以提高效率。请注意,您有两种情况是如何在过滤wp_postmeta 之前获取整个数据的?这使得无法使用任何索引,因此很慢。

SELECT m1.post_id
    FROM wp_postmeta AS m1
    JOIN wp_postmeta AS m2  USING(post_id)
    JOIN wp_posts    AS p2  USING(post_id)
    JOIN wp_term_relationships AS tr  ON p2.post_parent = tr.object_id
    WHERE m1.meta_key = 'attribute_pa_beden' AND   m1.meta_value in ("$size")
      AND m2.meta_key = 'attribute_pa_renk'  AND ( m1.meta_value = '$color'
                                                   OR '$color' = 'all_colors' )
      AND p2.post_type = 'product_variation'
      AND tr.term_taxonomy_id IN ($urun)

忘记这个,UPDATE直到你得到这个SELECT调试。(我可能犯了一些错误,但它看起来不是很简单吗?它会运行得更快,尤其是使用我推荐的索引。)

with color 可能会被优化,OR所以我不会担心。

我无法预测优化器将从 4 个表中的哪一个开始,因此需要这些索引来给它选择:

tr:  (term_taxonomy_id, object_id)  -- in this order
posts:  (post_type, post_id)        -- in this order
postmeta:  (meta_key, meta_value)   -- see note below

优化器选择从哪张表开始后,依次去其他每张表;顺序对我们来说并不重要。这些附加索引可能有用:

posts:   (post_parent, post_id)        -- in this order
postmeta:  (post_id, meta_key, meta_value)   -- see note below

如果meta_valueLONGTEXT,那么它不能在索引中,所以把它排除在外。(不,不要为“前缀”索引而烦恼。)

如果您使用的是 MySQL 5.5 或 5.6,meta_key则索引太长;请参阅我的链接以了解多种解决方法。

EAV 架构很糟糕,您正在找出原因。

回到 kludge for UPDATE,添加包装器:

UPDATE wp_postmeta AS m
    JOIN  ( SELECT post_id
              FROM ( the above query )
          ) AS kludge  USING (post_id)
    SET   m.meta_value = 'outofstock'
    WHERE m.meta_key = '_stock_status'
于 2018-12-28T04:05:43.067 回答
1
        SELECT  post_id
            from  
            (
                SELECT  *
                    from  wp_postmeta
            ) AS X
            where  ...

-->

        SELECT post_id 
            FROM wp_postmeta
            WHERE ...

(子查询只会减慢速度。)

              and  post_id in (
                SELECT  post_id

代替IN ( SELECT ... ), 使用JOIN.

除了这些技巧,请参阅我关于改进 postmeta 模式的技巧。

于 2018-12-19T20:02:44.227 回答