0

Out of curiosity, which of these segments of code would have the faster performance time when implemented on a mass scale?

Let's say we have a table members and we want to fetch their photos.

Method 1

$members = $db->query('SELECT * FROM members ORDER BY id ASC');
foreach($members as $m) 
    $memberIDs[] = $m['id'];

$photos = $db->query('SELECT * FROM photos WHERE member IN'.join(',', $memberIDs).' ORDER BY id ASC');
foreach($photos as $p) {
    // multi_arr_search(search_val, search_column, search_array) returns parent key in multi dimensional array
    $memberArrayKey = multi_arr_search($p['member'], 'id', $members);
    $members[$memberArrayKey]['photos'][] = $p;
}

OR

Method 2

$members = $db->query('SELECT * FROM members ORDER BY id ASC');
foreach($members as $k=>$m) 
    $members[$k]['photos'] = $db->query('SELECT * FROM photos WHERE member='.$m['id'].' ORDER BY id ASC');

Method 1 would result in fewer queries being ran, but requires more PHP work.

1
  • 1
    Method #3 - Use a JOIN in a single query; but definitely not #2 because there's an overhead per query, and the more queries you execute the slower it will all be Commented Jun 1, 2016 at 23:19

3 Answers 3

2

neither. You are using 15 years old deprecated methods. If you want to go fast, you need PDO prepared statements. @alberto is right. I am pretty sure, you can have one and only sql to perform this but here is a way you might want to consider in case off:

$showError = true;

define("SQLHOST", "127.0.0.1");
define("SQLUSER", "login");
define("SQLPASS", "password");
define("SQLSGBD", "database");

$conn = new PDO('mysql:host=' . SQLHOST . ';dbname=' . SQLSGBD . ';charset=UTF8', SQLUSER, SQLPASS);
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$sql1 = 'SELECT * FROM members ORDER BY id ASC';
$stmt1 = $conn->prepare($sql1);
$sql2 = 'SELECT * FROM photos WHERE member IN ? ORDER BY id ASC';
$stmt2 = $conn->prepare($sql2);

try {
    $stmt1->execute();
    $members = $stmt1->fetchAll(PDO::FETCH_ASSOC);
} catch (PDOException $e) {
    if ($showError === true) {
        var_dump("error query 1:" . __LINE__ . "-------" . __FUNCTION__ . "-------" . $e->getMessage());
        exit;
    }
}

if (count($members) !== 0) {
    $memberIDs = array();
    foreach ($members as $m) {
        $memberIDs[] = $m['id'];
    }
    $memberlist = join(',', $memberIDs);

    foreach ($members as $memberArrayKey => $result1) {
        $stmt2->bindParam(1, $memberlist, PDO::PARAM_STR);
        try {
            $stmt2->execute();
            $photos = $stmt2->fetchAll(PDO::FETCH_ASSOC);
        } catch (PDOException $e) {
            if ($showError === true) {
                var_dump("error query 2:" . __LINE__ . "-------" . __FUNCTION__ . "-------" . $e->getMessage());
                exit;
            }
        }
        if (count($photos) !== 0) {
            $memberArrayKey = multi_arr_search($p['member'], 'id', $members);
            $members[$memberArrayKey]['photos'][] = $p;
        }
    }
}
Sign up to request clarification or add additional context in comments.

Comments

1

Well, hard truth: none is actually scalable. If you're working with large sets of data, PHP will need quite a bit of memory to perform all the operations to link users and photos and will do it in a much more inefficient way than the DB can.

You should have the tool you have that is best at doing these kind of things, which is your DB: the DB should join the users & photos and get you that set.. anything you do differently will mean reduced performance and scalability.

Comments

1

1- It depends on many factors how are you working with MySQL, if you connect and disconnect connection with the DB at each call. 2- MySQL Server is in the same PHP.

Ideally, connect 1 time mysql to retrieve information with a single Query. And in your case, you can solucuonar with a JOIN. Which it is best to disconnect as quickly as possible and continue with MySQL PHP.

Another recommendation is to not use " * " in the SELECT. Only puts the fields that you go to work. This eat fewer resources because only you use is requested. It is optimal in Memory, CPU, Data via LAN

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.