Forum Moderators: coopster

Message Too Old, No Replies

Script Locking Up and Causing 500 Errors

php mysql script locking up on read & select

         

MrCaltweet

4:58 pm on Mar 17, 2010 (gmt 0)

10+ Year Member



A script is locking up and causing PHP errors because processes aren't finishing.

Mysql Insert Query Locking Becuz It Tries to Select before Read Lock Is Finish
My site is having some problems and getting locked on an insert (cause lock) and then it tries to select b4 read lock is finished causing my script on calendar.php to lock the site up which causes it (during heavy traffic) to just keep reloading without finishing out the whole page.

Here is the exact message my administrator sent me: (t appears that your script is causing contention with the database it is trying to access. You are inserting (causing a lock) ) and then attempting to select, and the read lock isn't complete yet. This is causing the lock contention that your calendar script is suffering from. Please reevaluate your code to streamline your queries and/or change it to atomic writes (by using the lock procedure)

What be the best thing to do at this point? Because its causing me to miss out on THOUSANDS OF DAILY USERS).

Need help Quick

Here is the file in question:
<?php 
$result = mysql_query("select uid, profile_image_url,screen_name from users order by made desc limit 12");
$num = 0;
while($row = mysql_fetch_array($result)){
$num++;
$img = $row['profile_image_url'];
if($img == "") { $img = "http://static.twitter.com/images/default_profile_normal.png"; }
$uid = $row['uid'];
$screen_name = $row['screen_name'];
echo "<a href=\"./u/$screen_name\"><img width=\"48px\" height=\"48px\" src=\"$img\"></a>";
if($num % 4 == 0){ echo "<br>"; }
}
?>
</div>
</div>
<div style="float:left;width:100%;">
<b class="wgb4"></b><b class="wgb3"></b><b class="wgb2"></b><b class="wgb1"></b></b>
</div>
</div>
</div>
<!--- END RECENT TWEETERS -->


<!--- SPOTLIGHT -->
<div style="width:420px;float:left;margin-left:30px;">
<div style="margin-left:0px;margin-top:0px;float:left;width:100%;">
<b class="wgtop"><b class="wgb1"></b><b class="wgb2"></b><b class="wgb3"></b><b class="wgb4"></b></b>
<div class="blue_box" style="height:200px; width:418px; color:#ffffff; text-align:center;">
<strong>Social Event Sharing</strong>
<ul style="size:8px; color:#000000; text-align:left;">
<li>Share/Find Events via Twitter, Facebook, Myspace, Email & other social media sites right from Caltweet</li>
<br>
<li>Caltweet is the fastest, easiest, & safest ways to Promote your events online</li>
<br>
<li>Print Guestlist & Have RSVP's Emailed Directly to your inbox <a href="http://example.com/create.php">Click Here to Start</a> or <a href="howto.php">Learn How to Use Example</a> </li>
<!-- <a href="http://example.com/961">
<img src="http://www.example.com/imgs/caltweet-charity.jpg" title="Example 4 Charity" alt="Example 4 Charity"/></a><br /><font color="#000000">Click here:</font> <a href="/961">Example4 Charity</a><br />-->
<form style="margin-top: 10px;" action="./allevents.php" method="get">
<input onkeyup="searchEvents();" size="42" autocomplete="off" style="height:9; font-size:12px; padding:0px;" type="search" name="se" id="se" />
<input style="height:9; font-size:12px; padding:0px;" value="Search" type="submit" /><br />
<div style=" z-index:1000; position:absolute; visibility:hidden; text-align:left; margin:0px; padding:0px; width:330px; background-color:#CCCCCC;" name="search" id="search"></div>
</form>
</ul>

</div>
<div style="float:left;width:100%;">
<b class="wgb4"></b><b class="wgb3"></b><b class="wgb2"></b><b class="wgb1"></b></b>
</div>
</div>
</div>
<!--- END SPOTLIGHT -->



</div>
<div class="right">
<div class="tags"><div style="height:185px; overflow:hidden;"><img src="/imgs/section_names_03.jpg" />

<?php
$max = mysql_fetch_array(mysql_query("select max(tag_count) as max from tags"));
$max = $max['max'];
$small = 10;
$large = 25;
$num = 0;
$result = mysql_query("select * from tags order by rand() limit 15");
while ($tag = mysql_fetch_array($result)){
if($num > 0){ echo ", "; }
$num++;
$tagcount = $tag['tag_count'];
if($tagcount == "" || $tagcount < 1){ $tagcount = 1; }
$percent = $tagcount / $max;
$size = (($large - $small) * $percent) + $small;
echo "<font size=\"{$size}px\" style=\"font-size: {$size}px;\"><a href=\"/t/". str_replace('+', '-', urlencode($tag['tag'])) . "\">{$tag['tag']}</a></font>";
}
?></div><div style="height:100px; overflow:hidden;">
Follow the Creators:<br />
<a href="http://twitter.com/mrcaltweet"><img src="http://example.com/imgs/freddy.jpg" width="55px" height="55px" /></a>
<a href="http://twitter.com/person_name"><img src="http://a3.twimg.com/profile_images/457209831/6935_167176678078_697813078_3722745_2905007_n-1_normal.jpg" width="55px" height="55px" /></a>

</div>

