1

I've just found out that main page of custom theme im developing does 150 db queries on every load (what takes even 2.5 seconds).

Many of them come from functions.php, and are caused by for example add_image_size() or register_nav_menus() functions. However, when I deleted these functions website still had functioned properly.

From what I understand, these functions just write some informations to database and nothing bad would happen if I seriously deleted them for ever (correct me if I'm wrong) because these informations are already there. For example, when I deleted register_nav_menus() (where I'm registering several menus), number of queries went down to ~120 and website still functioned properly.

Of course deleting these functions doesn't seem like the best idea (what if I want to start using my theme with clean database). So is there any way to check if for example these menus or image sizes have already been declared and exist in database? And if so, do not execute that register_nav_menus() or add_image_size() functions? Or wordpress is already doing it internally? Would this check cause additional database hit?

This is the first theme I'm developing and I would be grateful for any tips on how to lessen db queries in this case.

7
  • 1
    What exactly are the database calls? The main one I see is wp_options fetches for values that don't exist: WordPress will load all the options it thinks it needs in one go (everything flagged autoload) but there's no mechanism to say 'autoload an empty value for this option' so unset options get their own database query. So the more plugins you have and the more options they support that you haven't set the more database options fetches you'll make
    – Rup
    Oct 27, 2020 at 13:52
  • (I do occasionally plan to do something about it, e.g. keep a list of known empty option values in an autoloaded transient, and keep that list up-to-date as options are set and deleted, but I've never got around to it.)
    – Rup
    Oct 27, 2020 at 13:53
  • I'm talking about the number which I get with get_num_queries(). This includes everything I think, from creating terms, menus etc. to getting posts data.
    – user179669
    Oct 27, 2020 at 14:04
  • OK, well I think you'll have to look at examples and solve them individually. We can't just say "Yes! delete all your code" without knowing exactly what it's doing. There's no reason that your page should be creating terms on a page load, for example - something's gone wrong there. The usual approach might help of trying a default theme, try turning off plugins to see if you can work out where most of the queries come from.
    – Rup
    Oct 27, 2020 at 14:08
  • 1
    Oh, right, sorry, I understand now: these are updates you're writing yourself in your theme? No, you probably just need to do them once when the theme is activated.
    – Rup
    Oct 27, 2020 at 14:23

1 Answer 1

0

Options are persistent. You can set them when you activate your theme and then just use them. There's no need to change them when rendering any particular page unless your logic actually changes them.

And doing so may cause confusion. For example, lots of changes to image and thumbnail default sizes will make your Media subsystem contain lots of generated thumbnails with sizes you don't use any more.

Finally, consider using a persistent object cache. It significantly reduces database traffic for options and transients. Read all about it. https://developer.wordpress.org/reference/classes/wp_object_cache/#persistent-caching

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.