使用自连接和子查询优化updatesql查询

nom7f22z  于 2021-06-15  发布在  Mysql
关注(0)|答案(2)|浏览(401)

我的sql查询更新了数据库中的所有股票,但它的工作效率不是很高,有时会出现504个超时错误。代码运行良好。我怎样才能让它工作得更好。
p、 学生:请不要理会没有准备好的陈述,我稍后再补充。
有关表格的一些信息(wordpress插件默认表格):
wp\u posts:此表包括posts(帖子可以是产品,也可以是产品变体。示例一个产品是蝴蝶t恤,一个产品变体是蝴蝶t恤(红色大号)。
wp\u posttea:这个表包含了关于post的元信息。例如,如果一个产品变体是instock,或者它是什么颜色,或者它是什么尺寸。

//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);
    }
  }
kognpnkq

kognpnkq1#

从钥匙开始 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 过滤前?这使得不可能使用任何索引,因此是slooow。

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 已调试(我可能犯了一些错误,但看起来不是简单多了吗?它将运行得更快,尤其是使用我推荐的索引时。)
这个 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 ,则它不能在索引中,因此请将其忽略(不,不用麻烦用“前缀”索引。)
如果您使用的是MySQL5.5或5.6, meta_key 太长,无法作为索引;请参阅我的多个解决方法链接。
eav模式很糟糕,你正在找出原因。
回到狼窝 UPDATE ,添加 Package :

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'
iklwldmw

iklwldmw2#

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 .
除了这些技巧之外,请参阅我关于改进posmeta模式的技巧。

相关问题