Do I have to use $sth->finish within scripts to release resources after executing a query?
Nov 9, 2000, 11:31 AM
Novice (11 posts)
Nov 9, 2000, 11:31 AM
Post #3 of 12
Views: 9678
The reason I asked this question is, I have a forum script which uses MySQL, basically it works fine. But sometimes it becomes very unstable, from top list, I can see lots of alive forum_show.pl processes running in memory, like:
........................................ 14.01 2.1 0 forum_show.pl
........................................ 5.22 1.2 0 forum_show.pl
........................................ 5.10 2.3 0 forum_show.pl
........................................ 4.88 1.1 0 forum_show.pl
........................................ 4.01 2.8 0 forum_show.pl
........................................ 2.01 3.3 0 forum_show.pl
........................................ 1.18 0.8 0 forum_show.pl
........................................ 0.37 2.2 0 forum_show.plAll the forum_show.pl process are alive and running. This seems to happen regardless of how many users are accessing the board. The system slows down so much that.
Maybe my server provider limits the max. connection to database, so I am thinking of adding $sth->finish within scripts to release resource for other queries(?). But I am not sure if this can solve the problem, I am not MySQL expert. And If so, do I have to add $sth->finish after every query? Or only one at the end of every script (a script does several queries)?
Code:
PID USER PRI NI SIZE RSS SHARE STAT LIB %CPU %MEM TIME COMMAND ........................................ 14.01 2.1 0 forum_show.pl
........................................ 5.22 1.2 0 forum_show.pl
........................................ 5.10 2.3 0 forum_show.pl
........................................ 4.88 1.1 0 forum_show.pl
........................................ 4.01 2.8 0 forum_show.pl
........................................ 2.01 3.3 0 forum_show.pl
........................................ 1.18 0.8 0 forum_show.pl
........................................ 0.37 2.2 0 forum_show.pl
Maybe my server provider limits the max. connection to database, so I am thinking of adding $sth->finish within scripts to release resource for other queries(?). But I am not sure if this can solve the problem, I am not MySQL expert. And If so, do I have to add $sth->finish after every query? Or only one at the end of every script (a script does several queries)?
Nov 10, 2000, 4:39 PM
Administrator (9387 posts)
Nov 10, 2000, 4:39 PM
Post #4 of 12
Views: 9653
You don't need to call ->finish on mysql if the variable goes out of scope and is destroyed. ie:
sub foo {
if (..) {
my $sth = ...
}
# sth is destroyed
}
if you are using globals, and your sth's don't get destroyed by going out of scope, you should call sth->finish.
Cheers,
Alex
--
Gossamer Threads Inc.
sub foo {
if (..) {
my $sth = ...
}
# sth is destroyed
}
if you are using globals, and your sth's don't get destroyed by going out of scope, you should call sth->finish.
Cheers,
Alex
--
Gossamer Threads Inc.
Nov 14, 2000, 10:16 PM
Veteran (6956 posts)
Nov 14, 2000, 10:16 PM
Post #5 of 12
Views: 9611
Mysql is good about destroying the handles, other databases are not.
One good thing is to always run under use strict, and check your error logs. You'll often be warned if an auto-destruct on the handle was called, and you'll know you need to add a manual release.
Especially with GT's code, using 'strict' and checking your error logs can provide a wealth of information, since they have good error/debug routines in most of their programs.
PUGDOGŪ
PUGDOGŪ Enterprises, Inc.
FAQ: http://postcards.com/FAQ
One good thing is to always run under use strict, and check your error logs. You'll often be warned if an auto-destruct on the handle was called, and you'll know you need to add a manual release.
Especially with GT's code, using 'strict' and checking your error logs can provide a wealth of information, since they have good error/debug routines in most of their programs.
PUGDOGŪ
PUGDOGŪ Enterprises, Inc.
FAQ: http://postcards.com/FAQ
Nov 15, 2000, 10:23 PM
Administrator (9387 posts)
Nov 15, 2000, 10:23 PM
Post #7 of 12
Views: 9585
When you notice it becomes unstable, run a `mysqladmin proc` to see what the queries are. It could just be you have a query that isn't indexed properly.
Cheers,
Alex
--
Gossamer Threads Inc.
Cheers,
Alex
--
Gossamer Threads Inc.
Nov 17, 2000, 2:00 AM
Novice (11 posts)
Nov 17, 2000, 2:00 AM
Post #10 of 12
Views: 9572
Thanks Alex, I got the following result by using SHOW PROSESSLIST:
Id User Host db Command Time State Info
424880 hiu localhost users Sleep 4 NULL NULL
424881 hiu localhost users Sleep 4 NULL NULL
424882 hiu localhost users Sleep 4 NULL NULL
424883 hiu localhost users Sleep 4 NULL NULL
424884 hiu localhost users Sleep 4 NULL NULL
425508 hiu localhost users Sleep 4 NULL NULL
425510 hiu localhost users Sleep 4 NULL NULL
426063 hiu localhost users Sleep 14 NULL NULL
426064 hiu localhost users Sleep 14 NULL NULL
.......... lots of sleep commands .........
426091 hiu localhost users Query 0 NULL SHOW PROSESSLIST
what's the meaning of "sleep"?
Id User Host db Command Time State Info
424880 hiu localhost users Sleep 4 NULL NULL
424881 hiu localhost users Sleep 4 NULL NULL
424882 hiu localhost users Sleep 4 NULL NULL
424883 hiu localhost users Sleep 4 NULL NULL
424884 hiu localhost users Sleep 4 NULL NULL
425508 hiu localhost users Sleep 4 NULL NULL
425510 hiu localhost users Sleep 4 NULL NULL
426063 hiu localhost users Sleep 14 NULL NULL
426064 hiu localhost users Sleep 14 NULL NULL
.......... lots of sleep commands .........
426091 hiu localhost users Query 0 NULL SHOW PROSESSLIST
what's the meaning of "sleep"?
Nov 17, 2000, 2:35 PM
Administrator (9387 posts)
Nov 17, 2000, 2:35 PM
Post #11 of 12
Views: 9564
Sounds like you have something not releasing connections. Sleep means it's not doing any queries, but is still conneceted. You will eventually run out of connections to mysql and you will start getting fatal errors from DBI.
Cheers,
Alex
--
Gossamer Threads Inc.
Cheers,
Alex
--
Gossamer Threads Inc.