标签: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