Hi all,
I am hosting
www.nicetranslator.com on my linode (this is the only live site its hosting).
My goal is to have it logging every phrase translated (after the user waits 1.5/s without typing).
I have a table of phrases:
phraseID | phrase | phraseLang | sourceCount | resultCount
(sourceCount is the number of times the phrase is the source, result is number of times its the result)
And a table of translations
translationID | ip | timeSubmitted | sourceID | resultID
The following is the PHP I've got (including the functions I've used, rowExists() and getID() ):
Code:
<?php
require_once('../startFiles/StartPhp.dinc.php');
$userIP = $_GET['ip'];
$source = addslashes($_GET['source']);
$result = addslashes($_GET['result']);
$srcLang = $_GET['srcLang'];
$resLang = $_GET['resLang'];
if(($source == '') || ($result == '')){
die();
}
/* =Insert phrase if its new, otherwise increment its count
---------------------------------------------*/
if(rowExists("phrases", "phrase = '$source' AND phraseLang = '$srcLang'")){
$incSrcPhrase = mysql_query("UPDATE phrases SET sourceCount = sourceCount + 1 WHERE phrase = '$source' AND phraseLang ='$srcLang'")or die(mysql_error());
}
else{
$insertSrcPhrase = mysql_query("INSERT INTO phrases(phraseLang, phrase, sourceCount) VALUES('$srcLang', '$source', 1)")or die(mysql_error());
}
if(rowExists("phrases", "phrase = '$result' AND phraseLang = '$resLang'")){
$incResPhrase = mysql_query("UPDATE phrases SET resultCount = resultCount + 1 WHERE phrase = '$result' AND phraseLang ='$resLang'");
}
else{
$insertResPhrase = mysql_query("INSERT INTO phrases(phraseLang, phrase, resultCount) VALUES('$resLang', '$result', 1)");
}
/* =Log translation [ I've kept this commented out as of recent testings, doesn't seem to lessen the load ]
---------------------------------------------*/
$srcPhraseID= getID("phrases", "phrase", "phrase = '$source'");
$resPhraseID= getID("phrases", "phrase", "phrase = '$result'");
$insertTranslation = mysql_query("INSERT INTO translations(ip, sourceID, resultID) VALUES('$userIP', '$srcPhraseID', '$resPhraseID')");
?>
<?php
function rowExists($table, $where){
$numQuery = mysql_query("SELECT * FROM $table WHERE $where") or die(mysql_error());
$numRows = mysql_num_rows($numQuery);
return ($numRows>0);
}
function getID($table, $prefix, $where){
$theID = $prefix . "ID";
$queryRow = mysql_fetch_row(mysql_query("SELECT $theID FROM $table WHERE $where")) or die(mysql_error());
return $queryRow[0];
}
?>
When I run this, my CPU use instantly spikes to over 100%. I tried commenting out all the code querying the translation table, so it is only saving phrases and I get the same result.
I have run the optimization, which seemed to have some small affect when I first ran it, but no longer does.
The phrase table now has about 230,000 rows.
To be honest, this is my first project to draw such an audience so my first time dealing with a database this size. I was under the impression, though, that mySQL was quite apt to handle such a job.
What am I missing? Your help is very much appreciated.
Thanks for your time,
Nicky