而且数据都是保存在原来的INNODB表中的。handlersocket
在HandlerSocket操作innodb数据表的时候,显然也需要open/close table。
但它只打开一次,然后会reuse。
由于open/close table非常耗时,并且会带来严重的mutex竞争,所以这种改进,极大地提升了性能。
Of course HandlerSocket closes tables when traffics become small etc so that it won’t block administrative commands (DDL) forever.
上图是memcached的原理图。
memcached主要用来缓存数据集(database records)。
因为memcached的get操作比mysql的PK查询要快得多。
如果HandlerSocket获取数据的速度比memcached还要快,那么我们就没必要再使用memcached来缓存数据集了。做做其它缓存还是可以的。比如生成出来的HTML代码,还有一些统计数据等等。
花荣注:貌似我从来没有直接把mysql的数据集扔到memcached中。一直都是把中间结果保存到里面。难道走偏了?
使用 HandlerSocket
看下面这张user表:
CREATE TABLE user (user_id INT UNSIGNED PRIMARY KEY,user_name VARCHAR(50),user_email VARCHAR(255),created DATETIME) ENGINE=InnoDB;
在mysql中取出数据的方法如下:
mysql> SELECT user_name, user_email, created FROM user WHERE user_id=101;+---------------+-----------------------+---------------------+| user_name| user_email| created|+---------------+-----------------------+---------------------+| Yukari Takeba | yukari.takeba@dena.jp | 2010-02-03 11:22:33 |+---------------+-----------------------+---------------------+
在HandlerSocket中怎样操作呢?
安装HandlerSocket
详细文档看这里:
大概安装步骤如下:
1 下载
2 编译 HandlerSocket客户端和服务器端程序:
./configure --with-mysql-source=... --with-mysql-bindir=... ; make; make install
3 安装插件
mysql> INSTALL PLUGIN 'HandlerSocket' SONAME 'HandlerSocket.so';
安装完毕。不需要修改mysql的源代码。
mysql需要5.1或者以后版本。
编写HandlerSocket 客户端代码
目前HandlerSocket客户端只有C++ 和 Perl的库。还没有php和C的。
#!/usr/bin/perluse strict;use warnings;use Net::HandlerSocket;#1. establishing a connectionmy $args = { host => 'ip_to_remote_host', port => 9998 };my $hs = new Net::HandlerSocket($args);#2. initializing an index so that we can use in main logics. # MySQL tables will be opened here (if not opened)my $res = $hs->open_index(0, 'test', 'user', 'PRIMARY','user_name,user_email,created');die $hs->get_error() if $res != 0;#3. main logic #fetching rows by id #execute_single (index id, cond, cond value, max rows, offset)$res = $hs->execute_single(0, '=', [ '101' ], 1, 0);die $hs->get_error() if $res->[0] != 0;shift(@$res);for (my $row = 0; $row
本文来自电脑杂谈,转载请注明本文网址:
http://www.pc-fly.com/a/jisuanjixue/article-27116-4.html
虽然现在美国也是这么做的