Archive for the Category » 程序开发 «

星期日, 12月 14th, 2008 | Author: Joshua

其实二者没有什么可比性,但是对于不包含聚集函数的GROUP BY操作来说,和DISTINCT操作是等价的。不过虽然二者的结果是一样的,但是二者的执行计划并不相同。

在Oracle9i中:

SQL> SELECT * FROM V$VERSION;

BANNER
—————————————————————-
Oracle9i Enterprise Edition Release 9.2.0.4.0 - Production PL/SQL Release 9.2.0.4.0 - Production
CORE 9.2.0.3.0 Production
TNS for Linux: Version 9.2.0.4.0 - Production
NLSRTL Version 9.2.0.4.0 - Production

SQL> CREATE TABLE T AS SELECT ROWNUM ID, A.* FROM DBA_OBJECTS A;

表已创建。

SQL> CREATE INDEX IND_T_CREATED ON T (CREATED);

索引已创建。

SQL> ALTER TABLE T MODIFY CREATED NOT NULL;

表已更改。

SQL> ALTER SESSION SET NLS_DATE_FORMAT = ‘YYYY-MM-DD HH24:MI:SS’;

会话已更改。

SQL> EXEC DBMS_STATS.GATHER_TABLE_STATS(USER, ‘T’)

PL/SQL 过程已成功完成。

SQL> SET AUTOT ON EXP
SQL> SELECT COUNT(*) FROM (SELECT DISTINCT CREATED FROM T);

COUNT(*)
———-
4794

执行计划
———————————————————-
0 SELECT STATEMENT Optimizer=CHOOSE (Cost=65 Card=1)
1 0 SORT (AGGREGATE)
2 1 VIEW (Cost=65 Card=4794)
3 2 SORT (UNIQUE) (Cost=65 Card=4794 Bytes=38352)
4 3 INDEX (FAST FULL SCAN) OF ‘IND_T_CREATED’ (NON-UNIQUE) (Cost=4 Card=41802 Bytes=334416)

SQL> SELECT COUNT(*) FROM (SELECT CREATED FROM T GROUP BY CREATED);

COUNT(*)
———-
4794

执行计划
———————————————————-
0 SELECT STATEMENT Optimizer=CHOOSE (Cost=65 Card=1 Bytes=2)
1 0 SORT (AGGREGATE)
2 1 VIEW (Cost=65 Card=4794 Bytes=9588)
3 2 SORT (GROUP BY) (Cost=65 Card=4794 Bytes=38352)
4 3 INDEX (FAST FULL SCAN) OF ‘IND_T_CREATED’ (NON-UNIQUE) (Cost=4 Card=41802 Bytes=334416)

从执行计划上看,DISTINCT的操作是SORT (UNIQUE),而GROUP BY是SORT (GROUP BY)。DISTINCT操作只需要找出所有不同的值就可以了。而GROUP BY操作还要为其他聚集函数进行准备工作。从这一点上将,GROUP BY操作做的工作应该比DISTINCT所做的工作要多一些。

除了这一点,基本上看不到DISTINCT和GROUP BY(没有聚集函数的情况)有什么区别,而且从执行效率上也看不到明显的差异。

不过从10g开始,二者的差异开始体现出来了。

SQL> CONN YANGTK/YANGTK@YTK已连接。
SQL> SET AUTOT OFF
SQL> SET TIMING OFF
SQL> CREATE TABLE T AS SELECT ROWNUM ID, A.* FROM DBA_OBJECTS A;

表已创建。

SQL> CREATE INDEX IND_T_CREATED ON T (CREATED);

索引已创建。

SQL> ALTER TABLE T MODIFY CREATED NOT NULL;

表已更改。

SQL> ALTER SESSION SET NLS_DATE_FORMAT = ‘YYYY-MM-DD HH24:MI:SS’;

会话已更改。

SQL> EXEC DBMS_STATS.GATHER_TABLE_STATS(USER, ‘T’)

PL/SQL 过程已成功完成。

SQL> SET AUTOT ON
SQL> SET TIMING ON

建立好测试环境后,看一看标准分页函数中,两个操作的差异:

SQL> SELECT *
2 FROM
3 (
4 SELECT ROWNUM RN, A.*
5 FROM
6 (
7 SELECT CREATED
8 FROM T
9 GROUP BY CREATED
10 ) A
11 WHERE ROWNUM < 20
12 )
13 WHERE RN >= 10;

RN CREATED
———- ——————-
10 2005-12-19 17:07:57
11 2005-12-19 17:07:58
12 2005-12-19 17:08:24
13 2005-12-19 17:08:25
14 2005-12-19 17:08:26
15 2005-12-19 17:08:27
16 2005-12-19 17:08:28
17 2005-12-19 17:08:29
18 2005-12-19 17:08:33
19 2005-12-19 17:08:35

已选择10行。

已用时间: 00: 00: 00.06

执行计划
———————————————————-
Plan hash value: 3639065582

