HANDLER tbl_name OPEN [ [AS] alias]
HANDLER tbl_name READ index_name { = | <= | >= | < | > } (value1,value2,...)
[ WHERE where_condition ] [LIMIT ... ]
HANDLER tbl_name READ index_name { FIRST | NEXT | PREV | LAST }
[ WHERE where_condition ] [LIMIT ... ]
HANDLER tbl_name READ { FIRST | NEXT }
[ WHERE where_condition ] [LIMIT ... ]
HANDLER tbl_name CLOSE
通过HANDLER tbl_name OPEN打开一张表,无返回结果,实际上我们在这里声明了一个名为tb1_name的句柄。
3 实例分析
3.1 创建测试表及测试数据
create table handler_table(
c1 int,
c2 varchar(10),
c3 int(10)
);
insert into handler_table values(2, 'name2', 002);
insert into handler_table values(5, 'name5', 005);
insert into handler_table values(1, 'name1', 001);
insert into handler_table values(4, 'name4', 004);
insert into handler_table values(3, 'name3', 003);
3.2 不通过索引打开查看表
打开句柄:
mysql> handler handler_table open;
查看表数据:
mysql> handler handler_table read first;
+------+-------+------+
| c1 | c2 | c3 |
+------+-------+------+
| 2 | name2 | 2 |
+------+-------+------+
mysql> handler handler_table read next;
+------+-------+------+
| c1 | c2 | c3 |
+------+-------+------+
| 5 | name5 | 5 |
+------+-------+------+
mysql> handler handler_table read next;
+------+-------+------+
| c1 | c2 | c3 |
+------+-------+------+
| 1 | name1 | 1 |
+------+-------+------+
mysql> handler handler_table read next;
+------+-------+------+
| c1 | c2 | c3 |
+------+-------+------+
| 4 | name4 | 4 |
+------+-------+------+
mysql> handler handler_table read next;
+------+-------+------+
| c1 | c2 | c3 |
+------+-------+------+
| 3 | name3 | 3 |
+------+-------+------+
mysql> handler handler_table read next;
Empty set (0.00 sec)
关闭句柄:
mysql> handler handler_table close;
Query OK, 0 rows affected (0.00 sec)
3.3 通过索引打开查看表(FIRST,NEXT,PREV,LAST)
通过索引查看的话,可以按照索引的升序,从小到大,查看表信息。
创建索引:
mysql> create index handler_index on handler_table(c1);
打开句柄:
mysql> handler handler_table open as p;
查看表数据:
mysql> handler p read handler_index first;
+------+-------+------+
| c1 | c2 | c3 |
+------+-------+------+
| 1 | name1 | 1 |
+------+-------+------+
mysql> handler p read handler_index next;
+------+-------+------+
| c1 | c2 | c3 |
+------+-------+------+
| 2 | name2 | 2 |
+------+-------+------+
mysql> handler p read handler_index next;
+------+-------+------+
| c1 | c2 | c3 |
+------+-------+------+
| 3 | name3 | 3 |
+------+-------+------+
mysql> handler p read handler_index next;
+------+-------+------+
| c1 | c2 | c3 |
+------+-------+------+
| 4 | name4 | 4 |
+------+-------+------+
mysql> handler p read handler_index next;
+------+-------+------+
| c1 | c2 | c3 |
+------+-------+------+
| 5 | name5 | 5 |
+------+-------+------+
mysql> handler p read handler_index prev;
+------+-------+------+
| c1 | c2 | c3 |
+------+-------+------+
| 4 | name4 | 4 |
+------+-------+------+
mysql> handler p read handler_index last;
+------+-------+------+
| c1 | c2 | c3 |
+------+-------+------+
| 5 | name5 | 5 |
+------+-------+------+
关闭句柄:
mysql> handler p close;
3.4 通过索引打开查看表(=,<=,>=,<,>)
打开句柄:
mysql> handler handler_table open as p;
查看表数据:
mysql> handler p read handler_index = (2);
+------+-------+------+
| c1 | c2 | c3 |
+------+-------+------+
| 2 | name2 | 2 |
+------+-------+------+
mysql> handler p read handler_index next;
+------+-------+------+
| c1 | c2 | c3 |
+------+-------+------+
| 3 | name3 | 3 |
+------+-------+------+
mysql> handler p read handler_index next;
+------+-------+------+
| c1 | c2 | c3 |
+------+-------+------+
| 4 | name4 | 4 |
+------+-------+------+
mysql> handler p read handler_index next;
+------+-------+------+
| c1 | c2 | c3 |
+------+-------+------+
| 5 | name5 | 5 |
+------+-------+------+
mysql> handler p read handler_index last;
+------+-------+------+
| c1 | c2 | c3 |
+------+-------+------+
| 5 | name5 | 5 |
+------+-------+------+
关闭句柄:
mysql> handler p close;
3.5 附加:语法实例参考
handler handler_table open;
handler handler_table open as p;
handler handler_table read first;
handler handler_table read next;
handler handler_table read first limit 3;
handler handler_table read next limit 3,3;
handler handler_table read first where c1 > 2 limit 2;
handler handler_table read next where c1 >2 limit 1,2;
create index handler_index on handler_table(c1);
handler handler_table open;
handler handler_table read handler_index first;
handler handler_table read handler_index next limit 3;
handler handler_table read handler_index PREV limit 3,3;
handler handler_table read handler_index LAST where c1 > 2 limit 2;
handler handler_table read handler_index LAST where c1 > 2 limit 1,2;
handler handler_table read handler_index = (3);
handler handler_table read handler_index <= (3) limit 2;
handler handler_table read handler_index >= (3) limit 1,2;
handler handler_table read handler_index < (4) where c1 > 0 limit 2;
handler handler_table read handler_index > (1) where c1 < 6 limit 2,2;
handler handler_table close;
drop index handler_index on handler_table;
4 handler与select的比较
4.1 select语句一次返回所有相关行,handler每次返回一行。
4.2 HANDLER涉及的分析较少,比SELECT更快
4.3 没有优化程序或查询校验开销
4.4 在两个管理程序请求之间,不需要锁定表。
5 注意事项
5.1 如果一个应用停止了,所有仍然打开的句柄将自动停止。
5.2 handler不支持分区表。
5.3 执行TRUNCATE TABLE会关闭所有在该表上打开的handler。
5.4 handler打开一个表时不锁表,也不对表进行快照,所以当表中数据实时更新时,handler将会失去指针的位置。
****************************************************************************************
原文地址:http://blog.csdn.net/jesseyoung/article/details/40785137
博客主页:http://blog.csdn.net/jesseyoung
****************************************************************************************