這篇文章給大家介紹怎么進(jìn)行MySQL 5.5 MyISAM表鎖測試,內(nèi)容非常詳細(xì),感興趣的小伙伴們可以參考借鑒,希望對大家能有所幫助。
成都創(chuàng)新互聯(lián)公司專業(yè)為企業(yè)提供加格達(dá)奇網(wǎng)站建設(shè)、加格達(dá)奇做網(wǎng)站、加格達(dá)奇網(wǎng)站設(shè)計(jì)、加格達(dá)奇網(wǎng)站制作等企業(yè)網(wǎng)站建設(shè)、網(wǎng)頁設(shè)計(jì)與制作、加格達(dá)奇企業(yè)網(wǎng)站模板建站服務(wù),十年加格達(dá)奇做網(wǎng)站經(jīng)驗(yàn),不只是建網(wǎng)站,更提供有價(jià)值的思路和整體網(wǎng)絡(luò)服務(wù)。
對于MyISAM表,加的鎖是表級鎖;寫操作會阻塞讀操作,讀操作會阻塞寫操作,寫操作會阻塞寫操作;讀操作不會阻塞讀操作。
測試一,會話①的讀操作阻塞會話②的寫操作
會話①
mysql> create table t2(id tinyint(3) unsigned not null auto_increment,
-> name varchar(10) not null,
-> primary key(id))
-> engine=myisam auto_increment=8 default charset=gbk;
Query OK, 0 rows affected (0.03 sec)
mysql> insert into t2(name) values('Neo');
Query OK, 1 row affected (0.03 sec)
mysql> insert into t2(name) values('Trinity');
Query OK, 1 row affected (0.00 sec)
mysql> select * from t2;
+----+---------+
| id | name |
+----+---------+
| 8 | Neo |
| 9 | Trinity |
+----+---------+
2 rows in set (0.00 sec)
mysql> desc t2;
+-------+---------------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-------+---------------------+------+-----+---------+----------------+
| id | tinyint(3) unsigned | NO | PRI | NULL | auto_increment |
| name | varchar(10) | NO | | NULL | |
+-------+---------------------+------+-----+---------+----------------+
2 rows in set (0.09 sec)
為表增加讀鎖
mysql> lock table t2 read;
Query OK, 0 rows affected (0.00 sec)
會話②
mysql> select * from t2;
+----+---------+
| id | name |
+----+---------+
| 8 | Neo |
| 9 | Trinity |
+----+---------+
2 rows in set (0.00 sec)
會話會處于等待狀態(tài)
mysql> update t2 set name='James' where id=5;
會話①
mysql> show processlist;
+----+-----------------+-----------+------+---------+--------+-----------------------------------------------------------------------------+---------------------------------------+
| Id | User | Host | db | Command | Time | State | Info |
+----+-----------------+-----------+------+---------+--------+-----------------------------------------------------------------------------+---------------------------------------+
| 1 | system user | | NULL | Connect | 748155 | Slave has read all relay log; waiting for the slave I/O thread to update it | NULL |
| 2 | system user | | NULL | Connect | 748156 | Connecting to master | NULL |
| 13 | event_scheduler | localhost | NULL | Daemon | 600105 | Waiting on empty queue | NULL |
| 74 | neo | localhost | fire | Query | 0 | NULL | show processlist |
| 75 | neo | localhost | fire | Query | 83 | Waiting for table level lock | update t2 set name='James' where id=5 |
+----+-----------------+-----------+------+---------+--------+-----------------------------------------------------------------------------+---------------------------------------+
5 rows in set (0.00 sec)
mysql> unlock tables;
Query OK, 0 rows affected (0.00 sec)
會話②
mysql> update t2 set name='James' where id=5;
Query OK, 0 rows affected (1 min 48.96 sec)
Rows matched: 0 Changed: 0 Warnings: 0
測試二,會話①的寫操作阻塞會話②的讀操作
會話①
mysql> lock table t2 write;
Query OK, 0 rows affected (0.00 sec)
mysql> insert into t2(name) values('Jack');
Query OK, 1 row affected (0.01 sec)
mysql> commit;
Query OK, 0 rows affected (0.00 sec)
mysql> show processlist;
+----+-----------------+-----------+------+---------+--------+-----------------------------------------------------------------------------+------------------+
| Id | User | Host | db | Command | Time | State | Info |
+----+-----------------+-----------+------+---------+--------+-----------------------------------------------------------------------------+------------------+
| 1 | system user | | NULL | Connect | 748422 | Slave has read all relay log; waiting for the slave I/O thread to update it | NULL |
| 2 | system user | | NULL | Connect | 748423 | Connecting to master | NULL |
| 13 | event_scheduler | localhost | NULL | Daemon | 600372 | Waiting on empty queue | NULL |
| 74 | neo | localhost | fire | Query | 0 | NULL | show processlist |
| 75 | neo | localhost | fire | Query | 2 | Waiting for table metadata lock | select * from t2 |
+----+-----------------+-----------+------+---------+--------+-----------------------------------------------------------------------------+------------------+
5 rows in set (0.00 sec)
會話②
查詢會阻塞
mysql> select * from t2;
會話①
mysql> unlock tables;
Query OK, 0 rows affected (0.00 sec)
會話②
mysql> select * from t2;
+----+---------+
| id | name |
+----+---------+
| 8 | Neo |
| 9 | Trinity |
| 10 | Jack |
+----+---------+
3 rows in set (47.89 sec)
當(dāng)同一個(gè)會話中,鎖定一張表后,查詢另外一張表會報(bào)錯(cuò)
mysql> lock table test read;
Query OK, 0 rows affected (0.00 sec)
mysql> select * from test where id=80;
+------+------+
| id | name |
+------+------+
| 80 | Lily |
+------+------+
1 row in set (0.00 sec)
mysql> select * from t70;
ERROR 1100 (HY000): Table 't70' was not locked with LOCK TABLES
使用表的別名會報(bào)錯(cuò)
mysql> lock table test read;
Query OK, 0 rows affected (0.00 sec)
mysql> select * from test t where id=80;
ERROR 1100 (HY000): Table 't' was not locked with LOCK TABLES
需要對別名進(jìn)行鎖定
mysql> lock table test t read;
Query OK, 0 rows affected (0.00 sec)
mysql> select * from test t where id=80;
+------+------+
| id | name |
+------+------+
| 80 | Kame |
+------+------+
1 row in set (0.00 sec)
關(guān)于怎么進(jìn)行MySQL 5.5 MyISAM表鎖測試就分享到這里了,希望以上內(nèi)容可以對大家有一定的幫助,可以學(xué)到更多知識。如果覺得文章不錯(cuò),可以把它分享出去讓更多的人看到。
網(wǎng)頁標(biāo)題:怎么進(jìn)行MySQL5.5MyISAM表鎖測試
本文鏈接:http://aaarwkj.com/article26/pcdecg.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站制作、App開發(fā)、品牌網(wǎng)站設(shè)計(jì)、微信公眾號、用戶體驗(yàn)、軟件開發(fā)
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)