——————————————————————————-
| Id | Operation | Name | Rows | Bytes | Cost (%CPU)|
——————————————————————————-
| 0 | SELECT STATEMENT | | 19 | 418 | 1 (0)|
|* 1 | VIEW | | 19 | 418 | 1 (0)|
|* 2 | COUNT STOPKEY | | | | |
| 3 | VIEW | | 969 | 8721 | 1 (0)|
|* 4 | SORT GROUP BY STOPKEY| | 969 | 7752 | 1 (0)|
| 5 | INDEX FULL SCAN | IND_T_CREATED | 969 | 7752 | 1 (0)|
——————————————————————————-

Predicate Information (identified by operation id):
—————————————————

1 - filter(”RN”>=10)
2 - filter(ROWNUM<20)
4 - filter(ROWNUM<20)

统计信息
———————————————————-
1 recursive calls
0 db block gets
67 consistent gets
0 physical reads
0 redo size
642 bytes sent via SQL*Net to client
385 bytes received via SQL*Net from client
2 SQL*Net roundtrips to/from client
1 sorts (memory)
0 sorts (disk)
10 rows processed

SQL> SELECT *
2 FROM
3 (
4 SELECT ROWNUM RN, A.*
5 FROM
6 (
7 SELECT DISTINCT CREATED
8 FROM T
9 ) A
10 WHERE ROWNUM < 20
11 )
12 WHERE RN >= 10;

RN CREATED
———- ——————-
10 2005-12-19 17:07:57
11 2005-12-19 17:07:58
12 2005-12-19 17:08:24
13 2005-12-19 17:08:25
14 2005-12-19 17:08:26
15 2005-12-19 17:08:27
16 2005-12-19 17:08:28
17 2005-12-19 17:08:29
18 2005-12-19 17:08:33
19 2005-12-19 17:08:35

已选择10行。

已用时间: 00: 00: 00.03

执行计划
———————————————————-
Plan hash value: 1650124153

——————————————————————————-
| Id | Operation | Name | Rows | Bytes | Cost (%CPU)|
——————————————————————————-
| 0 | SELECT STATEMENT | | 19 | 418 | 14 (36)|
|* 1 | VIEW | | 19 | 418 | 14 (36)|
|* 2 | COUNT STOPKEY | | | | |
| 3 | VIEW | | 987 | 8883 | 14 (36)|
|* 4 | SORT GROUP BY STOPKEY| | 987 | 7896 | 14 (36)|
| 5 | INDEX FAST FULL SCAN| IND_T_CREATED | 50333 | 393K| 10 (10)|
——————————————————————————-

Predicate Information (identified by operation id):
—————————————————

1 - filter(”RN”>=10)
2 - filter(ROWNUM<20)
4 - filter(ROWNUM<20)

统计信息
———————————————————-
1 recursive calls
0 db block gets
73 consistent gets
0 physical reads
0 redo size
642 bytes sent via SQL*Net to client
385 bytes received via SQL*Net from client
2 SQL*Net roundtrips to/from client
1 sorts (memory)
0 sorts (disk)
10 rows processed

出乎意料的是,GROUP BY操作的COST更低,而且逻辑读也小,这似乎与二者的工作量成反比。仔细观察执行计划发现,问题的根源来自于GROUP BY使用INDEX FULL SCAN,而DISTINCT使用了INDEX FAST FULL SCAN。也许有人会感到奇怪,索引的快速全扫描不是要比索引全扫描效率更高吗?对于读取所有数据的情况下,确实是索引快速全扫效率更高。但是由于这里采用了分页,只取前20条数据,而且Oracle的10g增加了GROUP BY STOPKEY这种新的执行路径,因此在这里GROUP BY操作的效率更高。

观察执行计划中的处理行数可以发现,索引全扫描由于是按照索引的顺序扫描,因此利用了STOPKEY,仅仅处理了969条记录就停了下来。而对于DISTINCT操作的快速索引全速而言,显然没有使用STOPKEY,读取了所有的50333条记录。这就是GROUP BY和DISTINCT的性能差异原因。

Category: 数据库技术  | Tags: ,  | Leave a Comment
星期二, 12月 09th, 2008 | Author: Joshua

range($low, $high),range($low, $high, $step);//创建顺序值的数组如:range(1,4)为(1,2,3,4)又如range(’a',’z')

each($array)按顺序返回数组的当前元素,并且将下一个元素设置为当前元素;

reset($array)将数组当前元素重新设置到数组开始处

list()可以用来将一个数组分解为一系列的值,如 list($a,$b)=each($array)

shuffle($array),array_rand($arg, $num_req);对数组随机排序

array_reverse($input),array_reverse($input, $preserve_keys) 返回原数组的反向排序

sort($array);对数组排序

