Index: includes/session.inc
===================================================================
RCS file: /cvs/drupal/drupal/includes/session.inc,v
retrieving revision 1.40
diff -u -F^f -r1.40 session.inc
--- includes/session.inc	25 May 2007 15:04:41 -0000	1.40
+++ includes/session.inc	19 Jul 2007 16:31:59 -0000
@@ -75,8 +75,7 @@ function sess_write($key, $value) {
   else {
     db_query("UPDATE {sessions} SET uid = %d, cache = %d, hostname = '%s', session = '%s', timestamp = %d WHERE sid = '%s'", $user->uid, isset($user->cache) ? $user->cache : '', ip_address(), $value, time(), $key);
 
-    // TODO: this can be an expensive query. Perhaps only execute it every x minutes. Requires investigation into cache expiration.
-    if ($user->uid) {
+    if ($user->uid && sess_write_user($user->access)) {
       db_query("UPDATE {users} SET access = %d WHERE uid = %d", time(), $user->uid);
     }
   }
@@ -84,6 +83,24 @@ function sess_write($key, $value) {
   return TRUE;
 }
 
+
+/**
+ * Determine whether it is time to again write user's last access timestamp to users table. 
+ *
+ * @access integer
+ *   A timestamp indicating the user's last access to the site, according to the $user object.
+ * 
+ * @return boolean
+ **/
+function sess_write_user($access) {
+  // you may change this interval in your settings.php if desired.
+  $interval = variable_get('session_write_user_interval', 300);
+  $ago = time() - $access;
+  if ($interval == 0 || $ago > $interval) {
+    return TRUE;
+  }
+}
+
 /**
  * Called when an anonymous user becomes authenticated or vice-versa.
  */
