1

I have created several functions to pull data from a MySQL database.

I have my main query that loops through each horse in a racecard and uses the functions to pull data on that horse. The code that uses the functions is below:

//note I'm using select * until I finish knowing what I need to pull//

$horses12 = mysqli_query($db, 'SELECT * FROM tom_cards');
while($todayhorse12 = mysqli_fetch_array($horses12)){
$horse = $todayhorse12['Horse'];
$distance = $todayhorse12['Distance'];
$trainer = $todayhorse12['Trainer'];
$jockey = $todayhorse12['Jockeys_Claim'];
$weight = $todayhorse12['Weight'];
$class = $todayhorse12['Class'];

//function calls go here e.g
echo $horse.horselargerace($horse,$db)."<br />";

}

The issue as every function calls my database and it either takes forever or connections time out. Below are the functions - can anyone figure out a way to cut down on the number of MySQL connections I need to make?

///////////////////////////////////////////////////////////////////////////////////////
//did the horse run in a large field of 12 or more horses and perform well recently?//
//////////////////////////////////////////////////////////////////////////////////////
function horselargerace($horse, $db)
{
    $horses = mysqli_query($db, 'SELECT * FROM `horsesrp` WHERE `horse` = "' . $horse . '" and Runners > 12  ORDER BY Date Limit 5');
    $count  = 0;
    while ($todayhorse = mysqli_fetch_array($horses)) {
        $runners = 0;
        if ((int) $todayhorse['Place'] < 5) {
            $count = $count + 1;
        }
    }
    return $count;
}
//////////////////////////////////////////////////////////////////
//is the horse moving up in class after a good finish or a win?//
////////////////////////////////////////////////////////////////
function horselastclass($horse, $db, $class)
{
    $horses = mysqli_query($db, 'SELECT * FROM `horsesrp` WHERE `horse` = "' . $horse . '" ORDER BY Date Limit 1');
    $count  = 0;
    while ($todayhorse = mysqli_fetch_array($horses)) {
        $class2 = trim(str_replace("Class", "", $todayhorse['Class']));
        $class  = trim(str_replace("Class", "", $class));
        if ($class2 == "") {
            $class2 = $class;
        }
        if (trim($class) != "" or trim($class2) != "") {
            //if a horse is being dropped in class this should be easier
            if ((int) $class < (int) $class2) {
                $count = $count + 1;
            } elseif ((int) $class > (int) $class2) {
                $count = $count - 1;
            }
        }
    }
    return $count;
}
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//is the horse picking up or dropping weight today?                                                                                                               //
// 114pds or under is ideal.horse drops 5pds or more from the last start i take that as a positive, if he picks up more than 5pds then i consider that a negative.//
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
function horselastweight($horse, $db, $weight)
{
    $horses = mysqli_query($db, 'SELECT * FROM `horsesrp` WHERE `horse` = "' . $horse . '" ORDER BY Date Limit 1');
    $count  = 0;
    while ($todayhorse = mysqli_fetch_array($horses)) {
        $weight2 = preg_replace("/[^a-zA-Z]/", "", $weight);
        $weight2 = substr($weight2, 0, 1);
        if ($weight2 <> "") {
            $weight = substr($weight, 0, strpos($weight, $weight2));
        }
        //get stone and convert to pounds
        $total1 = (((int) substr($weight, 0, strpos($weight, "-"))) * 14) + (int) substr($weight, 1, strpos($weight, "-"));
        $total2 = (((int) substr(str_replace(chr(194), "", $todayhorse['Weight']), 0, strpos(str_replace(chr(194), "", $todayhorse['Weight']), "-"))) * 14) + (int) substr(str_replace(chr(194), "", $todayhorse['Weight']), 1, strpos(str_replace(chr(194), "", $todayhorse['Weight']), "-"));
        $weight = str_replace(chr(194), "", $todayhorse['Weight']) . "=" . $weight;
    }
    $total = (int) $total2 - (int) $total1;
    if ((int) $total > 4) {
        $count = $count + 1;
    } elseif ((int) $total < -4) {
        $count = $count - 1;
    }
    return $count;
}
//////////////////////////////////////////////////////////////////////////////
//did the horse have trouble in his/her last race? (comments broke slow ect)//
/////////////////////////////////////////////////////////////////////////////
function horsehavetrouble($horse, $db)
{
    $horses = mysqli_query($db, 'SELECT * FROM `horsesrp` WHERE `horse` = "' . $horse . '" ORDER BY Date Limit 1');
    $count  = 0;
    while ($todayhorse = mysqli_fetch_array($horses)) {
        if ($todayhorse['Place'] = "2" or $todayhorse['Place'] = "3" or $todayhorse['Place'] = "4" or $todayhorse['Place'] = "5") {
            $targets = array(
                "hampered",
                "awkward",
                "stumbled",
                "slipped",
                "jinked",
                "fell",
                "unseated"
            );
            foreach ($targets as $target) {
                if (strstr(strtolower($todayhorse['Comments']), $target) !== false) {
                    $count = $count + 1;
                }
            }
        }
    }
    return $count;
}
///////////////////////////////////////////////////////////////
//is the same jockey back on today after not winning in last?//
///////////////////////////////////////////////////////////////
function isjockeyonagainafterlosing($horse, $db, $jockey)
{
    $horses = mysqli_query($db, 'SELECT * FROM `horsesrp` WHERE `horse` = "' . $horse . '" and Place != "1" ORDER BY Date Limit 1');
    $count  = 0;
    while ($todayhorse = mysqli_fetch_array($horses)) {
        $pop = array_pop(explode(' ', $todayhorse['Jockeys_Claim']));
        if (trim(array_pop(explode(' ', $todayhorse['Jockeys_Claim']))) == trim(array_pop(explode(' ', trim($jockey))))) {
            $count = $count + 1;
        }
    }
    return $count;
}
//////////////////////////////////////////////////////
//has the jockey won previously on this same horse?//
////////////////////////////////////////////////////
function Hasjockeywonbeforeonhorse($horse, $db, $jockey)
{
    $horses = mysqli_query($db, 'SELECT * FROM `horsesrp` WHERE `horse` = "' . $horse . '" and Place ="1" ORDER BY Date');
    $count  = 0;
    while ($todayhorse = mysqli_fetch_array($horses)) {
        //get todays jockey and check to see if it matches with prev ones 
        if (trim(array_pop(explode(' ', $todayhorse['Jockeys_Claim']))) <> trim(array_pop(explode(' ', trim($jockey))))) {
            $count = $count + 1;
        }
    }
    return $count;
}
////////////////////////////////////
//is the horse changing trainers?//
//////////////////////////////////
function horsechnagedtrainers($horse, $db, $trainer)
{
    $horses = mysqli_query($db, 'SELECT * FROM `horsesrp` WHERE `horse` = "' . $horse . '" ORDER BY Date Limit 1');
    while ($todayhorse = mysqli_fetch_array($horses)) {
        $count = 0;
        //compare last trainer and current
        if (trim(array_pop(explode(' ', $todayhorse['Trainer']))) <> trim(array_pop(explode(' ', trim($trainer))))) {
            $count = $count + 1;
        }
    }
    return $count;
}
///////////////////////////////////////////////
//has the horse won at high odds in the past?//
///////////////////////////////////////////////
function horsehighodds($horse, $db)
{
    $horses = mysqli_query($db, 'SELECT Odds FROM `horsesrp` WHERE `horse` = "' . $horse . '" and Place ="1" ORDER BY Date Limit 1');
    while ($todayhorse = mysqli_fetch_array($horses)) {
        $fraction = str_replace("F", "", $todayhorse['Odds']);
        $fraction = str_replace("J", "", $fraction);
        $fraction = explode("/", $fraction);
        if ($fraction[1] != 0) {
            $fraction = ($fraction[0] / $fraction[1]);
        }
        $fraction = (int) $fraction;
        if (in_array((int) $fraction, range(2, 6))) {
            $count = $count + 1;
        }
    }
    return $count;
}
///////////////////////////////////////////////////////
//was the horse between 2-1 & 6-1 odds in last start?//
///////////////////////////////////////////////////////
function horsebetween2and6($horse, $db)
{
    $horses = mysqli_query($db, 'SELECT Odds FROM `horsesrp` WHERE `horse` = "' . $horse . '" ORDER BY Date Limit 1');
    $count  = 0;
    while ($todayhorse = mysqli_fetch_array($horses)) {
        //convert the odds to decimal
        $fraction = str_replace("F", "", $todayhorse['Odds']);
        $fraction = str_replace("J", "", $fraction);
        $fraction = explode("/", $fraction);
        if ($fraction[1] != 0) {
            $fraction = ($fraction[0] / $fraction[1]);
        }
        $fraction = (int) $fraction;
        if ((int) $fraction <= 2 and (int) $fraction >= 6) {
            $count = $count + 1;
        }
        if (in_array((int) $fraction, range(2, 6))) {
            $count = $count + 1;
        }
        return $count;
    }
}
//////////////////////////////////////////////////////
//was this horse a beaten favorite in last 3 starts?//
//////////////////////////////////////////////////////
function horsebeatenfav($horse, $db)
{
    $count  = 0;
    $horses = mysqli_query($db, 'SELECT * FROM `horsesrp` WHERE `horse` = "' . $horse . '" ORDER BY Date Limit 3');
    while ($todayhorse = mysqli_fetch_array($horses)) {
        if ($todayhorse['Place'] <> "1" and strpos($todayhorse['Odds'], 'F') !== false) {
            $count = $count + 1;
        }
    }
    return $count;
}
////////////////////////////////////////////////
//How many starts has the horse had this year?//
////////////////////////////////////////////////
function startswithin12months($horse, $db)
{
    $startdate = date('Y-m-d', strtotime('-1 year'));
    $enddate   = date('Y-m-d');
    $horses    = mysqli_query($db, 'SELECT Date FROM horsesrp where Horse = "' . $horse . '" and Date >= "' . $startdate . '" AND Date <= "' . $enddate . '" ORDER BY Date');
    return $horses->num_rows;
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//Is the horse changing distances today? - find out if the horse has ever won at this distance - if not -1point//
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////
function hashorsechangedistance($horse, $distance, $db)
{
    //select all distances this horse has run
    $horses        = mysqli_query($db, 'SELECT Distance FROM horsesrp where Horse = "' . $horse . '" ORDER BY Date');
    //count the times its run at this distance
    $distancecount = 0;
    while ($todayhorse = mysqli_fetch_array($horses)) {
        if ($todayhorse['Distance'] == $distance) {
            $distancecount = $distancecount + 1;
        }
    }
    //if distance is greater then 0 its ran at this distance before
    return $distancecount;
}
/////////////////////////////////////////////////////////
//How long has the horse been off (time between races)?//
/////////////////////////////////////////////////////////
function horselastrace($horse, $db)
{
    //select horse last run
    $sql    = 'SELECT `Date` FROM `horsesrp` where `Horse` = "' . $horse . '" ORDER BY Date limit 1';
    $horses = mysqli_query($db, $sql);
    while ($todayhorse = mysqli_fetch_array($horses)) {
        $dStart = new DateTime($todayhorse['Date']);
        $dEnd   = new DateTime(date('Y-m-d'));
        $dDiff  = $dStart->diff($dEnd);
        return $dDiff->days;
    }
}

Queries grouped together will be as follows:

$horses = mysqli_query($db, 'SELECT * FROM `horsesrp` WHERE `horse` = "'.$horse.'" ORDER BY Date Limit 1');
$horses = mysqli_query($db, 'SELECT * FROM `horsesrp` WHERE `horse` = "'.$horse.'" ORDER BY Date Limit 1');
$horses = mysqli_query($db, 'SELECT * FROM `horsesrp` WHERE `horse` = "'.$horse.'" ORDER BY Date Limit 1');
$horses = mysqli_query($db, 'SELECT * FROM `horsesrp` WHERE `horse` = "'.$horse.'" ORDER BY Date Limit 1');
$horses = mysqli_query($db, 'SELECT Odds FROM `horsesrp` WHERE `horse` = "'.$horse.'" ORDER BY Date Limit 1');
$sql = 'SELECT `Date` FROM `horsesrp` where `Horse` = "'.$horse.'" ORDER BY Date limit 1';
$horses = mysqli_query($db, 'SELECT * FROM `horsesrp` WHERE `horse` = "'.$horse.'" and Place != "1" ORDER BY Date Limit 1');
$horses = mysqli_query($db, 'SELECT * FROM `horsesrp` WHERE `horse` = "'.$horse.'" and Place ="1" ORDER BY Date');
$horses = mysqli_query($db, 'SELECT Odds FROM `horsesrp` WHERE `horse` = "'.$horse.'" and Place ="1" ORDER BY Date Limit 1');
$horses = mysqli_query($db, 'SELECT * FROM `horsesrp` WHERE `horse` = "'.$horse.'" ORDER BY Date Limit 3');
$horses = mysqli_query($db, 'SELECT Date FROM horsesrp where Horse = "'.$horse.'" and Date >= "'.$startdate.'" AND Date <= "'.$enddate.'" ORDER BY Date');
$horses = mysqli_query($db, 'SELECT Distance FROM horsesrp where Horse = "'.$horse.'"');

Table structure:

Field
Type
Null
Key
Default
Extra
ID
int(255)
NO
PRI
NULL
auto_increment
ParentID
int(255)
NO
NULL
Date
date
NO
NULL
Track
varchar(100)
YES
NULL
Runners
varchar(50)
YES
NULL
Going
varchar(50)
YES
NULL
Distance
varchar(50)
YES
NULL
Class
varchar(50)
YES
NULL
Place
varchar(10)
YES
NULL
Losing_Dist
varchar(50)
YES
NULL
Stall
varchar(250)
YES
NULL
Horse
varchar(50)
YES
NULL
Weight
varchar(50)
YES
NULL
Trainer
varchar(50)
YES
NULL
Odds
varchar(50)
YES
NULL
Oddsmovement
varchar(250)
NO
NULL
Jockeys_Claim
varchar(50)
YES
NULL
Comments
varchar(250)
YES
NULL
Race_Name
varchar(250)
YES
NULL
Timetaken
varchar(255)
NO
NULL
OR
varchar(6)
NO
NULL
halfer
  • 19,824
  • 17
  • 99
  • 186
emma perkins
  • 749
  • 1
  • 10
  • 28
  • Is it connecting successfully? If it times out maybe there are a lot of records to process. If it is not connecting then maybe there is not a network route to the database server. So the first thing to do is to find out from your code if it is connecting. – halfer Jan 02 '15 at 14:48
  • (Incidentally you might have SQL injection vulnerabilities in this code, and need to start using parameterisation in order to protect your application). – halfer Jan 02 '15 at 14:49
  • Sorry the code if fine and connects fine. I think its just too many connections to the database to sift through to many records when i maybe be able to pull the data quicker via 1 connection maybe instead of several – emma perkins Jan 02 '15 at 14:49
  • Ah yes - I've not read all of the code, but if you are connecting for every query, that needs fixing first. You only need to connect once per page. (It may be that you have too many records to process for a web process - how many records do you have? It may be better in an offline process). – halfer Jan 02 '15 at 14:50
  • How would you make it into one query the feed it into the functions? – emma perkins Jan 02 '15 at 14:59
  • I am able to group the first 5 into one but then the last 6 are different querys – emma perkins Jan 02 '15 at 15:00
  • also i have 996917 but this will keep increasing – emma perkins Jan 02 '15 at 15:07
  • Ah, we may be talking at cross purposes. You originally said you were using too many connections, but I am now not sure this is the case. I suspect what you meant was you are using too many queries, an entirely different thing. The connection is when you supplied a username, password and database name to "open" the database. How many times are you doing that per run of this code? – halfer Jan 02 '15 at 15:15
  • Nearly 1 million queries is way too many for a web process. Move this to a cron or a queued task. – halfer Jan 02 '15 at 15:18
  • well what im going to do is create the code then move it to a cron job - run the data once aday and store results in a database – emma perkins Jan 02 '15 at 15:55
  • Good idea. By default, PHP running on the console doesn't have a timeout. – halfer Jan 02 '15 at 17:39

1 Answers1

0

Do you have index on field Horse in table horsesrp? Specified queries should be processed fast, even with large dataset. You can do this as follows:

 ALTER TABLE horsesrp ADD INDEX Horse (Horse);

More on MySQL Indexes:

http://dev.mysql.com/doc/refman/5.5/en/mysql-indexes.html

How do MySQL indexes work?

Community
  • 1
  • 1
Kepi
  • 374
  • 2
  • 7
  • i have posted my table structure in the main post for you – emma perkins Jan 02 '15 at 15:05
  • As he orders by date, might an index HorseDate(Horse,Date) speed it up even a bit more? – Alfons Jan 02 '15 at 16:10
  • @Alfons: or she. I tend to recommend not making gender assumptions on Stack Overflow, as it can accidentally create an exclusionary environment. (FWIW it is common to use gender neutral phraseology, like "the OP"). – halfer Jan 02 '15 at 17:46