首页 > 其他 > 详细

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

时间:2015-09-05 23:49:24      阅读:335      评论:0      收藏:0      [点我收藏+]

文档:

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的并行插入问题(从其它表)

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

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!