简体   繁体   中英

Query performance SELECT FROM WHERE id 0 is faster than id 1

I have this php code in my server

$social = array();
    $id = $db->idFromUsername($username);
    switch ($_POST['action']) {
        case 'GET':
            $query = 'SELECT user_one_id, user_two_id FROM social WHERE (user_one_id=? OR user_two_id=?) AND status=1';
            foreach ($db->queryExecute($query, $id, $id) as $relationship) {
                $friend = array();
                $friend['username'] = $db->usernameFromId($relationship['user_one_id'] == $id ? $relationship['user_two_id'] : $relationship['user_one_id']);
                $friend['userImage'] = getUserImage($friend['username']);
                $social[] = $friend;
                $status = 'true';
            }
            break;
        case 'PENDING':
            $query = 'SELECT user_one_id, user_two_id FROM social WHERE (user_one_id=? OR user_two_id=?) AND action_user_id!=? AND status=0';
            foreach ($db->queryExecute($query, $id, $id, $id) as $pending) {
                $person = array();
                $person['username'] = $db->usernameFromId($pending['user_one_id'] == $id ? $pending['user_two_id'] : $pending['user_one_id']);
                $person['userImage'] = getUserImage($person['username']);
                $social[] = $person;
                $status = 'true';
            }
            break;
.....
....
...
..
.
}

and the database table looks like this :

在此处输入图片说明

for some reason the "get" takes at least 4 times longer to execute than "pending" if someone could give me insight on this situation ill be vary grateful.

thank you.

While there are things that can be done to narrow this gap, like adding appropriate indexes, you really should not assume queries will have similar execution times just because they look similar. A single parameterized query can have very different times just based on the values used for the parameters.

For an extreme example imagine "WHERE id = 0" vs "WHERE id = 1" on a multi-million row table where the majority of rows are id=1, and almost no rows have an id=0. Without an index, the "lookup" time on both queries will probably be similar, as the lack of an index will require a full table scan; but the amount of data collected and transmitted for one will be much greater for one than the other. With an index, the lookup time will be greatly reduced, but even then you'd still have to transmit all the data.

Long story short, from the two queries you've shown, your best index is probably a composite one on (status, action_user_id) in that order; the status part will (guessing from the data) narrow the rows inspected by half, and the action_user_id part will narrow that further (dependent on uniqueness of the value being searched). Reversing the order would mean the first query would be unable to take advantage of the index at all.

An index including user_two_id and/or user_two_id would likely NOT be helpful for these queries as OR conditions pretty much throw out any potential index use for the conditions being OR ed together.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM