0

I am retrieving 5000 plus data in my MYSQL database. I takes 5-10 minutes to retrieve the data (it is only just local slower when over the network) is there a way to improve the speed without using a plugin?

$ContactID = $_GET["Contact"];

    $sql = "SELECT * FROM tblContacts WHERE Coordinator = '$ContactID'";
    $result = mysqli_query($conn, $sql);
    $count = mysqli_num_rows($result);

    if($count > 0){
        while ($row = mysqli_fetch_array($result)) {
            $supdate = date("Y-m-d h:i", strtotime($row['ServerUpdate']));
            $mupdate = date("Y-m-d h:i", strtotime($row['MobileUpdate']));
            $ar[] = array(
                'ContactID' => $row['ContactID'],
                'FileAs' => $row['FileAs'],
                'FirstName' => $row['FirstName'],
                'MiddleName' => $row['MiddleName'],
                'LastName' => $row['LastName'],
                'Position' => $row['Position'],
                'Company' => $row['Company'],
                'CompanyID' => $row['CompanyID'],
                'ContactType' => $row['ContactType'],
                'RetailerType' => $row['RetailerType'],
                'PresStreet' => $row['PresStreet'],
                'PresBarangay' => $row['PresBarangay'],
                'PresDistrict' => $row['PresDistrict'],
                'PresTown' => $row['PresTown'],
                'PresProvince' => $row['PresProvince'],
                'PresCountry' => $row['PresCountry'],
                'Landmark' => $row['Landmark'],
                'Telephone1' => $row['Telephone1'],
                'Telephone2' => $row['Telephone2'],
                'Mobile' => $row['Mobile'],
                'Email' => $row['Email'],
                'Employee' => $row['Employee'],
                'Customer' => $row['Customer'],
                'Coordinator' => $row['Coordinator'],
                'ServerUpdate' => $supdate,
                'MobileUpdate' => $mupdate
            );
        }

        print json_encode($ar);
    }      
  • @BehzadDadashpour I am not display the data I am passing the data to my app –  Nov 14 '18 at 06:30
  • 1
    Be very careful with how you use variables in your queries. It's possible to perform SQL injection through the ContactID parameter. Use prepared statements. – Tordek Nov 14 '18 at 06:32
  • @Tordek yes I am going to do it I need to improve the speed of retrieving data –  Nov 14 '18 at 06:34
  • mySql DATE_FORMAT for date converting maybe help – Behzad Dadashpour Nov 14 '18 at 06:36
  • @BehzadDadashpour nope still slow –  Nov 14 '18 at 06:41
  • @MagnusEriksson I know, I just need to improve the retrieval time of data –  Nov 14 '18 at 06:42
  • 1
    I saw in a comment for one of the answers that this is retrieving a large data set. First you need to identify what process it is that 's slow. Is it the SQL query? (Is the `Coordinator` column properly indexed?) Is it your while loop? Is it the json_encode()? Is it outputting the result with `print_r()`? – M. Eriksson Nov 14 '18 at 06:49

2 Answers2

3
$ContactID = $_GET["Contact"];
$ar = array();
$sql = "SELECT ContactID,FileAs, FirstName ,MiddleName ,LastName ,Position ,Company ,CompanyID  ,ContactType ,RetailerType ,PresStreet 
             ,PresBarangay ,PresDistrict  ,PresTown , PresProvince  ,PresCountry ,Landmark   ,Telephone1 , Telephone2  ,Mobile 
              ,Email  ,Employee  ,Customer  ,Coordinator  FROM tblContacts WHERE Coordinator = '$ContactID'";
$result = mysqli_query($conn, $sql);
$count = mysqli_num_rows($result);

if($count > 0){
    $rowCount = 0;
    while ($row = mysqli_fetch_array($result)) {
        $supdate = date("Y-m-d h:i", strtotime($row['ServerUpdate']));
        $mupdate = date("Y-m-d h:i", strtotime($row['MobileUpdate']));
        $ar[$rowCount] = $row;
        $ar[$rowCount]['ServerUpdate'] = $supdate;
        $ar[$rowCount]['ServerUpdate']  = $mupdate;
         $rowCount++;
    }

    print json_encode($ar);
}      

You must declare the variable of array first in the top.

You can directly call from mysql what data needed. Hope can help.

j3thamz
  • 66
  • 4
0

It could be like this way.

$contactID = $_GET["Contact"];
$sql = "SELECT * FROM tblContacts WHERE Coordinator = '$contactID'";
$result = mysqli_query($conn, $sql);
if(mysqli_num_rows($result) > 0){
    $data = array();
    while($row = mysqli_fetch_array($result)){

        $row['supdate'] = date("Y-m-d h:i", strtotime($row['ServerUpdate']));
        $row['supdate'] = date("Y-m-d h:i", strtotime($row['MobileUpdate']));

        $data[] = $row;
    }
    print json_encode($data);
}    

updated for multiple records

Naveed Ramzan
  • 3,565
  • 3
  • 25
  • 30