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" }, 20, 0);
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" }, 1, 0, new string[] { "6", "c#", "x@x.net" });
28 Console.WriteLine("update count {0}", count);
29
30
31 count = session.Delete(HsOperator.Equal, new string[] { "6" }, 1, 0);
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行左右,學習網路編程的同學可以看看。
https://github.com/ahiguti/HandlerSocket-Plugin-for-MySQL/blob/master/docs-en/protocol.en.txt
代碼還沒經過生產環境測試,謹慎使用,僅供學習交流