From b78f4cbd31b5cfea3cddfea0b39891b785a34156 Mon Sep 17 00:00:00 2001 From: martinlanghoff Date: Wed, 19 Sep 2007 07:20:52 +0000 Subject: [PATCH] mod/chat: reduce number of queries in cron. Delete stale msgs in one DB query rather than in 1-per-chat-instance. (From 600 DB queries to 1 on a site with 600 chat instances.) Also - a walkthrough of chat_update_chat_times(), which seems buggy. --- mod/chat/lib.php | 20 +++++++++++--------- 1 file changed, 11 insertions(+), 9 deletions(-) diff --git a/mod/chat/lib.php b/mod/chat/lib.php index 265d3a710b..779e59dc3b 100644 --- a/mod/chat/lib.php +++ b/mod/chat/lib.php @@ -233,14 +233,14 @@ function chat_cron () { chat_delete_old_users(); /// Delete old messages - if ($chats = get_records("chat")) { - foreach ($chats as $chat) { - if ($chat->keepdays) { - $timeold = time() - ($chat->keepdays * 24 * 3600); - delete_records_select("chat_messages", "chatid = '$chat->id' AND timestamp < '$timeold'"); - } - } - } + $sql = "SELECT m.id + FROM {$CFG->prefix}chat_messages m + JOIN {$CFG->prefix}chat c + ON m.chatid = c.id + WHERE c.keepdays != 0 + AND m.timestamp < ( ".time()." - c.keepdays * 24 * 3600)"; + + delete_records_select("chat_messages", "id IN ($sql)"); return true; } @@ -506,7 +506,9 @@ function chat_update_chat_times($chatid=0) { update_record("chat", $chat); $event = NULL; // Update calendar too - if ($event->id = get_field('event', 'id', 'modulename', 'chat', 'instance', $chat->id)) { + $cond = "modulename='chat' AND instance = {$chat->id} + AND timestart != {$chat->chattime}"; + if ($event->id = get_field_select('event', 'id', $cond)) { $event->timestart = $chat->chattime; update_event($event); } -- 2.39.5