</div>
</div>
</div>
<div class="bottom">
<!--- RECENT EVENTS -->
<div style="width:860px;float:left;margin-left:18px;">
<div style="margin-left:0px;margin-top:0px;float:left;width:100%;">
<b class="retop"><b class="reb1"></b><b class="reb2"></b><b class="reb3"></b><b class="reb4"></b></b>
<div class="re_box" style="height:120px; width:857px;">


<div class="re_pic"><img src="/imgs/section_names_14.jpg" /></div>
<div class="seperator"></div>
<?php
$result = mysql_query("select * from events join users on (user_id = uid) where ispublic = 1 order by created desc limit 4");
$i = 0;
while($row = mysql_fetch_array($result)){
$i++;
$event_id = $row['event_id'];
$hex_id = encode_eid($event_id);
$event_time = date("F j, Y, g:i a", $row['event_time']);
$link = "<a href='http://caltweet.com/$hex_id'>";
$event_title = stripslashes($row['event_title']);
//$event_pic = "<img width='300px' height='300px' src='{$row['event_pic']}'>";
$event_pic = max_picture($row['event_pic'], 120, 90, $event_title);
$event_desc = $row['event_desc'];


thanks

[edited by: eelixduppy at 6:03 pm (utc) on Mar 17, 2010]
[edit reason] removed specifics [/edit]

Matthew1980

8:26 pm on Mar 17, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Hi there MrCaltweet,

Welcome to webmaster world ;-p

The only thing as I can see that's obvious to me is this:-


$max = mysql_fetch_array(mysql_query("select max(tag_count) as max from tags"));


I don't know whether It can be done as I have never tried it as it seems bad practice to me, split it up, then you can put error checking on to the function:-

$sqlGetInfo = mysql_query("select max(tag_count) as max from tags") or die(mysql_error());
$max = mysql_fetch_array($sqlGetInfo) or die(mysql_error());


Then at least you will know if your getting any DB side errors, but please ONLY use the or die(mysql_error()); for debugging, DONT leave it on for release, as this could give things away about you DB structure to hackers if something went wrong. It's best to use this function when in development mode.

If you can test this locally, put error_reporting(E_ALL); at the top of the file/page your working on, this will then flag up any php errors there may be, same disclaimer as above though ;-p

I'm not saying as this will fix anything, but it might give you an idea of where something isn't quite right.

Good luck :-p

Cheers,
MRb

MrCaltweet

8:55 pm on Mar 17, 2010 (gmt 0)

10+ Year Member



appreciate the feedback...this looks like its going to take some time..cuz I have no idea where to start...lol

Matthew1980

9:13 pm on Mar 17, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Hi there MrCaltweet,

From the reply, I assume that your not a too experienced coder/programmer then, saying that I only commented on what I can see, there are far more experienced programmers on this forum than me that could more than likely answer your question with the blink of an eye [HINT/NUDGE/WINK to the MODS!] ;-p

I think it will be a case of 1 step at a time, first thing I would personally do is definitely split that query up as I suggested, whether this affects processing time I can't say, but that's just my opinion, I could well be wrong! I hope I'm not though ;-p

Good luck!


Cheers,
MRb

MrCaltweet

9:27 pm on Mar 17, 2010 (gmt 0)

10+ Year Member



for right now i just took the process off the homepage..all it does is generate tags on homepage and im guessing with as many events we have now on the site over 10k ...there are TRIPLE the amount of tags and randomly selecting them and pushing them out is hogging up the process so for now its an easy/sloppy fix, but doesn't hurt the user experience as much as the 500 errors.

Yes your right I'm a def NEWBIE to programming...So I'm trying to figure out how placing an error response in the query will help? :/

Matthew1980

9:57 pm on Mar 17, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Hi there MrCaltweet,

Basically from what I can see adding the error messaging will just flag up any anomalies as the code executes of the server, it's the best way to rid you code of typo's and undefined indexes (variables without references or start points)

Wow! Your site must be pretty big, this could be a timing issue of how long it takes for a single process to execute then, and that is where I have no idea :/

I will presume as you have access to a local server (development environment on your laptop/PC) as this will certainly help you figure error's and glitches out.

Cheers,
MRb

MrCaltweet

10:02 pm on Mar 17, 2010 (gmt 0)

10+ Year Member



yes its pretty HUGE...on twitter/facebook we just added support for fbook and as it grew then this problem just arrived.

thats why this was so urgent we were losing thousands of visitors.

[edited by: jatar_k at 12:51 pm (utc) on Mar 18, 2010]
[edit reason] no specifics thanks [/edit]

jatar_k

1:11 pm on Mar 18, 2010 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



this would probably be the culprit

$result = mysql_query("select * from tags order by rand() limit 15");

another way to select the random tags without ordering a gigantic table, a separate table might be an idea, or a generated file somewhere that can just be included. The main page is going to be your heaviest page. You would be better off, especially if you have that much traffic to remove all of the queries from that page if possible and have included files or some other approach that can be generated.

or some other approach, separate table that is constantly shifting keys or some other interesting approach.

or you could use a random offset in a limit query, though you would have to watch for dupes, if there are no dupes in the table then this approach might work and provide a simulated randomness.

You don't have to have actual random results, they just have to appear random.

MrCaltweet

4:19 pm on Mar 18, 2010 (gmt 0)

10+ Year Member



I think I'm just going to leave it off..since we now have a search bar on the homnepage and about to release Caltweet 2.0 and no tags are on the front..but we still use tags as keywords for search engines..

and thnx