469,934 Members | 2,173 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,934 developers. It's quick & easy.

PHP And SQL UPDATE problem

I have problem with SQL update. Sometimes it can take 2-10 seconds. I need
to make this update every time page is opened - ASYNCHRONOUSLY.

I have read forums, php.net etc about running php code 'in background' after
all data generated is received by user browser. However i cant simply use
it. Nothing works for me, im on a paid server.

I have checked the headers and all content my PHP outputs is zipped and send
compressed by apache (content-length header filled automatically). Is there
any way of running SQL query asynchronously to PHP script (so send query to
SQL, query is placed in some buffer and function returns immediatelly?)

TIA for help.
Feb 17 '06 #1
6 1849
'~=_Slawek_=~' wrote:
I have problem with SQL update. Sometimes it can take 2-10 seconds. I need
to make this update every time page is opened - ASYNCHRONOUSLY.

I have read forums, php.net etc about running php code 'in background' after
all data generated is received by user browser. However i cant simply use
it. Nothing works for me, im on a paid server.

I have checked the headers and all content my PHP outputs is zipped and send
compressed by apache (content-length header filled automatically). Is there
any way of running SQL query asynchronously to PHP script (so send query to
SQL, query is placed in some buffer and function returns immediatelly?)

TIA for help.


There are many places that an SQL update can get bogged down... to name a few:
000) poorly designed logical and/or physical database
001) poorly written update statements
make sure the column you are updating is NOT the column in an index.
010) Number of rows in a non-indexed lookup to do the update
011) Number of users concurrently updating the same site
(locking issues)
100) System/Disk/Network latencies
since it is a "paid" service you have no control over this.

And since you have not provided us with any code segments to look at everything
is just speculation.

--
Michael Austin.
DBA Consultant
Donations welcomed. Http://www.firstdbasource.com/donations.html
:)
Feb 17 '06 #2
Hello Michael.

Thanks for thinking over my problem. Your 4. assumption is right i cant tell
all other are wrong but I think they are. For my code:

