二、实践:
1.myisam存储引擎的写阻塞: session_1: mysql> use sakila; Reading table information for completion of table and column names You can turn off this feature to get a quicker startup with -A Database changed mysql> show status like 'table%'; +-----------------------+-------+ | Variable_name | Value | +-----------------------+-------+ | Table_locks_immediate | 1369 | | Table_locks_waited | 1 | +-----------------------+-------+ 2 rows in set (0.01 sec) mysql> lock table film_text write; Query OK, 0 rows affected (0.00 sec) mysql> select film_id,title from film_text where film_id = 1; +---------+-------+ | film_id | title | +---------+-------+ | 1 | test | +---------+-------+ 1 row in set (0.00 sec) mysql> insert into film_text(film_id,title) values(10005,'test111'); Query OK, 1 row affected (0.02 sec) mysql> update film_text set title = 'test333' where film_id = 10005; Query OK, 1 row affected (0.01 sec) Rows matched: 1 Changed: 1 Warnings: 0 mysql> unlock tables; Query OK, 0 rows affected (0.00 sec) session_2: mysql> use sakila; Reading table information for completion of table and column names You can turn off this feature to get a quicker startup with -A Database changed mysql> select film_id,title from film_text where film_id = 10005; ^CCtrl-C -- sending "KILL QUERY 2" to server ... Ctrl-C -- query aborted. ERROR 1317 (70100): Query execution was interrupted mysql> select film_id,title from film_text where film_id = 10005; +---------+---------+ | film_id | title | +---------+---------+ | 10005 | test333 | +---------+---------+ 1 row in set (0.01 sec) 2.myisam存储引擎的读阻塞: session_1: mysql> use sakila; Reading table information for completion of table and column names You can turn off this feature to get a quicker startup with -A Database changed mysql> show status like 'table%'; +-----------------------+-------+ | Variable_name | Value | +-----------------------+-------+ | Table_locks_immediate | 1369 | | Table_locks_waited | 1 | +-----------------------+-------+ 2 rows in set (0.01 sec) mysql> lock table film_text write; ^CCtrl-C -- sending "KILL QUERY 143" to server ... Ctrl-C -- query aborted. ERROR 1317 (70100): Query execution was interrupted mysql> select film_id,title from film_text where film_id = 1; +---------+-------+ | film_id | title | +---------+-------+ | 1 | test | +---------+-------+ 1 row in set (0.00 sec) mysql> insert into film_text(film_id,title) values(10005,'test111'); Query OK, 1 row affected (0.02 sec) mysql> update film_text set title = 'test333' where film_id = 10005; Query OK, 1 row affected (0.01 sec) Rows matched: 1 Changed: 1 Warnings: 0 mysql> unlock tables; Query OK, 0 rows affected (0.00 sec) mysql> lock table film_text read; Query OK, 0 rows affected (0.00 sec) mysql> select film_id,title from film_text where film_id = 1; +---------+-------+ | film_id | title | +---------+-------+ | 1 | test | +---------+-------+ 1 row in set (0.00 sec) mysql> select film_id,title from film_text where film_id = 1; +---------+-------+ | film_id | title | +---------+-------+ | 1 | test | +---------+-------+ 1 row in set (0.00 sec) mysql> unlock tables; Query OK, 0 rows affected (0.00 sec) session_2: mysql> use sakila; Reading table information for completion of table and column names You can turn off this feature to get a quicker startup with -A Database changed mysql> select film_id,title from film_text where film_id = 1; +---------+-------+ | film_id | title | +---------+-------+ | 1 | test | +---------+-------+ 1 row in set (0.00 sec) mysql> select film_id,title from film where film_id = 1; +---------+------------------+ | film_id | title | +---------+------------------+ | 1 | ACADEMY DINOSAUR | +---------+------------------+ 1 row in set (0.00 sec) mysql> update film set tile = 'film_id_1' where film_id = 1; ^CCtrl-C -- sending "KILL QUERY 4" to server ... Ctrl-C -- query aborted. ERROR 1317 (70100): Query execution was interrupted mysql> update film set title = 'film_id_1' where film_id = 1; ^CCtrl-C -- sending "KILL QUERY 4" to server ... Ctrl-C -- query aborted. ERROR 1317 (70100): Query execution was interrupted mysql> update film_text set title = 'test10009' where film_id = 1; ^CCtrl-C -- sending "KILL QUERY 4" to server ... Ctrl-C -- query aborted. ERROR 1317 (70100): Query execution was interrupted mysql> update film_text set title = 'test10009' where film_id = 1; Query OK, 1 row affected (0.01 sec) Rows matched: 1 Changed: 1 Warnings: 0 3.myisam存储引擎的读写并发例子: session_1: mysql> use sakila; Reading table information for completion of table and column names You can turn off this feature to get a quicker startup with -A Database changed mysql> lock table film_text read local; Query OK, 0 rows affected (0.00 sec) mysql> insert into film_text(film_id,title) values (20001,'test'); ERROR 1099 (HY000): Table 'film_text' was locked with a READ lock and can't be updated mysql> update film_text set title = '20001test' where film_id = 20001; ERROR 1099 (HY000): Table 'film_text' was locked with a READ lock and can't be updated mysql> select film_id,title from film_text where film_id = 1; +---------+-----------+ | film_id | title | +---------+-----------+ | 1 | test10009 | +---------+-----------+ 1 row in set (0.01 sec) mysql> unlock tables; Query OK, 0 rows affected (0.00 sec) mysql> select film_id,title from film_text where film_id = 1; +---------+-----------+ | film_id | title | +---------+-----------+ | 1 | test10009 | +---------+-----------+ 1 row in set (0.00 sec) mysql> Ctrl-C -- exit! Aborted abc@ubuntu:~$ session_2: mysql> use sakila; Reading table information for completion of table and column names You can turn off this feature to get a quicker startup with -A Database changed mysql> select film_id,title from film_text where film_id = 1; +---------+-------+ | film_id | title | +---------+-------+ | 1 | test | +---------+-------+ 1 row in set (0.00 sec) mysql> select film_id,title from film where film_id = 1; +---------+------------------+ | film_id | title | +---------+------------------+ | 1 | ACADEMY DINOSAUR | +---------+------------------+ 1 row in set (0.00 sec) mysql> update film set tile = 'film_id_1' where film_id = 1; ^CCtrl-C -- sending "KILL QUERY 4" to server ... Ctrl-C -- query aborted. ERROR 1317 (70100): Query execution was interrupted mysql> update film set title = 'film_id_1' where film_id = 1; ^CCtrl-C -- sending "KILL QUERY 4" to server ... Ctrl-C -- query aborted. ERROR 1317 (70100): Query execution was interrupted mysql> update film_text set title = 'test10009' where film_id = 1; ^CCtrl-C -- sending "KILL QUERY 4" to server ... Ctrl-C -- query aborted. ERROR 1317 (70100): Query execution was interrupted mysql> update film_text set title = 'test10009' where film_id = 1; Query OK, 1 row affected (0.01 sec) Rows matched: 1 Changed: 1 Warnings: 0 mysql> insert into film_text(film_id,title ) values (30001,'test30001'); Query OK, 1 row affected (0.00 sec) mysql> update film_text set title = 'test' where film_id = 1; ^CCtrl-C -- sending "KILL QUERY 4" to server ... Ctrl-C -- query aborted. ERROR 1317 (70100): Query execution was interrupted mysql> update film_text set title = 'test' where film_id = 1; Query OK, 1 row affected (0.01 sec) Rows matched: 1 Changed: 1 Warnings: 0