Make WordPress Core

Opened 6 years ago

Last modified 5 years ago

#44921 new defect (bug)

User nicename discovery is slow to return

Reported by: spacedmonkey's profile spacedmonkey Owned by:
Milestone: Awaiting Review Priority: normal
Severity: normal Version: 2.0
Component: Users Keywords: has-patch needs-testing
Focuses: performance Cc:

Description

In the function wp_insert_user it checks to is if the user nicename is in use by another user before saving. This is because, a nicename should is required to be unique. Unlike usernames, however, that rejects already in use usernames, if first checks to see if the nicename is in uses, then runs a while loop to try and find the next available nicename with a suffix. So nicename jonny becomes jonny_2 if jonny is already in use.

The behaviour is fine, however it uses a while loop to find the next available nicename, which can result in an extremely high number of queries. Take the following use case.

You have a script that generates 10000 test users for a site. The call to wp_insert_user, passed 'user_nicename' => 'test_user'. Within the function call, it will run the while loop and find next available nicename. By the 9999th call, it would have to loop around 9999 to find the next slot, making the return of wp_insert_user extremely slow.

For testing scripts and sites with lots of registered users, this could make wp_insert_user almost unusable.

Attachments (3)

Screenshot from 2018-09-08 12-46-46.png (51.6 KB) - added by spacedmonkey 6 years ago.
An example of where this issue is happening in the wild.
44921.diff (1.5 KB) - added by spacedmonkey 5 years ago.
44921.2.diff (1.9 KB) - added by spacedmonkey 5 years ago.

Download all attachments as: .zip

Change History (6)

@spacedmonkey
6 years ago

An example of where this issue is happening in the wild.

#1 @spacedmonkey
6 years ago

I would recommend, using get_user_by('slug', $usernice); instead of a raw SQL query, as it would at go through caches.

@spacedmonkey
5 years ago

#2 @spacedmonkey
5 years ago

  • Keywords has-patch added

I did a first pass at this 44921.diff.

This doesn't add caching, but just replaces the raw calls to database with function calls to get_users. Now that this is going to core, if give us more options to cache upstream.

@spacedmonkey
5 years ago

#3 @spacedmonkey
5 years ago

  • Keywords needs-testing added

The patch 44921.2.diff adds a new helper function nicename_exists which is similar to email_exists.

This is a much cleaner way of calling this, my only issue with it is the kind of odd, function signature of nicename_exists

Note: See TracTickets for help on using tickets.