TTFB stands for Time To First Byte, this is the time the server takes to send first byte back to the browser after receiving a request. This metric seriously impacts page speed and can be a major reason behind higher bounce rates and lower SEO rankings for your website. It is recommended that we keep TTFB to be less than 600ms.
In a medium-to-large WordPress driven site tracking down the culprit for increased TTFB can be cumbersome and tedious task. Most of the time there is no clue whatsoever which could help determine the root cause for increased TTFB.
Thanks to large community and plugin marketplace that WordPress has there is lots of help available to the developers which they can use in their hunt for the cause behind increased TTFB. In this article we are going to take a look at one such tool freely available on WordPress Plugins Repository: Query Monitor
About Query Monitor
Query monitor is basically intended to debug SQL Queries, PHP errors, WP hooks, WP actions, HTTP API Calls. There are also some more advanced tools that query monitor provides. Query monitor once installed and activated shows all this information in a very nice and user friendly panel which categorizes all information in different tabs.
Below we discuss three tools provided in query monitor that can help you trace down the areas where the server spends most of the time.
Finding Slow SQL Queries
Query monitor’s SQL Queries panel provides an insight on all the database requests that were made during generation of a page. It enlists each query’s actual SQL Code that were executed, the time it took, component (core, theme or plugin) and the caller which if expanded shows the call stack too. You can sort this panel by time by clicking on the Time column. Hit it once to sort the table by descending time and check the SQL Queries one by one. There is a pretty good chance that there will be 2-3 queries that are taking larger time, Query Monitor will also highlight such queries in red. Find out such queries for your website and see if you could restructure the query in a different and optimized way, for example, you could consider adding Indexes on the columns that your queries are using in Where clause to help speed up database lookup. If you could optimize a few queries here you might get to reduce a few milliseconds from the TTFB.
The duplicate queries section in the left panel enlists all queries that were called more than once. These queries mostly taking very small fraction of time individually when taken collectively can be the real source of that very high TTFB for you WordPress website. The most common source of duplicate queries is fetching data from database inside loops. Check the source of the duplicate queries and restructure your code with an updated logic that keeps the db calls in-check. Solutions for such queries could be that you call the query once and store the result set in a global variable and in subsequent needs you can refer to this global variable. We have seen reduction of upto 2 seconds in TTFB using this technique.
HTTP API Calls
During page generation there might be cases where your theme or a plugin is making calls to some third party services or some data set using an HTTP Call. These HTTP Calls mostly are overlooked when searching for the solutions of reducing the TTFB. Find out the sources of these calls and re-think the logic for fetching data. May be you could cache the data locally and only fetch it from the source when it is outdated.
We had a scenario where a network of websites was sharing Header and Footer using stored HTML markup at one place and all websites would fetch data from these stored files over HTTP. The solution added was to cache these files locally and fetch renewed files at a gap of two-three days. This simple logic reduced the TTFB of the website by complete 1 and half seconds, which is a significant boost.
Please note that this is not a finishing line there is lot and lots of more that you can do when searching for solutions that could reduce TTFB of a slowed down website, above was just a reference
Let us know in the comments section if any of the above three tools provided in Query Monitor could help you speedup your website.