mysql 2分钟_为什么这个MYSQL UPDATE查询需要2分钟才能运行?

此查询执行大约需要2分钟(更改9条记录):

UPDATE table1 t1

SET t1.code_id = null, t1.code_group = null

WHERE t1.another_id IN (SELECT t2.another_id

FROM table2 t2

WHERE ((t2.id_parent = 2658 AND t2.year = 2016)

OR (t2.id = 2658 AND t2.year = 2016)))

单独执行此查询需要0.0030秒:

SELECT t2.another_id

FROM table2 t2

WHERE ((t2.id_parent = 2658 AND t2.year = 2016)

OR (t2.id = 2658 AND t2.year = 2016))

并以整数形式返回3行.

这是两个表的信息:

CREATE TABLE IF NOT EXISTS `table1`

(

`another_id` int(11) NOT NULL,

`table1_id` int(11) NOT NULL,

`code_group` varchar(1) DEFAULT NULL,

`code_id` int(10) DEFAULT NULL,

PRIMARY KEY (`another_id`,`table1_id`),

KEY `another_id` (`another_id`),

KEY `code_group` (`code_group`,`code_id`)

) ENGINE=MyISAM DEFAULT CHARSET=latin1;

CREATE TABLE IF NOT EXISTS `table2`

(

`id_year` int(11) NOT NULL,

`id` int(11) NOT NULL,

`id_parent` int(11) DEFAULT NULL,

`another_id` int(11) NOT NULL,

`code_group` varchar(1) DEFAULT NULL,

`code_id` int(10) DEFAULT NULL,

PRIMARY KEY (`id_year`,`id`),

KEY `id_parent` (`id_year`,`id_parent`)

KEY `another_id` (`another_id`)

) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_polish_ci;

是否有人可以告诉我为什么需要2分钟才能执行此查询?

最佳答案 您可以使用INNER JOIN更新如下:t2.year也不存在

UPDATE table1 t1

INNER JOIN table2 t2 ON t2.another_id = t1.another_id

AND ((t2.id_parent= 2658 AND t2.year= 2016) OR (t2.id= 2658 AND t2.year= 2016))

SET t1.code_id = NULL, t1.code_group = NULL

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值