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