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

时间:2018-01-28 18:57:27

标签: mysql performance

使用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)

这是show create table语句输出:

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

1 个答案:

答案 0 :(得分:0)

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