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

轉載自:http://www.9958.pw/post/session_method數據庫

相關文章
相關標籤/搜索