gpt4 book ai didi

mysql - 如何优化缓慢的 MySQL 表更新查询?

转载 作者:行者123 更新时间:2023-11-30 21:48:14 25 4
gpt4 key购买 nike

在 where 子句中使用主键对一列进行简单更新需要 0.8 秒到 1.5 秒。如何优化此查询?

更新查询:

UPDATE oc_product SET viewed=(viewed+1) WHERE 
product_id=1521225;
Query OK, 1 row affected (***1.10 sec***)
Rows matched: 1 Changed: 1 Warnings: 0

解释同一个查询

explain UPDATE oc_product SET viewed=(viewed+1)        WHERE product_id=1521225;
+----+-------------+------------+------------+-------+---------------+---------+---------+-------+------+----------+-------------+
| id | select_type | table | partitions | type | possible_keys | key | key_len | ref | rows | filtered | Extra |
+----+-------------+------------+------------+-------+---------------+---------+---------+-------+------+----------+-------------+
| 1 | UPDATE | oc_product | NULL | range | PRIMARY | PRIMARY | 4 | const | 1 | 100.00 | Using where |
+----+-------------+------------+------------+-------+---------------+---------+---------+-------+------+----------+-------------+
1 row in set (0.00 sec)

这是显示创建表语句的输出:

Create Table: CREATE TABLE `oc_product` (
`product_id` int(11) NOT NULL AUTO_INCREMENT,
`model` varchar(64) NOT NULL,
`sku` varchar(64) NOT NULL,
`upc` varchar(12) NOT NULL,
`ean` varchar(14) NOT NULL,
`jan` varchar(13) NOT NULL,
`isbn` varchar(17) NOT NULL,
`mpn` varchar(64) NOT NULL,
`location` varchar(128) NOT NULL,
`quantity` int(4) NOT NULL DEFAULT '0',
`stock_status_id` int(11) NOT NULL,
`image` varchar(255) DEFAULT NULL,
`manufacturer_id` int(11) NOT NULL,
`shipping` tinyint(1) NOT NULL DEFAULT '1',
`price` decimal(15,4) NOT NULL DEFAULT '0.0000',
`points` int(8) NOT NULL DEFAULT '0',
`tax_class_id` int(11) NOT NULL,
`date_available` date NOT NULL DEFAULT '1000-01-01',
`weight` decimal(15,8) NOT NULL DEFAULT '0.00000000',
`weight_class_id` int(11) NOT NULL DEFAULT '0',
`length` decimal(15,8) NOT NULL DEFAULT '0.00000000',
`width` decimal(15,8) NOT NULL DEFAULT '0.00000000',
`height` decimal(15,8) NOT NULL DEFAULT '0.00000000',
`length_class_id` int(11) NOT NULL DEFAULT '0',
`subtract` tinyint(1) NOT NULL DEFAULT '1',
`minimum` int(11) NOT NULL DEFAULT '1',
`sort_order` int(11) NOT NULL DEFAULT '0',
`status` tinyint(1) NOT NULL DEFAULT '0',
`viewed` int(5) NOT NULL DEFAULT '0',
`date_added` datetime NOT NULL,
`date_modified` datetime NOT NULL,
`maximum` int(11) DEFAULT NULL,
`cost` decimal(15,4) NOT NULL DEFAULT '0.0000',
`usps_factor` decimal(15,4) DEFAULT NULL,
`sitemap_file` int(4) DEFAULT NULL,
`sameday_delivery` tinyint(1) NOT NULL DEFAULT '0',
`sameday_price` decimal(15,4) NOT NULL DEFAULT '0.0000',
PRIMARY KEY (`product_id`)
) ENGINE=InnoDB AUTO_INCREMENT=1523031 DEFAULT
CHARSET =utf8

最佳答案

oc_product 表是否有长时间运行的查询?创建 oc_product 表的副本并对新表运行更新查询。

关于mysql - 如何优化缓慢的 MySQL 表更新查询?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48490172/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com