码迷,mamicode.com
首页 > 其他好文 > 详细

对于显示指定LOCK TABLES的并行插入问题(从其它表)

时间:2015-09-05 23:51:44      阅读:197      评论:0      收藏:0      [点我收藏+]

标签:mysql

文档:

If you acquire a table lock explicitly with LOCK TABLES, you can request a READ LOCAL lock rather
than a READ lock to enable other sessions to perform concurrent inserts while you have the table
locked.
To perform many INSERT and SELECT operations on a table real_table when concurrent inserts
are not possible, you can insert rows into a temporary table temp_table and update the real table
with the rows from the temporary table periodically. This can be done with the following code:


mysql> lock table t1;
ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ‘‘ at line 1
mysql> lock table t1 write;
Query OK, 0 rows affected (0.00 sec)

mysql> insert into t1 select * from t2;
ERROR 1100 (HY000): Table ‘t2‘ was not locked with LOCK TABLES
mysql> lock table t2;
ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ‘‘ at line 1
mysql> lock table t2 write;
Query OK, 0 rows affected (0.00 sec)

mysql> insert into t1 select * from t2;
ERROR 1100 (HY000): Table ‘t1‘ was not locked with LOCK TABLES
mysql> lock table t1 write,t2 write;
Query OK, 0 rows affected (0.00 sec)

mysql> insert into t1 select * from t2;
Query OK, 869 rows affected (0.01 sec)
Records: 869  Duplicates: 0  Warnings: 0

mysql> unlocks;
ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ‘unlocks‘ at line 1
mysql> unlock tables;
Query OK, 0 rows affected (0.00 sec)


可以看到:可以将需要插入到T1表的内容放在临时表t2上(temporary table),这样就避免要锁住select后面的表也可以保证插入到表T1中;

对于显示指定LOCK TABLES的并行插入问题(从其它表)

标签:mysql

原文地址:http://itkfc.blog.51cto.com/4716659/1691651

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!