MySQL內存表、MyISAM表、MemCache實現Session效率比較
早先問過BleakWind,他認為,給別人做的話MySQL內存表做會話比較 好一些,因為MySQL內存表做Session更容易維護(可以制作安裝腳本)。這個周末,我進行了一些測試,測試MySQL MyISAM表做會話(對時間給不給索引)、內存表做會話、MemCache做會話的效率比較。
定義會話Session類:

定義會話處理器接口(Session_Handler_Interface):

實現MySQL內存表Session處理器:

實現MemCache會話處理器:

MySQL內存表測試程序:

MemCache會話測試代碼:

MySQL會話表如下:
CREATE TABLE `session` (
`id` char(32) COLLATE utf8_unicode_ci NOT NULL,
`name` varchar(20) COLLATE utf8_unicode_ci NOT NULL,
`ip` varchar(15) COLLATE utf8_unicode_ci NOT NULL,
`time` int(10) unsigned NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=MEMORY DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;
測試結果:
MemCache:
======================================================================================
Concurrency Level: 30
Time taken for tests: 18.234375 seconds
Complete requests: 10000
Failed requests: 0
Write errors: 0
Total transferred: 2774781 bytes
HTML transferred: 280000 bytes
Requests per second: 548.41 [#/sec] (mean)
Time per request: 54.703 [ms] (mean)
Time per request: 1.823 [ms] (mean, across all concurrent requests)
Transfer rate: 148.57 [Kbytes/sec] received
Connection Times (ms)
min mean[+/-sd] median max
Connect: 0 0 1.9 0 31
Processing: 0 53 12.1 46 328
Waiting: 0 52 11.9 46 328
Total: 0 53 12.1 46 328
Percentage of the requests served within a certain time (ms)
50% 46
66% 62
75% 62
80% 62
90% 62
95% 62
98% 62
99% 78
100% 328 (longest request)
MySQL內存表:
=================================================================================
Concurrency Level: 30
Time taken for tests: 20.375000 seconds
Complete requests: 10000
Failed requests: 4694
(Connect: 0, Length: 4694, Exceptions: 0)
Write errors: 0
Total transferred: 3118440 bytes
HTML transferred: 623048 bytes
Requests per second: 490.80 [#/sec] (mean)
Time per request: 61.125 [ms] (mean)
Time per request: 2.038 [ms] (mean, across all concurrent requests)
Transfer rate: 149.45 [Kbytes/sec] received
Connection Times (ms)
min mean[+/-sd] median max
Connect: 0 0 1.9 0 31
Processing: 0 60 7.6 62 125
Waiting: 0 59 7.6 62 125
Total: 0 60 7.5 62 125
Percentage of the requests served within a certain time (ms)
50% 62
66% 62
75% 62
80% 62
90% 62
95% 62
98% 78
99% 78
100% 125 (longest request)
其他測試:
將MySQL實現的數據庫引擎改為 MyISAM(依然為 并發 30 測試 10000 次) 結果為:
Requests per second: 440.17 [#/sec] (mean)
Percentage of the requests served within a certain time (ms)
50% 62
66% 62
75% 78
80% 78
90% 78
95% 78
98% 78
99% 78
100% 640 (longest request)
為MyISAM表的 time 列增加索引(因為 會話表 讀寫次數幾乎相等,因此應該效果不明顯),結果為:
Requests per second: 441.08 [#/sec] (mean)
Percentage of the requests served within a certain time (ms)
50% 62
66% 62
75% 78
80% 78
90% 78
95% 78
98% 109
99% 109
100% 156 (longest request)
=================================================================================
結論:
MemCache做會話效率最高,也最靈活,但目前嘗不支持查看誰在線等功能,附加的,只能自己增加一個數組記錄在線用戶、以及最后活躍時間并實現gc等。
MySQL內存表做會話效率也相當的高,另外一個有點是,MySQL內存表似乎更穩定,longest request (125ms)明顯的短于 MemCache的(328ms)。不過缺點是,存儲的字段數以及字段長度受限。
MySQL MyISAM表做會話在這里居然也達到了440的rps,真不簡單。不過您要是等半個小時在測試一次,您就會明白MyISAM表的缺點了,頁面幾乎崩潰。MyISAM表的缺點是,一旦其中有較多的碎片,這個數據庫幾乎都不可用了,您注釋掉 gc 的代碼在這里貌似可以獲得更好的效率表現(^_^、當然也可以定時的Optimizer,概率觸發或者Cron定時啟動也不錯)
MyISAM表對time列增加索引對每秒完成的請求數沒什么影響,不過有一點需要注意的是,增加索引后,每次完成 request的時間更均勻了,longest request從640ms跌到了156ms。為time列增加索引有助于讓完成請求的時間變均勻。
測試平臺:
Acer Aspire 4520G:
CPU:AMD Athlon 64 * 2 TK-55
RAM: IG
OS: Windows XP sp2
Web Server: Apache 2.26
PHP: PHP5.25