0

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.

  • 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 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 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. – hayanawama Oct 27 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 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 at 14:23

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.