mysql键值表一次更新多个id的多行

f0ofjuux  于 2021-06-20  发布在  Mysql
关注(0)|答案(2)|浏览(367)

我目前正努力尽快获得一个mysql多行更新查询。
如果知道我正在使用wordpress,这个查询会更新wp\u postemta表,可能会很方便。
我目前的查询需要184秒。我真的需要加快速度,因为这个查询将连续运行多次,以更新csv表中的产品。
产品分50批进口。
当然,如果需要更多的信息请告诉我。希望有人愿意并且能够帮忙。谢谢!

UPDATE wp_postmeta t1
LEFT JOIN wp_wc_importer_product_cache t2 ON t1.post_id=t2.product_id
LEFT JOIN wp_wc_importer_csv t3 ON t2.sku=t3.sku AND t2.ean = t3.ean
    -- Global meta
    SET t1.meta_value = (
        CASE WHEN t1.meta_key = '_price' AND (t1.meta_value != t3.sell_price OR (t1.meta_value IS NULL AND t3.sell_price IS NOT NULL)) THEN t3.sell_price
             WHEN t1.meta_key = '_regular_price' AND (t1.meta_value != t3.sell_price OR (t1.meta_value IS NULL AND t3.sell_price IS NOT NULL)) THEN t3.sell_price
             WHEN t1.meta_key = '_tax_status' AND (t1.meta_value != 'taxable' OR (t1.meta_value IS NULL AND 'taxable' IS NOT NULL)) THEN 'taxable'
             WHEN t1.meta_key = '_backorders' AND (t1.meta_value != 'no' OR (t1.meta_value IS NULL AND 'no' IS NOT NULL)) THEN 'no'
             WHEN t1.meta_key = '_weight' AND (t1.meta_value != t3.weight OR (t1.meta_value IS NULL AND t3.weight IS NOT NULL)) THEN t3.weight
             WHEN t1.meta_key = '_product_version' AND (t1.meta_value != '3.3.1' OR (t1.meta_value IS NULL AND '3.3.1' IS NOT NULL)) THEN '3.3.1'
             ELSE CASE WHEN t2.sku_parent = '' THEN
                -- Parent meta
                CASE WHEN t1.meta_key = 'product_title' AND (t1.meta_value != t3.title_dutch OR (t1.meta_value IS NULL AND t3.title_dutch IS NOT NULL)) THEN t3.title_dutch
                     WHEN t1.meta_key = 'article_number' AND (t1.meta_value != t3.iso OR (t1.meta_value IS NULL AND t3.iso IS NOT NULL)) THEN t3.iso
                     WHEN t1.meta_key = '_tax_class' AND (t1.meta_value != '' OR (t1.meta_value IS NULL AND '' IS NOT NULL)) THEN ''
                     WHEN t1.meta_key = 'product_seo_text' AND (t1.meta_value != t3.desc_long_dutch OR (t1.meta_value IS NULL AND t3.desc_long_dutch IS NOT NULL)) THEN t3.desc_long_dutch
                     ELSE t1.meta_value
                END
             ELSE
                -- Variation meta
                CASE WHEN t1.meta_key = '_variation_description' AND (t1.meta_value != t3.invoice_description OR (t1.meta_value IS NULL AND t3.invoice_description IS NOT NULL)) THEN t3.invoice_description
                     WHEN t1.meta_key = 'attribute_pa_material' AND (t1.meta_value != t3.material OR (t1.meta_value IS NULL AND t3.material IS NOT NULL)) THEN t3.material
                     WHEN t1.meta_key = 'attribute_pa_diameter' AND (t1.meta_value != IF(t3.diameter_prefix != '', CONCAT(t3.diameter_prefix, ' ', t3.diameter), t3.diameter) OR (t1.meta_value IS NULL AND t3.diameter IS NOT NULL))
                        THEN IF(t3.diameter_prefix != '', CONCAT(t3.diameter_prefix, ' ', t3.diameter), t3.diameter)
                     WHEN t1.meta_key = 'attribute_pa_length' AND (t1.meta_value != t3.length OR (t1.meta_value IS NULL AND t3.length IS NOT NULL)) THEN t3.length
                     WHEN t1.meta_key = 'attribute_pa_sold-per' AND (t1.meta_value != t3.package_quantity OR (t1.meta_value IS NULL AND t3.package_quantity IS NOT NULL)) THEN  t3.package_quantity
                     WHEN t1.meta_key = 'attribute_pa_screw-thread' AND (t1.meta_value != t3.screw_thread OR (t1.meta_value IS NULL AND t3.screw_thread IS NOT NULL)) THEN t3.screw_thread
                     WHEN t1.meta_key = 'attribute_factuur-beschrijving' AND (t1.meta_value != t3.invoice_description OR (t1.meta_value IS NULL AND t3.invoice_description IS NOT NULL)) THEN t3.invoice_description
                     WHEN t1.meta_key = 'attribute_ean' AND (t1.meta_value != t3.ean OR (t1.meta_value IS NULL AND t3.ean IS NOT NULL)) THEN t3.ean
                     WHEN t1.meta_key = 'attribute_sp-artikel-nummer' AND (t1.meta_value != t3.sp_article_number OR (t1.meta_value IS NULL AND t3.sp_article_number IS NOT NULL)) THEN t3.sp_article_number
                     WHEN t1.meta_key = '_stock' AND (t1.meta_value != t3.stock AND t3.stock != '' AND t3.stock IS NOT NULL OR (t1.meta_value IS NULL AND t3.stock IS NOT NULL)) THEN t3.stock
                     WHEN t1.meta_key = '_stock_status' AND t3.stock != '' AND t3.stock IS NOT NULL THEN
                        CASE WHEN t3.stock > 0 THEN 'instock' ELSE 'outofstock' END
                     ELSE t1.meta_value
                END
            END
        END
    )
WHERE t1.post_id IN (52558,52559,52560,52561,52562,52563,52564,52565,52566,52567,52568,52569,52570,52571,52572,52573,52574,52575,52576,52577,52578,52579,52580,52581,52582,52583,52584,52585,52586,52587,52588,52589,52590,52591,52592,52593,52594,52595,52596,52597,52598,52599,52600,52601,52608,52609,52610,52611,52612,52613)

--编辑(关于m khalid junaid的问题)--
这些表的索引如下:
可湿性粉剂

wp\u wc\u导入程序\u csv

wp\ wc\进口商\产品\缓存

euoag5mw

euoag5mw1#

这看起来像是查询的这一部分:

LEFT JOIN wp_wc_importer_csv t3 ON t2.sku=t3.sku AND t2.ean = t3.ean

对每个更新的行执行wp\u wc\u importer\u csv的完全扫描。尝试将索引添加到wp\u wc\u importer\u csv on(sku,ean)字段。

rhfm7lfc

rhfm7lfc2#

写个人 UPDATEs 每种情况。
将它们 Package 到事务中( BEGIN ... COMMIT )
按照这里的提示改进 postmeta .
重新考虑使用 LEFT .
给我们看看 SHOW CREATE TABLE 对于t2和t3,它们可能需要一些新的综合指数。这个 SHOW INDEXES 这是不够的。缺少最佳索引。
如果您运行的是足够新的mysql版本,请提供 EXPLAIN UPDATE ...

相关问题