Reduced DB contention in User::saveOptions().
authorAaron Schulz <aschulz@wikimedia.org>
Tue, 16 Apr 2013 19:43:04 +0000 (12:43 -0700)
committerGerrit Code Review <gerrit@wikimedia.org>
Wed, 17 Apr 2013 17:05:55 +0000 (17:05 +0000)
Change-Id: Ic91501cd6476dae54b54b85f2f06c25bd2577c9b

includes/User.php

index ed97deb..eda8bc9 100644 (file)
@@ -4428,8 +4428,18 @@ class User {
                }
 
                $dbw = wfGetDB( DB_MASTER );
-               $dbw->delete( 'user_properties', array( 'up_user' => $userId ), __METHOD__ );
-               $dbw->insert( 'user_properties', $insert_rows, __METHOD__ );
+               $hasRows = $dbw->selectField( 'user_properties', '1',
+                       array( 'up_user' => $userId ), __METHOD__ );
+
+               if ( $hasRows ) {
+                       // Only do this delete if there is something there. A very large portion of
+                       // calls to this function are for setting 'rememberpassword' for new accounts.
+                       // Doing this delete for new accounts with no rows in the table rougly causes
+                       // gap locks on [max user ID,+infinity) which causes high contention since many
+                       // updates will pile up on each other since they are for higher (newer) user IDs.
+                       $dbw->delete( 'user_properties', array( 'up_user' => $userId ), __METHOD__ );
+               }
+               $dbw->insert( 'user_properties', $insert_rows, __METHOD__, array( 'IGNORE' ) );
        }
 
        /**