惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

博客园 - 司徒正美
M
MIT News - Artificial intelligence
博客园_首页
IT之家
IT之家
L
LangChain Blog
D
DataBreaches.Net
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
Google DeepMind News
Google DeepMind News
Blog — PlanetScale
Blog — PlanetScale
人人都是产品经理
人人都是产品经理
博客园 - Franky
云风的 BLOG
云风的 BLOG
罗磊的独立博客
量子位
G
Google Developers Blog
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
博客园 - 【当耐特】
博客园 - 叶小钗
S
SegmentFault 最新的问题
Stack Overflow Blog
Stack Overflow Blog
B
Blog
T
Tailwind CSS Blog
A
About on SuperTechFans
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com

博客园 - xhan

提交给mysql java驱动的优化下个版本要发布了^_^ java 可伸缩阻塞队列实现 java mysql大数据量批量插入与流式读取分析 innodb next-key lock引发的死锁 jremoting的功能扩展点 java开源项目jremoting largest remainder method java impl 数据库单元测试 元数据编程实战_使用Emit运行时生成Protobuf编码类 redis入门系列文章广告贴 九 redis学习笔记之虚拟内存 八 redis学习笔记之主从复制 七 redis学习笔记之持久化 六 redis学习笔记之发布订阅 五 redis学习笔记之pipeline 四 redis学习笔记之事务 三 redis学习笔记之排序 二 redis学习笔记之数据类型 - xhan - 博客园 一 redis学习笔记之环境搭建
发布个c#版的HandlerSocket客户端类库
xhan · 2011-03-24 · via 博客园 - xhan

HandlerSocket 是一个mysql 数据库的插件,它可以绕过mysql的查询分析和优化的过程,直接与innodb存储引擎进行交互。尤其是当大多数数据都被innodb缓存到内存中的时候,

查询分析和优化过程就会是整个查询处理过程的瓶颈。通过使用HandlerSocket可以绕过这个瓶颈,从而提升性能。这要比mysql+memched要有优势,因为HandlerSocket不需要处理缓存失效的问题。下面看下HsClient访问mysql的一个例子代码

 1 using System;
 2 using System.Collections.Generic;
 3 using System.Linq;
 4 using System.Text;
 5 using HsClient;
 6 
 7 namespace HsClientTest
 8 {
 9     class Program
10     {
11         private static HsIndexSessionFactory client = new HsIndexSessionFactory("192.168.200.9"9999);
12         static void Main(string[] args)
13         {
14             HsIndexSession session = null;
15             try
16             {
17                 session = client.OpenIndex(0"test""FUser""PRIMARY",
18                     new string[] { "UserId""Nickname""Email" });
19 
20                 HsResultSet rs = session.Find(HsOperator.GreatEqual, new string[] { "0" }, 200);
21 
22                 while (rs.Next())
23                 {
24                     Console.WriteLine("id:{0},nickname:{1},email:{2}", rs.GetInt(0), rs.GetString(1), rs.GetString(2));
25                 }
26 
27                 int count = session.Update(HsOperator.Equal, new string[] { "6" }, 10new string[] { "6""c#""x@x.net" });
28                 Console.WriteLine("update count {0}", count);
29 
30 
31                 count = session.Delete(HsOperator.Equal, new string[] { "6" }, 10);
32                 Console.WriteLine("delete count {0}", count);
33 
34 
35                 bool insertResult = session.Insert(new string[] { "6""haha""you@aa.ss" });
36                 Console.WriteLine("insert success {0}", insertResult);
37             }
38             finally
39             {
40                 if (session != null)
41                 {
42                     session.Close();
43                 }
44             }
45 
46 
47             //close all socket in the end
48             client.Shutdown();
49             Console.ReadKey();
50         }
51     }
52 }

 要访问mysql必须首先打开一个IndexSession.指明我们要访问的索引名称和要访问的字段。很容易理解,因为没有了mysql原来的查询分析和优化。所以访问表数据使用那个索引

就必须我们自己来选择。 注意通过IndexSession发起的CRUD操作的字段数量很顺序必须和打开IndexSession时提供的字段是一样的。使用完IndexSession要记得关闭,关闭会吧IndexSession使用的Socket连接,交还给连接池。最后Shutdown操作会关闭连接池中所有连接.

下载/Files/xhan/HsClient.rar 源代码也就500行左右,学习网络编程的同学可以看看。

代码还没经过生产环境测试,慎重使用,仅供学习交流