开发者

php mysql db connect very slow

I have a small PHP framework that basically just loads a controller and a view. An empty page loads in about 0.004 seconds, using microtime() at the beginning and end of execution.

Here's the "problem". If I do the following:

$link = @mysql_connect($server,$user,$pass,$link); @mysql_select_db($database, $link);

the page loads in about 0.500 seconds. A whopping 12500% jump in time to render the empty page.

is this normal or am I doing something seriously wrong here... (I'm hoping for the latte开发者_StackOverflow中文版r).

EDIT: Could someone say what a normal time penalty is for just connecting to a mysql db like above.


Error suppression with @ will slow down your script. Also an SQL connection is reliant on the speed of the server. So if the server is slow to respond, you will get a slow execution of your script.


Actually, I don't get what you're trying to do with the $link variable . $link = @mysql_connect($server,$user,$pass,$link); is probably wrong, possibly not doing what you want (ie nothing in your example) unless if you have more than one link to databases (advanced stuff)

the php.net documention states

new_link

If a second call is made to mysql_connect() with the same arguments, no new link will be established, but instead, the link identifier of the already opened link will be returned. The new_link parameter modifies this behavior and makes mysql_connect() always open a new link, even if mysql_connect() was called before with the same parameters. In SQL safe mode, this parameter is ignored.

On my webserver, the load time is always about the same on average (4000 µsecs the first time and 600 µsec the second time)

Half a second to connect to a mysql database is a bit slow but not unusual either. If it's on another server on the network with an existing load, it might be absolutely normal.

I wouldn't worry too much about this problem.

(oh, old question ! Nevermind, still replying)


Have you tried verifying your data integrity? do a repair table, and an optimze table to all tables. I know that sometimes when a table has gone corrupt the connection time can take an enormous amount of time / fail all together.


May be the reason is domain resolve slow.

skip-name-resolve

Add this to my.cnf, then restart mysqld.

And if you skip-name-resolve, you can't use hostname in mysql for user permission.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