Opened 8 years ago

Closed 6 years ago

#1659 closed defect (bug) (fixed)

(wp_)list_authors queries

Reported by: usayd Owned by: ryan
Priority: normal Milestone: 2.2
Component: Optimization Version: 1.5.2
Severity: normal Keywords: has-patch commit
Cc:

Description

These two template tags:

wp_list_authors(<?php wp_list_authors(); ?>) and

list_authors (<?php list_authors(); ?>) (no matter what functions within them are called)

add an extra query to the database for each member registered on the blog - weather they are authors or not. On mine it adds 24 extra queries as shown below. This action should only be taken out in one database query to reduce the time for the page to load.

 [24] => Array
        (
            [0] => SELECT COUNT(*) FROM wp_posts WHERE post_author = '17' AND post_status = 'publish'
            [1] => 0.0088999271392822
        )

    [25] => Array
        (
            [0] => SELECT COUNT(*) FROM wp_posts WHERE post_author = '15' AND post_status = 'publish'
            [1] => 0.008976936340332
        )

    [26] => Array
        (
            [0] => SELECT COUNT(*) FROM wp_posts WHERE post_author = '4' AND post_status = 'publish'
            [1] => 0.009009838104248
        )

    [27] => Array
        (
            [0] => SELECT COUNT(*) FROM wp_posts WHERE post_author = '19' AND post_status = 'publish'
            [1] => 0.0088570117950439
        )

    [28] => Array
        (
            [0] => SELECT COUNT(*) FROM wp_posts WHERE post_author = '2' AND post_status = 'publish'
            [1] => 0.0090000629425049
        )

    [29] => Array
        (
            [0] => SELECT COUNT(*) FROM wp_posts WHERE post_author = '9' AND post_status = 'publish'
            [1] => 0.0089190006256104
        )

    [30] => Array
        (
            [0] => SELECT COUNT(*) FROM wp_posts WHERE post_author = '8' AND post_status = 'publish'
            [1] => 0.0088551044464111
        )

    [31] => Array
        (
            [0] => SELECT COUNT(*) FROM wp_posts WHERE post_author = '13' AND post_status = 'publish'
            [1] => 0.011569976806641
        )

    [32] => Array
        (
            [0] => SELECT COUNT(*) FROM wp_posts WHERE post_author = '12' AND post_status = 'publish'
            [1] => 0.010303974151611
        )

    [33] => Array
        (
            [0] => SELECT COUNT(*) FROM wp_posts WHERE post_author = '14' AND post_status = 'publish'
            [1] => 0.0098090171813965
        )

    [34] => Array
        (
            [0] => SELECT COUNT(*) FROM wp_posts WHERE post_author = '11' AND post_status = 'publish'
            [1] => 0.02720308303833
        )

    [35] => Array
        (
            [0] => SELECT COUNT(*) FROM wp_posts WHERE post_author = '7' AND post_status = 'publish'
            [1] => 0.0099828243255615
        )

    [36] => Array
        (
            [0] => SELECT COUNT(*) FROM wp_posts WHERE post_author = '18' AND post_status = 'publish'
            [1] => 0.011690855026245
        )

    [37] => Array
        (
            [0] => SELECT COUNT(*) FROM wp_posts WHERE post_author = '20' AND post_status = 'publish'
            [1] => 0.0088968276977539
        )

    [38] => Array
        (
            [0] => SELECT COUNT(*) FROM wp_posts WHERE post_author = '6' AND post_status = 'publish'
            [1] => 0.0089771747589111
        )

    [39] => Array
        (
            [0] => SELECT COUNT(*) FROM wp_posts WHERE post_author = '10' AND post_status = 'publish'
            [1] => 0.0090258121490479
        )

    [40] => Array
        (
            [0] => SELECT COUNT(*) FROM wp_posts WHERE post_author = '5' AND post_status = 'publish'
            [1] => 0.0089180469512939
        )

    [41] => Array
        (
            [0] => SELECT COUNT(*) FROM wp_posts WHERE post_author = '16' AND post_status = 'publish'
            [1] => 0.008929967880249
        )

Attachments (3)

bug1659.diff (1.2 KB) - added by graeme 8 years ago.
1659.diff (1.5 KB) - added by rob1n 6 years ago.
Patch refreshed and refined.
1659.2.diff (1.4 KB) - added by rob1n 6 years ago.
Lose the post_date_gmt test.

Download all attachments as: .zip

Change History (17)

  • Owner changed from anonymous to markjaquith
  • Status changed from new to assigned

Basically, what it does is this:

  1. fetch all users
  2. go through each, and count their posts

It should be possible to split the logic early on, and do a different query based on your $hide_empty setting. When $hide_empty is off, no prob, do the existing query and don't bother to filter later on. If $hide_empty is on, you need to do one big fancy query that joins the authors and the post tables, and grabs the count of the $wpdb->posts.ID column... might be a tad complicated, but I don't know of a better way.

  • Keywords bg|2nd-opinion added; template tags removed
  • Owner changed from markjaquith to graeme
  • Status changed from assigned to new

Believe I've licked this. As always, there's likely a better way...

get_userdata() is still being called once per user, but (if the bug report is right) all that is being taken from the cache. Ideally the number of posts an author has (ah) authored should be part of their userdata, maybe? Anyway...

graeme8 years ago

  • Keywords bg|has-patch added
  • Owner changed from graeme to rob1n
  • Status changed from new to assigned
  • Milestone set to 2.3
  • Milestone changed from 2.3 to 2.2
  • Keywords bg|2nd-opinion bg|has-patch removed

rob1n6 years ago

Patch refreshed and refined.

  • Keywords has-patch added
  • Status changed from assigned to new
  • Status changed from new to assigned
  • Keywords commit added

Lose the test for post_date_gmt. We don't need that anymore.

rob1n6 years ago

Lose the post_date_gmt test.

  • Owner changed from rob1n to ryan
  • Status changed from assigned to new

Reassigning to ryan for commit.

Let's try this one out. +1

  • Resolution set to fixed
  • Status changed from new to closed

(In [5135]) Speed (wp_)list_authors by consolidating some queries. Props graeme. fixes #1659

Note: See TracTickets for help on using tickets.