mysql_connect กับ mysql_pconnect ต่างกันยังไง

mysql_connect กับ mysql_pconnect ต่างกันยังไง

เปรียบเทียบข้อดี

เปรียบเทียบข้อเสีย

ฟันธงว่าควรใช้อะไรดี

อัน นี้ขึ้นอยู่กับงาน หากเป็นงานที่ต้องการ Connection เยอะมากๆแนะนำ mysql_pconnect (จริงๆ ถ้าไม่ได้ใช้ Share Host หรือมี Database แค่ตัวเดียว หรือจำนวนน้อยใน Server นั้นใช้ไปเลยก็ไม่เสียหายครับ) แต่ถ้าเป็น Shared Host หรือ มี Database จำนวนมากก็ใช้ mysql_connect ปกติไปก็ได้ครับ

ผลทดสอบความเร็ว

ของ mysql_connect

Code:

<?php
mysql_connect("127.0.0.1","root","");
mysql_select_db("test");

mysql_close();
?>

ผล:

mix5003@Shana:~$ ab -k -c 200 -t 10 http://127.0.0.1/benc/connect/connect.php
This is ApacheBench, Version 2.3 <$Revision: 655654 $>
Copyright 1996 Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/
Licensed to The Apache Software Foundation, http://www.apache.org/

Benchmarking 127.0.0.1 (be patient)
Completed 5000 requests
Completed 10000 requests
Completed 15000 requests
Completed 20000 requests
Completed 25000 requests
Finished 27160 requests


Server Software:        Apache/2.4.2
Server Hostname:        127.0.0.1
Server Port:            80

Document Path:          /benc/connect/connect.php
Document Length:        0 bytes

Concurrency Level:      200
Time taken for tests:   10.000 seconds
Complete requests:      27160
Failed requests:        0
Write errors:           0
Keep-Alive requests:    26939
Total transferred:      6345842 bytes
HTML transferred:       0 bytes
Requests per second:    2715.98 [#/sec] (mean)
Time per request:       73.638 [ms] (mean)
Time per request:       0.368 [ms] (mean, across all concurrent requests)
Transfer rate:          619.71 [Kbytes/sec] received

Connection Times (ms)
              min  mean[+/-sd] median   max
Connect:        0    0  10.5      0    1000
Processing:     1   69 539.0      9    9877
Waiting:        1   69 539.0      9    9876
Total:          1   69 540.1      9    9880

Percentage of the requests served within a certain time (ms)
  50%      9
  66%     19
  75%     27
  80%     32
  90%     44
  95%     57
  98%     98
  99%   2445
 100%   9880 (longest request)

จะเห็นว่ากราฟพุ่งสูงสุดที่ 8000 Connection (จริงๆน่าจะเป็น 4000 นะครับ แต่เวลานั้น เครื่องผมกาก refresh กราฟไม่ได้ 1 ทีครับ) และต่อได้ 2715 ครั้งต่อ 1 วินาทีเลยทีเดียว (Concurrent หรือจำนวนการเชื่อมต่อพร้อมกันคือ 200 นะครับ)

mysql_pconnect

Code:

<?php
mysql_pconnect("127.0.0.1","root","");
mysql_select_db("test");

mysql_close();
?>

ผล:

mix5003@Shana:~$ ab -k -c 200 -t 10 http://127.0.0.1/benc/connect/pconnect.php
This is ApacheBench, Version 2.3 <$Revision: 655654 $>
Copyright 1996 Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/
Licensed to The Apache Software Foundation, http://www.apache.org/

Benchmarking 127.0.0.1 (be patient)
Completed 5000 requests
Completed 10000 requests
Completed 15000 requests
Completed 20000 requests
Completed 25000 requests
Completed 30000 requests
Completed 35000 requests
Completed 40000 requests
Completed 45000 requests
Finished 46890 requests


Server Software:        Apache/2.4.2
Server Hostname:        127.0.0.1
Server Port:            80

Document Path:          /benc/connect/pconnect.php
Document Length:        0 bytes

Concurrency Level:      200
Time taken for tests:   10.000 seconds
Complete requests:      46890
Failed requests:        0
Write errors:           0
Keep-Alive requests:    46479
Total transferred:      10954242 bytes
HTML transferred:       0 bytes
Requests per second:    4688.95 [#/sec] (mean)
Time per request:       42.653 [ms] (mean)
Time per request:       0.213 [ms] (mean, across all concurrent requests)
Transfer rate:          1069.74 [Kbytes/sec] received

Connection Times (ms)
              min  mean[+/-sd] median   max
Connect:        0    0   9.2      0     999
Processing:     1   41 362.9      5    8905
Waiting:        1   41 362.9      5    8905
Total:          1   41 363.8      5    8909

Percentage of the requests served within a certain time (ms)
  50%      5
  66%     10
  75%     14
  80%     17
  90%     26
  95%     35
  98%     79
  99%   1271
 100%   8909 (longest request)

จะเห็นว่ากราฟพุ่งสูงสุดที่ 130 Connection  และต่อได้ 4688 ครั้งต่อ 1 วินาทีเลยทีเดียว (Concurrent หรือจำนวนการเชื่อมต่อพร้อมกันคือ 200 นะครับ) จะเห็นได้ว่าเร็วกว่า mysql_connect เกือบ 2 เท่าเลยทีเดียว(ขี้เกียจคำนวน ประมาณเอาแล้วกัน 555)

 

Exit mobile version