- User module improvements: added an 'access' column to the users-table to

keep track of the user's last access. In turn, this allowed me to:

  1. Optimize the "Who's online" block.  On drupal.org, the "Who's online"
     block requires 32 SQL queries.  With this patch, only 2 queries are
     left (eliminated 30 SQL queries), and one of the two remaining queries
     became appr. 20 times faster.

  2. Correct the "Last access" column in the user administration overview
     table.  The presented data was not accurate, which led to the column
     being removed.  You can now sort users by 'last access'.
4.7.x
Dries Buytaert 2005-05-07 11:39:54 +00:00
parent 898e2b6688
commit 39d0fe9a38
6 changed files with 32 additions and 22 deletions

View File

@ -692,7 +692,7 @@ CREATE TABLE users (
theme varchar(255) NOT NULL default '',
signature varchar(255) NOT NULL default '',
created int(11) NOT NULL default '0',
changed int(11) NOT NULL default '0',
access int(11) NOT NULL default '0',
login int(11) NOT NULL default '0',
status tinyint(4) NOT NULL default '0',
timezone varchar(8) default NULL,
@ -702,7 +702,7 @@ CREATE TABLE users (
data longtext,
PRIMARY KEY (uid),
UNIQUE KEY name (name),
KEY changed (changed)
KEY access (access)
) TYPE=MyISAM;
--

View File

@ -687,7 +687,7 @@ CREATE TABLE users (
theme varchar(255) NOT NULL default '',
signature varchar(255) NOT NULL default '',
created integer NOT NULL default '0',
changed integer NOT NULL default '0',
access integer NOT NULL default '0',
login integer NOT NULL default '0',
status smallint NOT NULL default '0',
timezone varchar(8) default NULL,

View File

@ -111,7 +111,8 @@ $sql_updates = array(
"2005-04-14" => "update_132",
"2005-04-24" => "update_133",
"2005-04-30" => "update_134",
"2005-05-06" => "update_135"
"2005-05-06" => "update_135",
"2005-05-08" => "update_136"
);
function update_32() {
@ -2447,6 +2448,14 @@ function update_135() {
return array();
}
function update_136() {
$ret = array();
$ret[] = update_sql("ALTER TABLE {users} CHANGE COLUMN changed access int(11) NOT NULL default '0'");
$ret[] = update_sql('UPDATE {users} SET access = login WHERE login > created');
$ret[] = update_sql('UPDATE {users} SET access = created WHERE access = 0');
return $ret;
}
function update_sql($sql) {
$edit = $_POST["edit"];
$result = db_query($sql);

View File

@ -46,6 +46,7 @@ function sess_write($key, $value) {
global $user;
db_query("UPDATE {sessions} SET uid = %d, cache = %d, hostname = '%s', session = '%s', timestamp = %d WHERE sid = '%s'", $user->uid, $user->cache, $_SERVER["REMOTE_ADDR"], $value, time(), $key);
db_query("UPDATE {users} SET access = %d WHERE uid = %d", time(), $user->uid);
return '';
}

View File

@ -126,7 +126,7 @@ function user_save($account, $array = array(), $category = 'account') {
$query .= "data = '%s', ";
$v[] = serialize($data);
db_query("UPDATE {users} SET $query changed = %d WHERE uid = %d", array_merge($v, array(time(), $account->uid)));
db_query("UPDATE {users} SET $query WHERE uid = %d", array_merge($v, array($account->uid)));
// Reload user roles if provided
if (is_array($array['roles'])) {
@ -147,7 +147,6 @@ function user_save($account, $array = array(), $category = 'account') {
}
else {
$array['created'] = time();
$array['changed'] = time();
$array['uid'] = db_next_id('{users}_uid');
// Note, we wait with saving the data column to prevent module-handled
@ -392,7 +391,7 @@ function user_fields() {
}
else {
// Make sure we return the default fields at least
$fields = array('uid', 'name', 'pass', 'mail', 'picture', 'mode', 'sort', 'threshold', 'theme', 'signature', 'created', 'changed', 'login', 'status', 'timezone', 'language', 'init', 'data');
$fields = array('uid', 'name', 'pass', 'mail', 'picture', 'mode', 'sort', 'threshold', 'theme', 'signature', 'created', 'access', 'login', 'status', 'timezone', 'language', 'init', 'data');
}
}
@ -549,7 +548,7 @@ function user_block($op = 'list', $delta = 0, $edit = array()) {
// Perform database queries to gather online user lists.
$guests = db_fetch_object(db_query('SELECT COUNT(sid) AS count FROM {sessions} WHERE timestamp >= %d AND uid = 0', time() - $time_period));
$users = db_query('SELECT DISTINCT(uid), MAX(timestamp) AS max_timestamp FROM {sessions} WHERE timestamp >= %d AND uid != 0 GROUP BY uid ORDER BY max_timestamp DESC', time() - $time_period );
$users = db_query('SELECT uid, name, access FROM {users} WHERE access >= %d AND uid != 0 ORDER BY access DESC', time() - $time_period);
$total_users = db_num_rows($users);
// Format the output with proper grammar.
@ -565,8 +564,8 @@ function user_block($op = 'list', $delta = 0, $edit = array()) {
if ($max_users) {
$items = array();
while ($max_users-- && $uid = db_fetch_object($users)) {
$items[] = format_name(user_load(array('uid' => $uid->uid)));
while ($max_users-- && $account = db_fetch_object($users)) {
$items[] = format_name($account);
}
if ($items) {
@ -1667,9 +1666,10 @@ function user_admin_account() {
array('data' => t('Status'), 'field' => 'u.status'),
array('data' => t('Roles')),
array('data' => t('Member for'), 'field' => 'u.created', 'sort' => 'desc'),
array('data' => t('Last access'), 'field' => 'u.access'),
t('Operations')
);
$sql = 'SELECT u.uid, u.name, u.status, u.created, u.login FROM {users} u WHERE uid != 0';
$sql = 'SELECT u.uid, u.name, u.status, u.created, u.access FROM {users} u WHERE uid != 0';
$sql .= tablesort_sql($header);
$result = pager_query($sql, 50);
@ -1683,13 +1683,13 @@ function user_admin_account() {
$roles[] = $role->name;
}
$rows[] = array(format_name($account), $status[$account->status], implode(', ', $roles), format_interval(time() - $account->created), l(t('edit'), "user/$account->uid/edit", array(), $destination));
$rows[] = array(format_name($account), $status[$account->status], implode(', ', $roles), format_interval(time() - $account->created), t('%time ago', array('%time' => format_interval(time() - $account->access))), l(t('edit'), "user/$account->uid/edit", array(), $destination));
}
$pager = theme('pager', NULL, 50, 0, tablesort_pager());
if (!empty($pager)) {
$rows[] = array(array('data' => $pager, 'colspan' => '5'));
$rows[] = array(array('data' => $pager, 'colspan' => '6'));
}
return theme('table', $header, $rows);
}

View File

@ -126,7 +126,7 @@ function user_save($account, $array = array(), $category = 'account') {
$query .= "data = '%s', ";
$v[] = serialize($data);
db_query("UPDATE {users} SET $query changed = %d WHERE uid = %d", array_merge($v, array(time(), $account->uid)));
db_query("UPDATE {users} SET $query WHERE uid = %d", array_merge($v, array($account->uid)));
// Reload user roles if provided
if (is_array($array['roles'])) {
@ -147,7 +147,6 @@ function user_save($account, $array = array(), $category = 'account') {
}
else {
$array['created'] = time();
$array['changed'] = time();
$array['uid'] = db_next_id('{users}_uid');
// Note, we wait with saving the data column to prevent module-handled
@ -392,7 +391,7 @@ function user_fields() {
}
else {
// Make sure we return the default fields at least
$fields = array('uid', 'name', 'pass', 'mail', 'picture', 'mode', 'sort', 'threshold', 'theme', 'signature', 'created', 'changed', 'login', 'status', 'timezone', 'language', 'init', 'data');
$fields = array('uid', 'name', 'pass', 'mail', 'picture', 'mode', 'sort', 'threshold', 'theme', 'signature', 'created', 'access', 'login', 'status', 'timezone', 'language', 'init', 'data');
}
}
@ -549,7 +548,7 @@ function user_block($op = 'list', $delta = 0, $edit = array()) {
// Perform database queries to gather online user lists.
$guests = db_fetch_object(db_query('SELECT COUNT(sid) AS count FROM {sessions} WHERE timestamp >= %d AND uid = 0', time() - $time_period));
$users = db_query('SELECT DISTINCT(uid), MAX(timestamp) AS max_timestamp FROM {sessions} WHERE timestamp >= %d AND uid != 0 GROUP BY uid ORDER BY max_timestamp DESC', time() - $time_period );
$users = db_query('SELECT uid, name, access FROM {users} WHERE access >= %d AND uid != 0 ORDER BY access DESC', time() - $time_period);
$total_users = db_num_rows($users);
// Format the output with proper grammar.
@ -565,8 +564,8 @@ function user_block($op = 'list', $delta = 0, $edit = array()) {
if ($max_users) {
$items = array();
while ($max_users-- && $uid = db_fetch_object($users)) {
$items[] = format_name(user_load(array('uid' => $uid->uid)));
while ($max_users-- && $account = db_fetch_object($users)) {
$items[] = format_name($account);
}
if ($items) {
@ -1667,9 +1666,10 @@ function user_admin_account() {
array('data' => t('Status'), 'field' => 'u.status'),
array('data' => t('Roles')),
array('data' => t('Member for'), 'field' => 'u.created', 'sort' => 'desc'),
array('data' => t('Last access'), 'field' => 'u.access'),
t('Operations')
);
$sql = 'SELECT u.uid, u.name, u.status, u.created, u.login FROM {users} u WHERE uid != 0';
$sql = 'SELECT u.uid, u.name, u.status, u.created, u.access FROM {users} u WHERE uid != 0';
$sql .= tablesort_sql($header);
$result = pager_query($sql, 50);
@ -1683,13 +1683,13 @@ function user_admin_account() {
$roles[] = $role->name;
}
$rows[] = array(format_name($account), $status[$account->status], implode(', ', $roles), format_interval(time() - $account->created), l(t('edit'), "user/$account->uid/edit", array(), $destination));
$rows[] = array(format_name($account), $status[$account->status], implode(', ', $roles), format_interval(time() - $account->created), t('%time ago', array('%time' => format_interval(time() - $account->access))), l(t('edit'), "user/$account->uid/edit", array(), $destination));
}
$pager = theme('pager', NULL, 50, 0, tablesort_pager());
if (!empty($pager)) {
$rows[] = array(array('data' => $pager, 'colspan' => '5'));
$rows[] = array(array('data' => $pager, 'colspan' => '6'));
}
return theme('table', $header, $rows);
}