Optimal Way to Revise Chatbox to Increase Speed?

I have a chatbox via the meta refresh tag which is included on every page. Thing is, it’s contributing to almost half of the loading time for loading a new page. Anyway to decrease this time by changing some codes (or the chat code itself)? I tried using Ajax but it still processed rather slowly.

chat history:
[php]<?php

include(‘config.php’);

$query = mysql_query(“SELECT * FROM chat ORDER BY id DESC LIMIT 20”);
while($row = mysql_fetch_assoc($query))
{
if($row[‘userID’] == 0) { $player = ‘Guest’; }
else
{
$player_q = mysql_query(“SELECT * FROM users WHERE id=’” . $row[‘userID’] . “’”);
$player_r = mysql_fetch_assoc($player_q);
$player = $player_r[‘callname’];
}echo ‘

’ . date(‘M d, h:i:s’, $row[‘time’]) . ‘

’ . $player . ': ’ . $row[‘message’] . ‘
’;
}

?>

[/php]

chat.php
[php]<?php
include(‘config.php’);
?>

body{background-color:transparent} <?php if(isset($_POST['form'])) { $message = mysql_real_escape_string($_POST['message']); $time = time(); if($_SESSION['id'] != "") { $userID = $_SESSION['id']; } else { $userID = 0; } mysql_query("INSERT INTO `chat` (`userID`, `time`, `message`) VALUES ('$userID', '$time', '$message')") or die(mysql_error()); } mysql_real_escape_string($message); echo '
Sponsor our Newsletter | Privacy Policy | Terms of Service