PHP数组是一个重要的概念,它包含有大量的函数,方便人们的开发…现将它的数组分类,以方便查询及应用.
先说说PHP数组的定义…PHP数组包含两个项,key和value,可以通过key来获取相应的value,其中key又可以是数值和关联的,如$array[0],$array[one]…
创建数组
PHP中的数组声明跟其它语言的也有点小小的差别,但一样可以声明为一维,两维,三维及多维等,如
$array[0] = 1,$array = array(1,2,3); 一维数组,只包括三个值,属于数值型数组,引用时可用$array[0]来代表1,创建数值数组时可以省略索引.
$array = array(
1 => “one”,
2 => “two”,
3 => “three”,
4 => array(
“one” => 1,
“two” => 2,
“three” => 3
)
);
二维数组,同时又是关联数组,引用时可以$array[4][“one”]来代表1.
三维以上依此类推…
如果要批量创建数组,则可以通过下面这个函数:
array range ( mixed low, mixed high [, number step] )
如$array = range(1,6);代表array(1,2,3,4,5,6);
$array = range(a,f); 代表 array(a,b,c,d,e,f);

输出数组
PHP中输出数组的函数有比较多,常用的有
bool print_r ( mixed expression [, bool return] )
void var_dump ( mixed expression [, mixed expression [, ...]] )
还有像echo,print,printf都可以输出单个数组.

测试数组
有时我们需要判定一个变量是否为数组,则可以使用:
bool is_array ( mixed var )

增加或删除数组元
数组声明后并不是一成不变的,可能通过对数组的增加删除来进行深入的操作:
int array_push ( array &array, mixed var [, mixed ...] ) 将一个或多个单元压入数组的末尾,数组的长度根据入栈变量的数目增加,如array_push($array,$var)
mixed array_pop ( array &array ) 将数组的最后一个元素弹出(出栈),并在结束后重置数组的指针
mixed array_shift ( array &array ) 返回数组的第一个元素.
int array_unshift ( array &array, mixed var [, mixed ...] ) 在数组的开头插入一个或多个单元
array array_pad ( array input, int pad_size, mixed pad_value ) 用值将数组填补到指定的长度,如array_pad($array,3,$var);

定位数组元
bool in_array ( mixed needle, array haystack [, bool strict] ) 检查数组中是否存在某个值
array array_keys ( array input [, mixed search_value [, bool strict]] ) 返回数组中的所有键名,重组成一个新数组
bool array_key_exists ( mixed key, array search ) 检查给定的key是否存在于数组中.
array array_values ( array input ) 返回数组中所有的值
mixed array_search ( mixed needle, array haystack [, bool strict] ) 在数组中搜索给定的值,成功则返回key.

遍历数组
PHP中提供了很多获取key和value的函数
mixed key ( array &array ) 从关联数组中取得键名
mixed reset ( array &array ) 将数组指针重置
array each ( array &array ) 返回数组中的键/值对并将数组向前移一步
mixed current ( array &array ) 返回数组中的当前单元
mixed end ( array &array ) 将数组中的指针移向最后一位
mixed next ( array &array ) 将数组中的指针移向下一位
mixed prev ( array &array ) 将数组中的指针移向上一位
array array_reverse ( array array [, bool preserve_keys] ) 返回一个单元顺序相反的数组
array array_flip ( array trans ) 将数组中的键值角色调换
除了上面的函数外还可以使用循环来对数组中的元素进行遍历,如
foreach (array_expr as $value)
{ statement }
foreach (array_expr as $key=>$value)
{ statement }
提取每个键/值对,直到获得所有项或满足某些内部条件为止
void list ( mixed varname, mixed … ) 把数组中的值赋给一些变量

确定数组大小和唯一性
int count ( mixed var [, int mode] ) 计算数组中单元数组或对象中属性的个数, sizeof 的同名函数
array array_count_values ( array input ) 统计数组中所有值出现的次数
array array_unique ( array array ) 移除数组中重复的值

数组排序
这个听说是计算器的核心问题…呵呵…事实也是这样…
bool sort ( array &array [, int sort_flags] ) 对数组进行排序
bool natsort ( array &array ) 用自然排序法对数组进行排序
bool natcasesort ( array &array ) 用自然排序法对数组进行排序,不区分大小写
bool rsort ( array &array [, int sort_flags] ) 对数组进行逆向排序
bool asort ( array &array [, int sort_flags] ) 对数组进行排序并保持索引关系
bool array_multisort ( array ar1 [, mixed arg [, mixed ... [, array ...]]] ) 对多个数组或多维数组进行排序
bool arsort ( array &array [, int sort_flags] ) 对数组进行逆序排序并保持索引关系
bool ksort ( array &array [, int sort_flags] ) 对数组按键名排序
bool krsort ( array &array [, int sort_flags] ) 对数组按键名逆序排序

