From 0397b462174887bda3fea4bcebf9a26f6b591a3e Mon Sep 17 00:00:00 2001 From: Joas Schilling Date: Tue, 27 Mar 2012 17:29:03 +0200 Subject: [PATCH] [ticket/10605] Split query to be able to use indexes PHPBB3-10605 --- phpBB/includes/functions_privmsgs.php | 34 ++++++++++++++++++++++++--- 1 file changed, 31 insertions(+), 3 deletions(-) diff --git a/phpBB/includes/functions_privmsgs.php b/phpBB/includes/functions_privmsgs.php index 576da4d439..dd81e8f92d 100644 --- a/phpBB/includes/functions_privmsgs.php +++ b/phpBB/includes/functions_privmsgs.php @@ -1098,11 +1098,11 @@ function phpbb_delete_user_pms($user_id) } // Get PM Information for later deleting + // The two queries where split, so we can use our indexes + // Part 1: get PMs the user received $sql = 'SELECT msg_id, author_id, folder_id, pm_unread, pm_new FROM ' . PRIVMSGS_TO_TABLE . ' - WHERE user_id = ' . $user_id . ' - OR (author_id = ' . $user_id . ' - AND folder_id = ' . PRIVMSGS_NO_BOX . ')'; + WHERE user_id = ' . $user_id; $result = $db->sql_query($sql); $undelivered_msg = $undelivered_user = $delete_ids = array(); @@ -1127,6 +1127,34 @@ function phpbb_delete_user_pms($user_id) } $db->sql_freeresult($result); + // Part 2: get PMs the user sent + $sql = 'SELECT msg_id, author_id, folder_id, pm_unread, pm_new + FROM ' . PRIVMSGS_TO_TABLE . ' + WHERE author_id = ' . $user_id . ' + AND folder_id = ' . PRIVMSGS_NO_BOX; + $result = $db->sql_query($sql); + + while ($row = $db->sql_fetchrow($result)) + { + if ($row['author_id'] == $user_id && $row['folder_id'] == PRIVMSGS_NO_BOX) + { + // Undelivered messages + $undelivered_msg[] = $row['msg_id']; + + if (isset($undelivered_user[$row['user_id']])) + { + ++$undelivered_user[$row['user_id']]; + } + else + { + $undelivered_user[$row['user_id']] = 1; + } + } + + $delete_ids[(int) $row['msg_id']] = (int) $row['msg_id']; + } + $db->sql_freeresult($result); + if (empty($delete_ids)) { return false;