[Drupal] How to avoid the overhead of executing a database query many times in a page request
https://www.zyxware.com/sites/default/files/styles/user_image/public/default_images/index.png?itok=2YmREnrP
BY sarath.mohan
5 years ago
Drupal-Performance
0 comments comment

Multiple database access in a site can create overheads. There is a chance of this being adversely affecting the site performance if the size of the website is considerably large.

Consider a function that returns the details of a new product. And if we call this function in different regions of a page, the query will be executed for each function call. ie, multiple database requests are made for the same result.

We can reduce this overhead by using the static variable. Static variables can be used for caching data for the current request. For the above case, create a static variable in the function to store the database result. In the function definition which follows,check whether the static variable is set or not, If the variable is set, then the result can be immediately returned. So there is only one database access for all the calls in that page.

The following function shows the usage of static variable.


In the above function the static variable $product_details is set when the function is called. Then for the all other calls in the same request will give the data from the static variable, without a database access.

So using the static variable to keep the database result can be reused in a single request, which reduces the overhead.


RELATED ARTICLE

/themes/custom/zyxpro_light/images/placeholder.png
close

on 06th December 2011 / by webmaster
Running Drupal on a shared hosting environment could become a challenge when the site starts getting a lot of visitors. So f you run your Drupal installation on one of the shared hosting providers like GoDaddy, Hostgator, Dreamhost etc and your site is reasonably popular then it is likely that you would have run into performance issues if the site is not configured correctly. If you configure your Drupal site correctly you should be able to stick with shared hosting for a longer period of time before moving on to dedicated hosting. Drupal Drupal Development Drupal Performance Leave a reply Your email address will not be published. Required fields are marked *
/themes/custom/zyxpro_light/images/placeholder.png
close

on 27th April 2012 / by Anoop John
You have your new Drupal site all ready for take off. You just want to make sure you have everything well in place. Here is a little checklist for you to make sure you have the essentials ready. In this part of the checklist series, we look at the important performance related essentials for a Drupal website, so that your site is ready to receive the multitudes it oughta get. These will help you ensure that your site will sail smooth in times of high traffic. Drupal Drupal Performance Drupal Checklist Leave a reply Your email address will not be published. Required fields are marked *
Leave a reply
Your email address will not be published. Required fields are marked *

Filtered HTML

  • Web page addresses and email addresses turn into links automatically.
  • Allowed HTML tags: <a href hreflang> <em> <strong> <cite> <blockquote cite> <code> <ul type> <ol start type='1 A I'> <li> <dl> <dt> <dd> <h2 id='jump-*'> <h3 id> <h4 id> <h5 id> <h6 id>
  • Lines and paragraphs break automatically.
The content of this field is kept private and will not be shown publicly.
CAPTCHA This question is for testing whether or not you are a human visitor and to prevent automated spam submissions.