Curious about query minimization (6 posts)

  1. Ivovic
    Posted 8 years ago #

    I'm just curious if anyone can think of a practical way to limit the number of additional queries generated by functions like the_permalink().

    It's interesting that you can pull 20 posts with 1 query, but you need 20 additional queries to pull the permalinks.

    God help you if you use the_permalink() twice for each post.

    I understand that caching the variable wouldn't be practical, but I wonder, is it a good idea to cache the permalink in the posts table? -- maybe there could be an option on the permalinks page to reset all the posts?

    I'm just thinking out loud here - can anyone think of a better way?

  2. haochi
    Posted 8 years ago #

    What the buck? Are you serious?
    What's the point of having the post_name column in wp_posts???

    What about get_permalink()?

  3. Ivovic
    Posted 8 years ago #

    that's a good point actually, given that post_name field is there, it shouldn't be pulling extra queries for each post.

    I completely forgot about that field, so when you mentioned it I thought there obviously must be a reason it's there.

    That caused me to investigate further...

    Interestingly... when I use the_permalink() on a page which pulls posts via query_posts, the_permalink() does NOT add additional queries for each post.... (though query_posts is quite heavy on queries itself, but not nearly as bad as doing one for each permalink)

    however, when I run my own query on the posts table and use setup_postdata() on the result, so I can use functions like the_title, etc, the_permalink() runs a single query each time it's called.

    ... even though $post->post_name is right there.

    I can only assume it does that, in order to check heirachy... because the permalink can involve information about categories and subcategories etc.

    In summary, it's not as bad as I thought, but it's still pretty bad.

    I really only noticed now, while working on what I'm doing at the moment, because for each test post I displayed, 5 extra queries showed up.

  4. Ivovic
    Posted 8 years ago #

    ok... additional information.

    Seems I was thinking along the right lines here....

    my development site used a permalink structure which was /%category%/%postname% - so naturally wordpress would have to query the heirachy of my categories in order to display the permalinks.

    I just tested this more thoroughly, and it seems that permalink structure is the root of all evil here.

    when I change it to date-based permalinks, they're much more predictable - and the information to construct them is readily available in the posts table, so the additional query isn't required.

    I also tested this with queries performed by wordpress internal functions like query_posts(), and it is also adversely affected by using /%category%/%postname% as the permalink structure.

    In summary: /%category%/%postname% adds LOTS of extra query pressure on your blog, even for doing simple stuff.

  5. haochi
    Posted 8 years ago #

    OT: I was thinking about that in the shower.

  6. Ivovic
    Posted 8 years ago #

    Well, its good to have an answer - I don't think I'm going to miss /%category%/%postname% very much. I'm not going to go back to date-based links, but I'll certainly avoid the category heirachy.

    At least I discovered this in development, before google crawled my site.

    I want to note again for those skim-reading, it has nothing to do with internal or external functions, and everything to do with the permalink structure.

Topic Closed

This topic has been closed to new replies.

About this Topic


No tags yet.