6

If I have a script which inserts data then exits, the script will be opened by 100 users at the same time or within 2 mins.

(Actually I'm doing email tracking.)

So pconnect is better, or connect is better to reduce the resource?

I have close when after insert.

Prof. Falken
  • 24,226
  • 19
  • 100
  • 173
user192344
  • 1,274
  • 6
  • 22
  • 36
  • Possible duplicate of [mysql\_connect VS mysql\_pconnect](http://stackoverflow.com/questions/247807/mysql-connect-vs-mysql-pconnect) – rubo77 Dec 02 '15 at 11:55

5 Answers5

5

mysql_pconnect() drops the open connection into a pool that can be used by any other request to the same process. As such, each worker keeps the connection open until it dies. This can be acceptable if you keep the number of workers low, but as soon as you raise the number of workers then you're better off switching to mysql_connect(). It will take slightly longer per request since the connection has to be made each time, but you will only create as many connections as there are requests, not workers.

Ignacio Vazquez-Abrams
  • 776,304
  • 153
  • 1,341
  • 1,358
  • 1
    yea, my script is only for request, therefore if i have over 1000 ppl open the script and run it in between 5 mins, it is better use mysql_connect? – user192344 Mar 14 '10 at 05:12
  • 2
    If there's a 1000 people over 5 minutes, and your script takes 100ms on average, that's 0.3 concurrent user on average, which is something you can probably handle with the the computer that's embedded in your microwave. – Evert Jul 04 '17 at 13:07
2

connect uses fewer resources (idle instances of the web server don't need to keep a database connection open), but pconnect is slightly faster (don't have to open a new connection, it's already there).

user262976
  • 1,994
  • 12
  • 7
2

You can also check this page for more info

http://php.net/manual/en/function.mysql-pconnect.php

Napoleon

indianwebdevil
  • 4,879
  • 7
  • 37
  • 51
0

If you use pconnect , you will have a lot of connections on SLEEP mode with this kind of script that runs 100 times in 2 minutes and your mysql will die.

You can use mysql_connect() , mysql_close()

Dinidu Hewage
  • 2,169
  • 6
  • 40
  • 51
alex-
  • 1
-1

mysql_pconnect() : is permanent connection with database. you cant lose your connection while such kind of operation.

mysql_connect() : is for connect database with normal way using some time due to large number of operation you might lose your connection.

I suggest mysql_pconnect() for database connection.

JayminLimbachiya
  • 971
  • 1
  • 13
  • 19
  • There is no such a thing as permanent when it comes to a database connection. "p" in pconnect stands for persistent; i.e the connection itself is not dropped when the request handling finishes (or when worker dies). AFAIK, this is relevant only in a webserver context. – ϹοδεMεδιϲ Aug 15 '17 at 18:53