合并,拆分,接合和分解数组
array array_combine ( array keys, array values ) 创建一个数组,一个数组的值作为其键名,另一个数组的值作为其值
array array_merge ( array array1 [, array array2 [, array ...]] ) 合并一个或多个数组
array array_merge_recursive ( array array1 [, array ...] ) 递归地全部一个或多个数组
array array_slice ( array array, int offset [, int length [, bool preserve_keys]] ) 从数组中取出一段,建立一个新的数组,如果offset为正数,拆分从距数组开关的offset位置开始,如果为负数,则拆分从距数组末尾的offset 位置开始,此时距数组开关的count(input_array)-|length|位置结束
array array_splice ( array &input, int offset [, int length [, array replacement]] ) 把数组中的部分值去掉,并用其它值替代.offset设置同上
array array_intersect ( array array1, array array2 [, array ...] ) 计算数组的交集,即是说如果第一个数组中出现过的值在接下来的几个数组中都有出现,则取出该值
array array_intersect_assoc ( array array1, array array2 [, array ...] ) 带索引检查数组中的交集
array array_intersect_key ( array array1, array array2 [, array ...] ) 使用键名比较数组中的交集
array array_diff ( array array1, array array2 [, array ...] ) 计算数组的差集, 即是说跟第一个数组中不同的值
array array_diff_assoc ( array array1, array array2 [, array ...] ) 带索引检查数组中的差集
array array_diff_key ( array array1, array array2 [, array ...] ) 使用键名比较数组中的差集

其它比较有用的数组函数
数组函数还有好多没有列出来…再上几个比较有用也比较常的,其它的就参考手册啦…手册里很清楚
mixed array_rand ( array input [, int num_req] ) 数组中随机取出一个或多个键,num指定个数
bool shuffle ( array &array ) 将数组打乱
number array_sum ( array array ) 计算数组中所有值的总和,关联数组忽略
array array_chunk ( array input, int size [, bool preserve_keys] ) 将一个数组分割成几个

