我的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);
}
}
2条答案
按热度按时间kognpnkq1#
从钥匙开始
SELECT
:是的,我知道你需要“躲起来”
wp_postmeta
从UPDATE wp_postmeta
,但我们可以重新安排,使之更有效率。请注意,您有两个获取整个wp_postmeta
过滤前?这使得不可能使用任何索引,因此是slooow。忘了那个吧
UPDATE
直到你得到这个SELECT
已调试(我可能犯了一些错误,但看起来不是简单多了吗?它将运行得更快,尤其是使用我推荐的索引时。)这个
OR
颜色可能会优化出来,所以我不会担心。我无法预测优化器将从4个表中的哪一个开始,因此需要这些索引来为它提供选择:
在优化器选择从哪个表开始之后,它将依次转到其他每个表;订单对我们来说无关紧要。这些附加索引可能有用:
如果
meta_value
是LONGTEXT
,则它不能在索引中,因此请将其忽略(不,不用麻烦用“前缀”索引。)如果您使用的是MySQL5.5或5.6,
meta_key
太长,无法作为索引;请参阅我的多个解决方法链接。eav模式很糟糕,你正在找出原因。
回到狼窝
UPDATE
,添加 Package :iklwldmw2#
-->
(子查询只会减慢速度。)
而不是
IN ( SELECT ... )
,使用JOIN
.除了这些技巧之外,请参阅我关于改进posmeta模式的技巧。