我有一个项目,我在其中插入很多信息到每秒自动增量主键的表,并使用多线程执行此操作,这意味着有许多线程尝试向该表插入新行。因为表上有一个用于插入查询的锁,我不能同时执行插入,因此我无法从线程获得最大性能......
有没有办法克服这种锁定?
答案 0 :(得分:2)
您可以在my.cnf中将 innodb_autoinc_lock_mode 设置为1。然后没有用于Auto_increment的LOCK。如果一个Thead回滚看样本
,则可能在值中有漏洞MariaDB [test]> show variables like 'innodb_autoinc_lock_mode';
+--------------------------+-------+
| Variable_name | Value |
+--------------------------+-------+
| innodb_autoinc_lock_mode | 1 |
+--------------------------+-------+
1 row in set (0.00 sec)
样本1
MariaDB [test]> start transaction;
Query OK, 0 rows affected (0.00 sec)
MariaDB [test]> insert into autoinc VALUES(NULL,'hello');
Query OK, 1 row affected (0.01 sec)
thread 2 ------------------> MariaDB [test]> start transaction;
thread 2 ------------------> Query OK, 0 rows affected (0.00 sec)
thread 2 ------------------> MariaDB [test]> insert into autoinc VALUES(NULL,'world');
thread 2 ------------------> Query OK, 1 row affected (0.00 sec)
thread 2 ------------------> MariaDB [test]> commit;
thread 2 ------------------> Query OK, 0 rows affected (0.00 sec)
thread 2 ------------------> MariaDB [test]>
MariaDB [test]> commit;
Query OK, 0 rows affected (0.00 sec)
MariaDB [test]> select * from autoinc;
+----+-------+
| id | d |
+----+-------+
| 1 | hello |
| 2 | world |
+----+-------+
2 rows in set (0.00 sec)
MariaDB [test]>
带回滚的示例2
MariaDB [test]> start transaction;
Query OK, 0 rows affected (0.00 sec)
MariaDB [test]> insert into autoinc VALUES(NULL,'Guten');
Query OK, 1 row affected (0.00 sec)
thread 2 ------------------> MariaDB [test]> start transaction;
thread 2 ------------------> Query OK, 0 rows affected (0.00 sec)
thread 2 ------------------> MariaDB [test]> insert into autoinc VALUES(NULL,'Tag');
thread 2 ------------------> Query OK, 1 row affected (0.00 sec)
thread 2 ------------------> MariaDB [test]> commit;
thread 2 ------------------> Query OK, 0 rows affected (0.01 sec)
MariaDB [test]> rollback;
Query OK, 0 rows affected (0.00 sec)
MariaDB [test]> select * from autoinc;
+----+-------+
| id | d |
+----+-------+
| 1 | hello |
| 2 | world |
| 4 | Tag |
+----+-------+
3 rows in set (0.00 sec)
MariaDB [test]>