使用自连接和子查询优化UPDATE SQL查询

时间:2018-12-19 15:22:02

标签: mysql sql query-optimization query-performance update-statement

我的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);
    }
  }

2 个答案:

答案 0 :(得分: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

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

答案 1 :(得分:1)

首先使用键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_postmeta中“隐藏” UPDATE 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。 (我可能犯了一些错误,但是看起来不是很简单吗?它将运行得更快,尤其是使用我推荐的索引时。)

带有颜色的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

Optimizer选择要从哪个表开始后,它将依次转到其他每个表;顺序对我们来说无关紧要。这些其他索引可能会有用:

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太长而无法成为索引;有关多种解决方法,请参见我的link

EAV模式很糟糕,您正在找出原因。

返回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'