2023拉

PHP对于mysql 的锁表

上一篇 / 下一篇  2012-08-23 10:32:57 / 个人分类:PHP学习

 

<?php

//author:

mysql_query("lock table table write");

省略...

mysql_query("unlock tables");

LOCK TABLES按照如下方式执行:

1.    按照内部定义的顺序,对所有要被锁定的表进行分类。从用户的角度,此顺序是未经定义的。

2.    如果使用一个读取和一个写入锁定对一个表进行锁定,则把写入锁定放在读取锁定之前。

3.    一次锁定一个表,直到线程得到所有锁定为止。

该规则确保表锁定不会出现死锁定。

MySQL的表级锁有两种模式:表共享读锁(Table Read Lock)和表独占写锁(Table Write Lock)。
----------------------------
当前锁模式 | 读锁 | 写锁 |
----------------------------
读锁       | 是    |   否 |
----------------------------
写锁       | 否    |   否 |
----------------------------   MyISAM表的读操作,不会阻塞其他用户对同一表的读请求,但会阻塞对同一表的写请求;对MyISAM表的写操作,则会阻塞其他用户对同一表的读和写操 作;MyISAM表的读操作与写操作之间,以及写操作之间是串行的!根据如表20-2所示的例子可以知道,当一个线程获得对一个表的写锁后,只有持有锁的 线程可以对表进行更新操作。其他线程的读、写操作都会等待,直到锁被释放为止。查询表级锁争用情况
可以通过检查table_locks_waited和table_locks_immediate状态变量来分析系统上的表锁定争夺.
mysql> show status like 'table%';获得表table_name的WRITE/READ锁定
mysql> lock table table_name write;
mysql> lock table table_name read;
解锁
mysql> unlock tables;如何加表锁
Lock tables orders read local, order_detail read local;
Select sum(total) from orders;
Select sum(subtotal) from order_detail;
Unlock tables;
   上面的例子在LOCK TABLES时加了“local”选项,其作用就是在满足MyISAM表并发插入条件的情况下,允许其他用户在表尾并发插入记录.    当使用LOCK TABLES时,不仅需要一次锁定用到的所有表,而且,同一个表在SQL语句中出现多少次,就要通过与SQL语句中相同的别名锁定多少次,否则也会出错! 举例说明如下。
1.对actor表获得读锁:
mysql> lock table actor read;Query OK, 0 rows affected (0.00 sec)2.但是通过别名访问会提示错误:
mysql> select a.first_name,a.last_name,b.first_name,b.last_name from actor a,actor b where a.first_name = b.first_name and a.first_name = 'Lisa' and a.last_name = 'Tom' and a.last_name <> b.last_name;ERROR 1100 (HY000): Table 'a' was not locked with LOCK TABLES3.需要对别名分别锁定:
mysql> lock table actor as a read,actor as b read;Query OK, 0 rows affected (0.00 sec)4.按照别名的查询可以正确执行:
mysql> select a.first_name,a.last_name,b.first_name,b.last_name from actor a,actor b where a.first_name = b.first_name and a.first_name = 'Lisa' and a.last_name = 'Tom' and a.last_name <> b.last_name;+------------+-----------+------------+-----------+| first_name | last_name | first_name | last_name |+------------+-----------+------------+-----------+| Lisa       | Tom       | LISA       | MONROE    |+------------+-----------+------------+-----------+1 row in set (0.00 sec)


并发插入(Concurrent Inserts)
    在一定条件下,MyISAM表也支持查询和插入操作的并发进行。
MyISAM存储引擎有一个系统变量concurrent_insert,专门用以控制其并发插入的行为,其值分别可以为0、1或2。
1.当concurrent_insert设置为0时,不允许并发插入。
2.当concurrent_insert设置为1时,如果MyISAM表中没有空洞(即表的中间没有被删除的行),MyISAM允许在一个进程读表的同时,另一个进程从表尾插入记录。这也是MySQL的默认设置。
3.当concurrent_insert设置为2时,无论MyISAM表中有没有空洞,都允许在表尾并发插入记录。
    利用MyISAM存储引擎的并发插入特性,来解决应用中对同一表查询和插入的锁争用。例如,将concurrent_insert系统变量设为2,总是允 许并发插入;同时,通过定期在系统空闲时段执行OPTIMIZE TABLE语句来整理空间碎片,收回因删除记录而产生的中间空洞。

mysql> create table t_haosee (aa int);
Query OK, 0 rows affected (0.05 sec)

mysql> LOCK TABLES t_haosee WRITE;
Query OK, 0 rows affected (0.00 sec)

mysql> insert into t_haosee set aa=1;
Query OK, 1 row affected (0.03 sec)

mysql> insert into t_haosee set aa=2;
Query OK, 1 row affected (0.02 sec)

mysql> unlock tables;
Query OK, 0 rows affected (0.00 sec)

mysql> select * from t_haosee;
+------+
| aa   |
+------+
|    1 |
|    2 |
+------+
2 rows in set (0.00 sec)

mysql>


TAG:

 

评分:0

我来说两句

Open Toolbar