php pconnect vs connect
Asked Answered
C

5

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.

Clotilde answered 14/3, 2010 at 4:54 Comment(1)
Possible duplicate of mysql_connect VS mysql_pconnectHypsometer
G
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.

Gasper answered 14/3, 2010 at 4:57 Comment(2)
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?Clotilde
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.Mirella
D
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).

Drucilla answered 14/3, 2010 at 4:58 Comment(2)
so if need less resource, better to use connect? since i worry about open many connectionClotilde
"slightly faster" what does this mean? would you notice the difference if you start 1000 requests?Hypsometer
H
2

You can also check this page for more info

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

Napoleon

Horlacher answered 18/4, 2010 at 5:54 Comment(0)
C
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()

Circadian answered 12/5, 2016 at 2:27 Comment(0)
D
-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.

Dhyana answered 4/7, 2017 at 13:3 Comment(1)
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.Assyriology

© 2022 - 2024 — McMap. All rights reserved.