mysql_query("UPDATE liczniki SET value=value+1 WHERE
owner_id='".$this->get_user_id()."' AND
owner_licznik_number='$owner_licznik_number'");

It takes 80ms to a couple of seconds, on shared hosting with DB overloaded
by other users (I assume some kind of write behind caching is responsible
for that). Table size is for now 100 records.

For table structure:
FIELD::TYPE::NULL::KEY::DEFAULT
id::int(10) unsigned::::PRI::NULL::auto_increment
owner_id::int(10) unsigned::YES::MUL::NULL::
owner_user::varchar(255)::YES::MUL::NULL::
owner_licznik_number::int(11) unsigned::::MUL::0::
value::int(10) unsigned::::::0::
type::enum('inny','online','wyswietlen','odwiedzin ')::::::inny::
sub_type::enum('tekstowy','graficzny')::::::teksto wy::
style::blob::YES::::NULL::
settings::blob::YES::::NULL::
last_updated::timestamp::YES::::0000-00-00 00

TIA.

Feb 17 '06 #3
I think even if your server is overloaded, is this happening every
time, every day, every minute? is your site getting a massive number
of hits?

I have seen before when you leave connections open, result sets not
freed, etc. this memory is not necessarily reallocated or destroyed and
thus everytime the system needs to allocate more memory for your PHP
script it may need to swap some to the disk, and this would cause your
latency. Remember to close all your connects and free all of your
result sets.

Alex
http://prepared-statement.blogspot.com

Feb 17 '06 #4
'~=_Slawek_=~' wrote:
Hello Michael.

Thanks for thinking over my problem. Your 4. assumption is right i cant tell
all other are wrong but I think they are. For my code:

mysql_query("UPDATE liczniki SET value=value+1 WHERE
owner_id='".$this->get_user_id()."' AND
owner_licznik_number='$owner_licznik_number'");

It takes 80ms to a couple of seconds, on shared hosting with DB overloaded
by other users (I assume some kind of write behind caching is responsible
for that). Table size is for now 100 records.

For table structure:
FIELD::TYPE::NULL::KEY::DEFAULT
id::int(10) unsigned::::PRI::NULL::auto_increment
owner_id::int(10) unsigned::YES::MUL::NULL::
owner_user::varchar(255)::YES::MUL::NULL::
owner_licznik_number::int(11) unsigned::::MUL::0::
value::int(10) unsigned::::::0::
type::enum('inny','online','wyswietlen','odwiedzin ')::::::inny::
sub_type::enum('tekstowy','graficzny')::::::teksto wy::
style::blob::YES::::NULL::
settings::blob::YES::::NULL::
last_updated::timestamp::YES::::0000-00-00 00

TIA.


Sounds like it may be time to find another host which doesn't overload
their servers like this. A simple update for 100 records shouldn't take
anywhere near 2 seconds.

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
js*******@attglobal.net
==================
Feb 18 '06 #5
Maybe you have some suggestions what host should I try? My hosting is very
good (cpanel, unlimited ftp, mail, domains, awstat, server logs, custom
error pages, directory protection, installed nearly all PHP extensions, 6
postgre + mysql database without limits, 10 GIG month transfer, 2GIG on disk
for only $35 yearly).

@Alex:
Yes i wrote its overloaded. Sometimes there is 10MBytes of data/sec taken
from DB (probably some site written badly).

I just taken some stats, the question is: should i change hosting. I mean I
only updated 25 X 4-byte fields that are not primary indexes and delay is
IMO huge (but im new to DB optimisation so I can be wrong). How slow itll
get when i finally have 2000 records table (its my destination, not much
IMO).

I guess that after 4'th update my table finally all go to cache the >WHOLE<
25 rows >LOL<.

/*[23:22:21][ 344 ms]*/ UPDATE liczniki SET value=1
/*(25 row(s) affected)*/
/*[23:22:21][ 406 ms]*/ UPDATE liczniki SET value=1
/*[23:22:22][ 609 ms]*/ UPDATE liczniki SET value=1
/*[23:22:22][ 219 ms]*/ UPDATE liczniki SET value=1
/*[23:22:22][ 63 ms]*/ UPDATE liczniki SET value=1
/*[23:22:22][ 62 ms]*/ UPDATE liczniki SET value=1
/*[23:22:22][ 63 ms]*/ UPDATE liczniki SET value=1
/*[23:22:22][ 47 ms]*/ UPDATE liczniki SET value=1
/*[23:22:22][ 47 ms]*/ UPDATE liczniki SET value=1
/*[23:22:22][ 63 ms]*/ UPDATE liczniki SET value=1

Feb 18 '06 #6
'~=_Slawek_=~' wrote:
Maybe you have some suggestions what host should I try? My hosting is very
good (cpanel, unlimited ftp, mail, domains, awstat, server logs, custom
error pages, directory protection, installed nearly all PHP extensions, 6
postgre + mysql database without limits, 10 GIG month transfer, 2GIG on disk
for only $35 yearly).

@Alex:
Yes i wrote its overloaded. Sometimes there is 10MBytes of data/sec taken
from DB (probably some site written badly).

I just taken some stats, the question is: should i change hosting. I mean I
only updated 25 X 4-byte fields that are not primary indexes and delay is
IMO huge (but im new to DB optimisation so I can be wrong). How slow itll
get when i finally have 2000 records table (its my destination, not much
IMO).

I guess that after 4'th update my table finally all go to cache the >WHOLE<
25 rows >LOL<.

/*[23:22:21][ 344 ms]*/ UPDATE liczniki SET value=1
/*(25 row(s) affected)*/
/*[23:22:21][ 406 ms]*/ UPDATE liczniki SET value=1
/*[23:22:22][ 609 ms]*/ UPDATE liczniki SET value=1
/*[23:22:22][ 219 ms]*/ UPDATE liczniki SET value=1
/*[23:22:22][ 63 ms]*/ UPDATE liczniki SET value=1
/*[23:22:22][ 62 ms]*/ UPDATE liczniki SET value=1
/*[23:22:22][ 63 ms]*/ UPDATE liczniki SET value=1
/*[23:22:22][ 47 ms]*/ UPDATE liczniki SET value=1
/*[23:22:22][ 47 ms]*/ UPDATE liczniki SET value=1
/*[23:22:22][ 63 ms]*/ UPDATE liczniki SET value=1


Well, you also get what you pay for. Hosting companies have to pay for
the servers, connections to the world, electricity, personnel, etc.

If they're going to charge a cheap price, they have to put a lot of
people on a server. And if your server is being overloaded by another
user (or multiple users), there's not much you can do about it.

You can request a different server from your host. If they move you,
you might or might not get better response. Or you can find another
hosting company. Again, if you go with a $35/yr hosting company you may
or may not get better response.

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
js*******@attglobal.net
==================
Feb 18 '06 #7

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

6 posts views Thread by al | last post: by
13 posts views Thread by abdoly | last post: by
8 posts views Thread by Zorpiedoman | last post: by
5 posts views Thread by =?Utf-8?B?UlBhcmtlcg==?= | last post: by
11 posts views Thread by SAL | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.