13.2.4 HANDLER Statement语句

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

The HANDLER statement provides direct access to table storage engine interfaces. HANDLER语句提供对表存储引擎接口的直接访问。It is available for InnoDB and MyISAM tables.它可用于InnoDBMyISAM表。

The HANDLER ... OPEN statement opens a table, making it accessible using subsequent HANDLER ... READ statements. HANDLER ... OPEN语句打开一个表,使它能被后续的HANDLER ... READ语句访问。This table object is not shared by other sessions and is not closed until the session calls HANDLER ... CLOSE or the session terminates.此表对象不被其他会话共享,并且在会话调用HANDLER ... CLOSE之前或会话终止之前不会关闭。

If you open the table using an alias, further references to the open table with other HANDLER statements must use the alias rather than the table name. 如果使用别名打开表,则使用其他HANDLER语句对打开表的进一步引用必须使用别名而不是表名。If you do not use an alias, but open the table using a table name qualified by the database name, further references must use the unqualified table name. 如果不使用别名,而是使用数据库名称限定的表名打开表,则进一步引用必须使用限定的表名。For example, for a table opened using mydb.mytable, further references must use mytable.例如,对于使用mydb.mytable打开的表,进一步的引用必须使用mytable

The first HANDLER ... READ syntax fetches a row where the index specified satisfies the given values and the WHERE condition is met. 第一个HANDLER ... READ语法获取指定索引满足给定值且满足WHERE条件的行。If you have a multiple-column index, specify the index column values as a comma-separated list. 如果有多列索引,请将索引列值指定为逗号分隔的列表。Either specify values for all the columns in the index, or specify values for a leftmost prefix of the index columns. 为索引中的所有列指定值,或为索引列的最左侧前缀指定值。Suppose that an index my_idx includes three columns named col_a, col_b, and col_c, in that order. 假设索引my_idx包含三列,按顺序命名为col_acol_bcol_cThe HANDLER statement can specify values for all three columns in the index, or for the columns in a leftmost prefix. HANDLER语句可以为索引中的所有三列指定值,也可以为最左侧前缀中的列指定值。For example:例如:

HANDLER ... READ my_idx = (col_a_val,col_b_val,col_c_val) ...
HANDLER ... READ my_idx = (col_a_val,col_b_val) ...
HANDLER ... READ my_idx = (col_a_val) ...

To employ the HANDLER interface to refer to a table's PRIMARY KEY, use the quoted identifier `PRIMARY`:要使用HANDLER接口引用表的主键,请使用带引号的标识符`PRIMARY`

HANDLER tbl_name READ `PRIMARY` ...

The second HANDLER ... READ syntax fetches a row from the table in index order that matches the WHERE condition.第二个HANDLER ... READ语法按与WHERE条件匹配的索引顺序从表中获取行。

The third HANDLER ... READ syntax fetches a row from the table in natural row order that matches the WHERE condition. 第三个HANDLER ... READ语法按与WHERE条件匹配的自然行顺序从表中获取行。It is faster than HANDLER tbl_name READ index_name when a full table scan is desired. 当需要全表扫描时,它比HANDLER tbl_name READ index_name要快。Natural row order is the order in which rows are stored in a MyISAM table data file. 自然行顺序是在MyISAM表数据文件中存储行的顺序。This statement works for InnoDB tables as well, but there is no such concept because there is no separate data file.这个语句也适用于InnoDB表,但是没有这样的概念,因为没有单独的数据文件。

Without a LIMIT clause, all forms of HANDLER ... READ fetch a single row if one is available. 没有LIMIT子句,HANDLER ... READ的所有形式,如果一行可用,则只读取一行。To return a specific number of rows, include a LIMIT clause. 要返回特定数量的行,请包含LIMIT子句。It has the same syntax as for the SELECT statement. 它的语法与SELECT语句相同。See Section 13.2.10, “SELECT Statement”.请参阅第13.2.10节,“SELECT语句”。

HANDLER ... CLOSE closes a table that was opened with HANDLER ... OPEN.HANDLER ... CLOSE关闭用HANDLER ... OPEN打开的表。

There are several reasons to use the HANDLER interface instead of normal SELECT statements:使用HANDLER接口而不是普通的SELECT语句有几个原因:

HANDLER is a somewhat low-level statement. HANDLER是一个有点低级的语句。For example, it does not provide consistency. 例如,它不提供一致性。That is, HANDLER ... OPEN does not take a snapshot of the table, and does not lock the table. 也就是说,HANDLER ... OPEN不获取表的快照,也不锁定表。This means that after a HANDLER ... OPEN statement is issued, table data can be modified (by the current session or other sessions) and these modifications might be only partially visible to HANDLER ... NEXT or HANDLER ... PREV scans.这意味着在HANDLER ... OPEN语句发出之后,可以修改表数据(由当前会话或其他会话修改),并且这些修改可能仅对HANDLER ... NEXT扫描或HANDLER ... PREV扫描部分可见。

An open handler can be closed and marked for reopen, in which case the handler loses its position in the table. 打开的处理程序可以关闭并标记为重新打开,在这种情况下,处理程序将丢失其在表中的位置。This occurs when both of the following circumstances are true:当以下两种情况都成立时,就会发生这种情况:

TRUNCATE TABLE for a table closes all handlers for the table that were opened with HANDLER OPEN.TRUNCATE TABLE对某个表关闭在HANDLER OPEN中打开的表的所有处理程序。

If a table is flushed with FLUSH TABLES tbl_name WITH READ LOCK was opened with HANDLER, the handler is implicitly flushed and loses its position.如果用HANDLER打开FLUSH TABLES tbl_name WITH READ LOCK,则处理程序将隐式刷新并丢失其位置。