gpt4 book ai didi

mysql - 每个自定义列的 Innodb 自动增量,并发性差

转载 作者:行者123 更新时间:2023-11-29 03:22:58 25 4
gpt4 key购买 nike

我们有 MySql 5.5 表:

CREATE TABLE IF NOT EXISTS `invoices` (
`id` varchar(36) NOT NULL,
`client_id` smallint(4) NOT NULL,
`invoice_number` int(11) DEFAULT NULL,
PRIMARY KEY (`id`),
UNIQUE KEY `client_id_2` (`client_id`,`invoice_number`),
KEY `client_id` (`client_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

我们像这样将数据插入该表:

INSERT INTO `invoices` ( `id` , `client_id` ,  `invoice_number`   )  
VALUES (
UUID(),
10 ,
( SELECT (MAX(`invoice_number`) +1) as next_invoice_number FROM `invoices` WHERE `client_id` = 10 )
);

“10”是 client_id 值。

它可以工作,但是它的并发性很差。我怎样才能拥有具有良好并发性的工作解决方案?

复合主键自增不是解决方案。我们需要为每个 client_id 值自动递增。 Composite-primary-key auto increment 提供整个表的自动增量,而不是每个 client_id 列值。

最佳答案

不确定此处并发性差是什么意思。虽然每个 DML 操作都在隐式事务上运行,但您也可以使用 begin transaction ... end 构造将其包装在显式事务 block 中。

关于mysql - 每个自定义列的 Innodb 自动增量,并发性差,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40910514/

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