1

While working on some old custom code I noticed that variable_set() was being used in a manner that always made new key pair values and never made a call to variable_del. These values would be read once under a certain condition and never read again.

Are persistant variables something that get cleared on clear cache or are they always building up?

if they are building up does this affect the size or speed of page load?

example:

variable_set('key_name_' . $increment, $value);

2 Answers 2

2

In Drupal 6 and 7, variables created with variable_set() are stored in the {variable} database table. They are not automatically cleared when the cache is cleared. They are only removed when variable_del() is called.

Whenever a page is loaded, all variables in the {variable} table are loaded into the global $conf array. So yes - if a site is automatically creating new variables at a constant rate without cleaning them up, it could in theory cause performance degradation.

You can see in the variable_get() function that it is simply loading the global $conf array and looking for the requested variable within that.

It sounds like the custom code should have been using a dedicated database table for the data it was storing, because then it would only load that information when it needed to. Variables are loaded on every pageload.

3
  • alright since the value is hardly used it should be safe to call variable_del(), now for all values already stored would it be safe to go in the {variable} table and simply delete everything?
    – blu
    Dec 2, 2016 at 15:02
  • The {variable} table is used for a lot of different configuration. It is not a cache table, so if you delete things in it they will not be rebuilt. So DO NOT delete EVERYTHING in the table - that will break your site in very many ways. But if you mean "delete all the variables created by the custom code you mentioned in your question" - then as long as you are sure the code does not need them: yes it should be safe.
    – m.stenta
    Dec 4, 2016 at 3:13
  • Clearing out the variables dropped our PHP Peak usage from 250MB down to about 60-70MB
    – blu
    Dec 16, 2016 at 17:07
0

Yes.

If you find a module doing this in Drupal 7, consider checking that the value of the variable has changed before saving it, converting to use the cache system if that’s feasible, saving the value in a dedicated table if it’s not requested frequently, or like drupal_http_request() just logging the change via watchdog().

From High Performance Drupal.

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.