Category: PHP  | Tags: ,  | Leave a Comment
星期三, 12月 03rd, 2008 | Author: Joshua
协议
Protocol
memcached 的客户端使用TCP链接 与 服务器通讯。(UDP接口也同样有效,参考后文的 “UDP协议” )一个运行中的memcached服务器监视一些(可设置)端口。客户端连接这些端口,发送命令到服务器,读取回应,最后关闭连接。 Clients of memcached communicate with server through TCP connections. (A UDP interface is also available; details are below under “UDP protocol.”) A given running memcached server listens on some (configurable) port; clients connect to that port, send commands to the server, read responses, and eventually close the connection.
结束会话不需要发送任何 命令。当不再需memcached服务时,要客户端可以在任何时候关闭连接。需要注意的是,鼓励客户端缓存这些连接,而不是每次需要存取数据时都重新打开 连接。这是因为memcached 被特意设计成及时开启很多连接也能够高效的工作(数百个,上千个如果需要的话)。缓存这些连接,可以消除建立连接所带来的开销(/*/相对而言,在服务器 端建立一个新连接的准备工作所带来的开销,可以忽略不计。)。 There is no need to send any command to end the session. A client may just close the connection at any moment it no longer needs it. Note, however, that clients are encouraged to cache their connections rather than reopen them every time they need to store or retrieve data. This is because memcached is especially designed to work very efficiently with a very large number (many hundreds, more than a thousand if necessary) of open connections. Caching connections will eliminate the overhead associated with establishing a TCP connection (the overhead of preparing for a new connection on the server side is insignificant compared to this).
在memcache协议 中发送的数据分两种:文本行 和 自由数据。 文本行被用于来自客户端的命令和服务器的回应。自由数据用于客户端从服务器端存取数据时。同样服务器会以字节流的方式传回自由数据。/*/服务器不用关心 自由数据的字节顺序。自由数据的特征没有任何限制;但是通过前文提到的文本行,这项数据的接受者(服务器或客户端),便能够精确地获知所发送的数据库的长 度。 There are two kinds of data sent in the memcache protocol: text lines
and unstructured data. Text lines are used for commands from clients
and responses from servers. Unstructured data is sent when a client
wants to store or retrieve data. The server will transmit back
unstructured data in exactly the same way it received it, as a byte
stream. The server doesn’t care about byte order issues in
unstructured data and isn’t aware of them. There are no limitations on
characters that may appear in unstructured data; however, the reader
of such data (either a client or a server) will always know, from a
preceding text line, the exact length of the data block being
transmitted.
文本行固定以“\r \n”(回车符紧跟一个换行符)结束。 自由数据也是同样会以“\r\n”结束,但是 \r(回车符)、\n(换行符),以及任何其他8位字符,均可出现在数据中。因此,当客户端从服务器取回数据时,必须使用数据区块的长度来确定数据区块的 结束位置,而不要依据数据区块末尾的“\r\n”,即使它们固定存在于此。 Text lines are always terminated by \r\n. Unstructured data is _also_
terminated by \r\n, even though \r, \n or any other 8-bit characters
may also appear inside the data. Therefore, when a client retrieves
data from a server, it must use the length of the data block (which it
will be provided with) to determine where the data block ends, and not
the fact that \r\n follows the end of the data block, even though it
does.
键值
Keys
存储在memcached中的数据通过键值来标识。键值是一个文本字符串,对于需要存取这项数据的客户端而言,它必须是唯一的。键值当前的长度限制设定为250字符(当然,客户端通常不会用到这么长的键);键值中不能使用制表符和其他空白字符(例如空格,换行等)。 Data stored by memcached is identified with the help of a key. A key
is a text string which should uniquely identify the data for clients
that are interested in storing and retrieving it. Currently the
length limit of a key is set at 250 characters (of course, normally
clients wouldn’t need to use such long keys); the key must not include
control characters or whitespace.
命令
Commands
所有命令分为3种类型 There are three types of commands.
存储命令(有3项:’set’、’add’、’repalce’)指示服务器储存一些由键值标识的数据。客户端发送一行命令,后面跟着数据区块;然后,客户端等待接收服务器回传的命令行,指示成功与否。 Storage commands (there are three: “set”, “add” and “replace”) ask the
server to store some data identified by a key. The client sends a
command line, and then a data block; after that the client expects one
line of response, which will indicate success or faulure.
取回命令(只有一 项:’get’)指示服务器返回与所给键值相符合的数据(一个请求中右一个或多个键值)。客户端发送一行命令,包括所有请求的键值;服务器每找到一项内 容,都会发送回客户端一行关于这项内容的信息,紧跟着是对应的数据区块;直到服务器以一行“END”回应命令结束。 Retrieval commands (there is only one: “get”) ask the server to
retrieve data corresponding to a set of keys (one or more keys in one
request). The client sends a command line, which includes all the
requested keys; after that for each item the server finds it sends to
the client one response line with information about the item, and one
data block with the item’s data; this continues until the server
finished with the “END” response line.
/*?*/其他的命令都不能携带自由数据。在这些命令中,客户端发送一行命令,然后等待(由命令所决定)一行回应,或最终以一行“END”结束的多行命令。 All other commands don’t involve unstructured data. In all of them,
the client sends one command line, and expects (depending on the
command) either one line of response, or several lines of response
ending with “END” on the last line.
一行命令固定以命令名称开始,接着是以空格隔开的参数(如果有参数的话)。命令名称大小写敏感,并且必须小写。 A command line always starts with the name of the command, followed by
parameters (if any) delimited by whitespace. Command names are
lower-case and are case-sensitive.
一些客户端发送给服务器 的命令会包含一些时限(针对内容或客户端请求的操作)。这时,时限的具体内容既可以是Unix时间戳(从1970年1月1日开始的秒钟数),或当前时间开 始的秒钟数。对后者而言,不能超过 60*60*24*30(30天);如果超出,服务器将会理解为Unix时间戳,而不是从当前时间起的秒偏移。 Some commands involve a client sending some kind of expiration time
(relative to an item or to an operation requested by the client) to
the server. In all such cases, the actual value sent may either be
Unix time (number of seconds since January 1, 1970, as a 32-bit
value), or a number of seconds starting from current time. In the
latter case, this number of seconds may not exceed 60*60*24*30 (number
of seconds in 30 days); if the number sent by a client is larger than
that, the server will consider it to be real Unix time value rather
than an offset from current time.
错误字串
Error strings
每一个由客户端发送的命令,都可能收到来自服务器的错误字串回复。这些错误字串会以三种形式出现: Each command sent by a client may be answered with an error string
from the server. These error strings come in three types:
- “ERROR\r\n”
意味着客户端发送了不存在的命令名称。 means the client sent a nonexistent command name.
- “CLIENT_ERROR <error>\r\n”
意味着输入的命令行里存在一些客户端错误,例如输入未遵循协议。<error>部分是人类易于理解的错误解说…… means some sort of client error in the input line, i.e. the input
doesn’t conform to the protocol in some way. <error> is a
human-readable error string.
- “SERVER_ERROR <error>\r\n”
意味着一些服务器错误,导致命令无法执行。<error>部分是人类易于理解的错误解说。在一些严重的情形下(通常应该不会遇到),服务器将在发送这行错误后关闭连接。这是服务器主动关闭连接的唯一情况。 means some sort of server error prevents the server from carrying
out the command. <error> is a human-readable error string. In cases
of severe server errors, which make it impossible to continue
serving the client (this shouldn’t normally happen), the server will
close the connection after sending the error line. This is the only
case in which the server closes a connection to a client.
在后面每项命令的描述中,这些错误行不会再特别提到,但是客户端必须考虑到这些它们存在的可能性。 In the descriptions of individual commands below, these error lines
are not again specifically mentioned, but clients must allow for their
possibility.
存储命令
Storage commands
首先,客户端会发送一行像这样的命令: First, the client sends a command line which looks like this:
<command name> <key> <flags> <exptime> <bytes>\r\n
- <command name> 是 set, add, 或者 repalce - <command name> is “set”, “add” or “replace”
  • set 意思是 “储存此数据”
  • add 意思是 “储存此数据,只在服务器*未*保留此键值的数据时”
  • replace意思是 “储存此数据,只在服务器*曾*保留此键值的数据时”
  • “set” means “store this data”.
  • “add” means “store this data, but only if the server *doesn’t* already
    hold data for this key”.
  • “replace” means “store this data, but only if the server *does*
    already hold data for this key”.
- <key> 是接下来的客户端所要求储存的数据的键值 - <key> is the key under which the client asks to store the data
- <flags> 是在取回内容时,与数据和发送块一同保存服务器上的任意16位无符号整形(用十进制来书写)。客户端可以用它作为“位域”来存储一些特定的信息;它对服务器是不透明的。 - <flags> is an arbitrary 16-bit unsigned integer (written out in
decimal) that the server stores along with the data and sends back
when the item is retrieved. Clients may use this as a bit field to
store data-specific information; this field is opaque to the server.
- <exptime> 是终止时间。如果为0,该项永不过期(虽然它可能被删除,以便为其他缓存项目腾出位置)。如果非0(Unix时间戳或当前时刻的秒偏移),到达终止时间后,客户端无法再获得这项内容。 - <exptime> is expiration time. If it’s 0, the item never expires
(although it may be deleted from the cache to make place for other
items). If it’s non-zero (either Unix time or offset in seconds from
current time), it is guaranteed that clients will not be able to
retrieve this item after the expiration time arrives (measured by
server time).
- <bytes> 是随后的数据区块的字节长度,不包括用于分野的“\r\n”。它可以是0(这时后面跟随一个空的数据区块)。 - <bytes> is the number of bytes in the data block to follow, *not*
including the delimiting \r\n. <bytes> may be zero (in which case
it’s followed by an empty data block).
在这一行以后,客户端发送数据区块。 After this line, the client sends the data block:
<data block>\r\n
- <data block> 是大段的8位数据,其长度由前面的命令行中的<bytes>指定。 - <data block> is a chunk of arbitrary 8-bit data of length <bytes>
from the previous line.
发送命令行和数据区块以后,客户端等待回复,可能的回复如下: After sending the command line and the data blockm the client awaits
the reply, which may be:
- “STORED\r\n”
表明成功. to indicate success.
- “NOT_STORED\r\n”
表明数据没有被存储,但不是因为发生错误。这通常意味着add 或 replace命令的条件不成立,或者,项目已经位列删除队列(参考后文的“delete”命令)。 to indicate the data was not stored, but not
because of an error. This normally means that either that the
condition for an “add” or a “replace” command wasn’t met, or that the
item is in a delete queue (see the “delete” command below).
取回命令
Retrieval command
一行取回命令如下: The retrieval command looks like this:
get <key>*\r\n
- <key>* 表示一个或多个键值,由空格隔开的字串 - <key>* means one or more key strings separated by whitespace.
这行命令以后,客户端的等待0个或多个项目,每项都会收到一行文本,然后跟着数据区块。所有项目传送完毕后,服务器发送以下字串: After this command, the client expects zero or more items, each of
which is received as a text line followed by a data block. After all
the items have been transmitted, the server sends the string
“END\r\n”
来指示回应完毕。 to indicate the end of response.
服务器用以下形式发送每项内容: Each item sent by the server looks like this:
VALUE <key> <flags> <bytes>\r\n
<data block>\r\n
- <key> 是所发送的键名 - <key> is the key for the item being sent
- <flags> 是存储命令所设置的记号 - <flags> is the flags value set by the storage command
- <bytes> 是随后数据块的长度,*不包括* 它的界定符“\r\n” - <bytes> is the length of the data block to follow, *not* including
its delimiting \r\n
- <data block> 是发送的数据 - <data block> is the data for this item.
如果在取回请求中发送了一些键名,而服务器没有送回项目列表,这意味着服务器没这些键名(可能因为它们从未被存储,或者为给其他内容腾出空间而被删除,或者到期,或者被已客户端删除)。 If some of the keys appearing in a retrieval request are not sent back
by the server in the item list this means that the server does not
hold items with such keys (because they were never stored, or stored
but deleted to make space for more items, or expired, or explicitly
deleted by a client).
删除
Deletion
命令“delete”允许从外部删除内容: The command “delete” allows for explicit deletion of items:
delete <key> <time>\r\n
- <key> 是客户端希望服务器删除的内容的键名 - <key> is the key of the item the client wishes the server to delete
- <time> 是一个单位为秒的时间(或代表直到某一刻的Unix时间),在该时间内服务器会拒绝对于此键名的“add”和“replace”命令。此时内容被放入 delete队列,无法再通过“get”得到该内容,也无法是用“add”和“replace”命令(但是“set”命令可用)。直到指定时间,这些内容 被最终从服务器的内存中彻底清除。 - <time> is the amount of time in seconds (or Unix time until which)
the client wishes the server to refuse “add” and “replace” commands
with this key. For this amount of item, the item is put into a
delete queue, which means that it won’t possible to retrieve it by
the “get” command, but “add” and “replace” command with this key
will also fail (the “set” command will succeed, however). After the
time passes, the item is finally deleted from server memory.
<time>参数 是可选的,缺省为0(表示内容会立刻清除,并且随后的存储命令均可用)。 The parameter <time> is optional, and, if absent, defaults to 0
(which means that the item will be deleted immediately and further
storage commands with this key will succeed).
此命令有一行回应: The response line to this command can be one of:
- “DELETED\r\n”
表示执行成功 to indicate success
- “NOT_FOUND\r\n”
表示没有找到这项内容 to indicate that the item with this key was not found.
参考随后的“flush_all”命令使所有内容无效 See the “flush_all” command below for immediate invalidation
of all existing items.
增加/减少
Increment/Decrement
命令 “incr” 和 “decr”被用来修改数据,当一些内容需要 替换、增加 或减少时。这些数据必须是十进制的32位无符号整新。如果不是,则当作0来处理。修改的内容必须存在,当使用“incr”/“decr”命令修改不存在的 内容时,不会被当作0处理,而是操作失败。 Commands “incr” and “decr” are used to change data for some item
in-place, incrementing or decrementing it. The data for the item is
treated as decimal representation of a 32-bit unsigned integer. If the
current data value does not conform to such a representation, the
commands behave as if the value were 0. Also, the item must already
exist for incr/decr to work; these commands won’t pretend that a
non-existent key exists with value 0; instead, they will fail.
客户端发送命令行: The client sends the command line:
incr <key> <value>\r\n

decr <key> <value>\r\n
- <key> 是客户端希望修改的内容的建名 - <key> is the key of the item the client wishes to change
- <value> 是客户端要增加/减少的总数。 - <value> is the amount by which the client wants to increase/decrease
the item. It is a decimal representation of a 32-bit unsigned integer.
回复为以下集中情形: The response will be one of:
- “NOT_FOUND\r\n”
指示该项内容的值,不存在。 to indicate the item with this value was not found
- <value>\r\n ,<value>是 增加/减少 。 - <value>\r\n , where <value> is the new value of the item’s data,
after the increment/decrement operation was carried out.
注意”decr”命令发生下溢:如果客户端尝试减少的结果小于0时,结果会是0。”incr” 命令不会发生溢出。 Note that underflow in the “decr” command is caught: if a client tries
to decrease the value below 0, the new value will be 0. Overflow in
the “incr” command is not checked.
…… Note also that decrementing a number such that it loses length isn’t
guaranteed to decrement its returned length. The number MAY be
space-padded at the end, but this is purely an implementation
optimization, so you also shouldn’t rely on that.
状态
Statistics
命令”stats” 被用于查询服务器的运行状态和其他内部数据。有两种格式。不带参数的: The command “stats” is used to query the server about statistics it
maintains and other internal data. It has two forms. Without
arguments:
stats\r\n
这会在随后输出各项状态、设定值和文档。另一种格式带有一些参数: it causes the server to output general-purpose statistics and
settings, documented below. In the other form it has some arguments:
stats <args>\r\n
通过<args>,服务器传回各种内部数据。因为随时可能发生变动,本文不提供参数的种类及其传回数据。 Depending on <args>, various internal data is sent by the server. The
kinds of arguments and the data sent are not documented in this vesion
of the protocol, and are subject to change for the convenience of
memcache developers.
各种状态
General-purpose statistics
受到无参数的”stats”命令后,服务器发送多行内容,如下: Upon receiving the “stats” command without arguments, the server sents
a number of lines which look like this:
STAT <name> <value>\r\n
服务器用以下一行来终止这个清单: The server terminates this list with the line
END\r\n
在每行状态中,<name> 是状态的名字,<value> 使状态的数据。 以下清单,是所有的状态名称,数据类型,和数据代表的含义。 In each line of statistics, <name> is the name of this statistic, and
<value> is the data. The following is the list of all names sent in
response to the “stats” command, together with the type of the value
sent for this name, and the meaning of the value.
在“类型”一列中,”32u”表示32位无符号整型,”64u”表示64位无符号整型,”32u:32u”表示用冒号隔开的两个32位无符号整型。 In the type column below, “32u” means a 32-bit unsigned integer, “64u”
means a 64-bit unsigner integer. ‘32u:32u’ means two 32-but unsigned
integers separated by a colon.
名称/Name 类型/Type 含义/Meaning
pid 32u 服务器进程ID Process id of this server process
uptime 32u 服务器运行时间,单位秒 Number of seconds this server has been running
time 32u 服务器当前的UNIX时间 current UNIX time according to the server
version string 服务器的版本号 Version string of this server
rusage_user 32u:32u 该进程累计的用户时间
(秒:微妙)
Accumulated user time for this process
(seconds:microseconds)
rusage_system 32u:32u 该进程累计的系统时间
(秒:微妙)
Accumulated system time for this process
(seconds:microseconds)
curr_items 32u 服务器当前存储的内容数量 Current number of items stored by the server
total_items 32u 服务器启动以来存储过的内容总数 Total number of items stored by this server
ever since it started
bytes 64u 服务器当前存储内容所占用的字节数 Current number of bytes used by this server
to store items
curr_connections 32u 连接数量 Number of open connections
total_connections 32u 服务器运行以来接受的连接总数 Total number of connections opened since
the server started running
connection_structures 32u 服务器分配的连接结构的数量 Number of connection structures allocated
by the server
cmd_get 32u 取回请求总数 Cumulative number of retrieval requests
cmd_set 32u 存储请求总数 Cumulative number of storage requests
get_hits 32u 请求成功的总次数 Number of keys that have been requested and
found present
get_misses 32u 请求失败的总次数 Number of items that have been requested
and not found
bytes_read 64u 服务器从网络读取到的总字节数 Total number of bytes read by this server
from network
bytes_written 64u 服务器向网络发送的总字节数 Total number of bytes sent by this server to
network
limit_maxbytes 32u 服务器在存储时被允许使用的字节总数 Number of bytes this server is allowed to
use for storage.
其它命令
Other commands
“flush_all”命令有一个可选的数字参数。它总是执行成功,服务器会发送“OK\r\n”回应。它的效果是使已经存在的项目立即失效(缺 省),或在指定的时间后。此后执行取回命令,将不会有任何内容返回(除非重新存储同样的键名)。flush_all 实际上没有立即释放项目所占用的内存,而是在随后陆续有新的项目被储存时执行。flush_all 效果具体如下:它导致所有更新时间早于flush_all所设定时间的项目,在被执行取回命令时命令被忽略。 “flush_all” is a command with an optional numeric argument. It always succeeds, and the server sends “OK\r\n” in response. Its effect is to invalidate all existing items immediately (by default) or after the expiration specified. After invalidation none of the items will be returned in response to a retrieval command (unless it’s stored again under the same key *after* flush_all has invalidated the items). flush_all doesn’t actually free all the memory taken up by existing items; that will happen gradually as new items are stored. The most precise definition of what flush_all does is the following: it causes all items whose update time is earlier than the time at which flush_all was set to be executed to be ignored for retrieval purposes.
“version”命令没有参数: “version” is a command with no arguments:
version\r\n
在回应中,服务器发送: In response, the server sends
“VERSION <version>\r\n”
<version> 是服务器的版本字串。 where <version> is the version string for the server.
“quit”命令没有参数: “quit” is a command with no arguments:
quit\r\n
接收此命令后,服务器关闭连接。不过,客户端可以在不再需要时,简单地关闭连接就行,并不一定需要发送这个命令。 Upon receiving this command, the server closes the connection. However, the client may also simply close the connection when it no longer needs it, without issuing this command.
UDP 协议
UDP protocol
当来自客户端的连接数远大于TCP连接的上限时,可以使用基于UDP的接口。UDP接口不能保证传输到位,所以只有在不要求成功的操作中使用;比如被用于一个“get”请求时,会因不当的缓存处理而发生错误或回应有遗失。 For very large installations where the number of clients is high enough that the number of TCP connections causes scaling difficulties, there is also a UDP-based interface. The UDP interface does not provide guaranteed delivery, so should only be used for operations that aren’t required to succeed; typically it is used for “get” requests where a missing or incomplete response can simply be treated as a cache miss.
每个UDP数据包都包含一个简单的帧头,数据之后的内容与TCP协议的描述类似。在执行所产生的数据流中,请求必须被包含在单独的一个UDP数据包中,但是回应可能跨越多个数据包。(只有“get”和“set”请求例外,跨越了多个数据包) Each UDP datagram contains a simple frame header, followed by data in the same format as the TCP protocol described above. In the current implementation, requests must be contained in a single UDP datagram, but responses may span several datagrams. (The only common requests that would span multiple datagrams are huge multi-key “get” requests and “set” requests, both of which are more suitable to TCP transport for reliability reasons anyway.)
帧头有8字节长,如下(均由16位整数组成,网络字节顺序,高位在前): The frame header is 8 bytes long, as follows (all values are 16-bit integers in network byte order, high byte first):
  • 0-1 请求ID
  • 2-3 序号
  • 4-5 该信息的数据包总数
  • 6-7 保留位,必须为0
  • 0-1 Request ID
  • 2-3 Sequence number
  • 4-5 Total number of datagrams in this message
  • 6-7 Reserved for future use; must be 0
请求ID有客户端提供。一般它会是一个从随机基数开始的递增值,不过客户端想用什么样的请求ID都可以。服务器的回应会包含一个和请求中的同样的ID。客户端使用请求ID来区分每一个回应。任何一个没有请求ID的数据包,可能是之前的请求遭到延迟而造成的,应该被丢弃。 The request ID is supplied by the client. Typically it will be a monotonically increasing value starting from a random seed, but the client is free to use whatever request IDs it likes. The server’s response will contain the same ID as the incoming request. The client uses the request ID to differentiate between responses to outstanding requests if there are several pending from the same server; any datagrams with an unknown request ID are probably delayed responses to an earlier request and should be discarded.
序号的返回是从0到n-1,n是该条信息的数据包数量。 The sequence number ranges from 0 to n-1, where n is the total number of datagrams in the message. The client should concatenate the payloads of the datagrams for a given response in sequence number order; the resulting byte stream will contain a complete response in the same format as the TCP protocol (including terminating \r\n sequences).