Hi there guys,
I'm back with a revised version. Thanks to everyone's guidance, I made the following changes:
First, I UNSET everything I could right after I was done with it in that loop.
Secondly, I got rid of almost all the selects and inserts and did get rid of all the updates by creating one master searchpool array that each section builds upon. With this new setup, there's only one searchpool insert loop for each item ID.
Finally, I altered the queries to only do what's required. I dropped orders where it didn't matter and I only asked for elements that I needed.
Before I show the code, I ran this and it completed the entire process in 17 seconds. This is in comparison to the 4:30+ time of yesterday's version. Also of note is that there was no degradation in performance between the start and the end. It was running as quickly at the end as it was when it started. In spite of this ridonkulous increase in performance, I'd greatly appreciate any suggestions in further improvements if anyone spots anything glaring.
/* Time to do some actual work. */
/* Wipe out current searchpool data. */
$q_dsp = "DELETE FROM searchpool";
$r_dsp = mysqli_query ($link, $q_dsp) or die('Catastrophic failure [Super secret code 7219229]');
$i = 1;
$q_prim = "SELECT id, title, mpn, description FROM cart_items";
$r_prim = mysqli_query ($link, $q_prim) or die('Catastrophic failure [Super secret code 48155]');
$n_prim = mysqli_num_rows($r_prim);
$total = $n_prim;
while ($row_prim = mysqli_fetch_assoc ($r_prim)) {
$ciid = $row_prim['id'];
$title = $row_prim['title'];
$mpn = $row_prim['mpn'];
$description = $row_prim['description'];
/* Build the MPN array */
/* A weight of 999999 is an MPN */
$mpn_array[0] = $mpn;
$q_mpn = "SELECT mpn FROM mpns WHERE ciid = '$ciid'";
$r_mpn = mysqli_query($link, $q_mpn) or die('Catastrophic failure [Super secret code 715495]');
while ($row_mpn = mysqli_fetch_assoc ($r_mpn)) {
$mpn_array[] = $row_mpn['mpn'];
}
/* Build the keywords array */
$keyword_array = array();
$q_kw = "SELECT * FROM keywords WHERE ciid = '$ciid'";
$r_kw = mysqli_query($link, $q_kw) or die('Catastrophic failure [Super secret code 384955]');
while ($row_kw = mysqli_fetch_assoc ($r_kw)) {
$keyword_array[] = $row_kw['string'];
}
/* Additional specs are needed */
$q_specs = "SELECT name, value FROM specs WHERE ciid = '$ciid' ORDER BY id ASC";
$r_specs = mysqli_query($link, $q_specs) or die('Catastrophic failure [Super secret code 954219]');
while ($row_specs = mysqli_fetch_assoc ($r_specs)) {
$spec_array[]['name'] = $row_specs['name'];
$spec_array[]['value'] = $row_specs['value'];
}
/**************************************/
/** Search Pool Start **/
/**************************************/
$sp_array = array();
/* Title Start */
$sp_title = stripStopWords($title);
$exploded_title = explode(" ", strtolower($sp_title));
foreach ($exploded_title as $string){
$string = leavealphanum(trim($string));
if($string != ''){
if (!in_array(strtolower($string), $sp_array)) {
$sp_array[$string] = 20;
}else{
$sp_array[$string] = $sp_array[$string] + 20;
}
}
}
UNSET($exploded_title);
/* Title End */
/* MPNs Start */
/* Loop through the MPN array. */
FOREACH($mpn_array AS $string){
if (!in_array($string, $sp_array) AND $string != '') {
$sp_array[$string] = 999999;
}else{
$sp_array[$string] = $sp_array[$string] + 999999;
}
$string2 = leavealnumspacers($string);
if (!in_array($string2, $sp_array) AND $string2 != '') {
$sp_array[$string2] = 999999;
}
$string3 = leavealphanum($string);
if (!in_array($string3, $sp_array) AND $string3 != '') {
$sp_array[$string] = 999999;
}
}
UNSET($mpn_array);
/* MPNs End */
/* Description Start */
$sp_desc = stripStopWords(strtolower(strip_tags($description)));
$desc_exploded = explode(" ", leavealnumspacers(strtolower($sp_desc)));
FOREACH($desc_exploded AS $string){
$string = trim($string);
if($string != ''){
IF(!array_key_exists ($string, $sp_array)) {
$sp_array[$string] = 1;
}else{
$sp_array[$string] = $sp_array[$string] + 1;
}
}
}
UNSET($desc_exploded);
/* Description End */
/* Keywords Start */
IF(!EMPTY($keyword_array)){
FOREACH($keyword_array AS $string){
$string = trim(strtolower($string));
if($string != ''){
if (!array_key_exists ($string, $sp_array)) {
$sp_array[$string] = 20;
}else{
$sp_array[$string] = $sp_array[$string] + 20;
}
}
}
}
UNSET($keyword_array);
/* Keywords End */
/* Additional Specs Start */
$spec_strings = array();
$spec_string = '';
IF(ISSET($spec_array)){
FOREACH($spec_array AS $spec_entry){
$spec_string .= $spec_entry['name'] .' '. $spec_entry['value'].' ';
}
$spec_exploded = explode(" ", leavealnumspacers(strtolower($spec_string)));
FOREACH($spec_exploded AS $string){
if($string != ''){
$string = trim($string);
if (!array_key_exists ($string, $sp_array)) {
$sp_array[$string] = 1;
}else{
$sp_array[$string] = $sp_array[$string] + 1;
}
}
}
UNSET($spec_exploded);
}
/* Additional Specs End */
/* We have created the searchpool array. Insert it into the database. */
FOREACH($sp_array AS $sp_key => $sp_value){
$sp_key = mysqli_real_escape_string($link, $sp_key);
$sp_val = mysqli_real_escape_string($link, $sp_val);
$q_spin = "INSERT INTO searchpool (ciid, string, weight) VALUES ('$ciid', '$sp_key', '$sp_val')";
$r_psin = mysqli_query($link, $q_spin) or die('Catastrophic failure [Super secret code 95412]');
}
/**************************************/
/** Search Pool End **/
/**************************************/