Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I reduce the waiting (ttfb) time

I have a query which involves getting a list of user from a table in sorted order based on at what time it was created. I got the following timing diagram from the chrome developer tools.

timing from the chrome

You can see that TTFB (time to first byte) is too high.
I am not sure whether it is because of the SQL sort. If that is the reason then how can I reduce this time?
Or is it because of the TTFB. I saw blogs which says that TTFB should be less (< 1sec). But for me it shows >1 sec. Is it because of my query or something else?
I am not sure how can I reduce this time.
I am using angular. Should I use angular to sort the table instead of SQL sort? (many posts say that shouldn't be the issue)
What I want to know is how can I reduce TTFB. Guys! I am actually new to this. It is the task given to me by my team members. I am not sure how can I reduce TTFB time. I saw many posts, but not able to understand properly. What is TTFB. Is it the time taken by the server?

like image 869
govindpatel Avatar asked Feb 18 '15 10:02

govindpatel


People also ask

Why is TTFB waiting so long?

The most common culprit for high TTFB is dynamic content generation. This refers to the time it takes PHP and database queries to generate your webpages. The primary contributing factors to slow dynamic content generation are large files, excess or slow database queries, and autoload data.

What is a good TTFB speed?

Generally, anything under 100 ms is great and good TTFB. Google PageSpeed Insights recommends under 200 ms for server response time. If you are in the 300-500 ms range, this is pretty standard.

What affects TTFB?

What affects time to first byte? TTFB is impacted by three key actions: 1) sending a request from a client machine to the server, 2) processing that request on the server and generating a response, and 3) sending the response from the server to the client.


Video Answer


5 Answers

The TTFB is not the time to first byte of the body of the response (i.e., the useful data, such as: json, xml, etc.), but rather the time to first byte of the response received from the server. This byte is the start of the response headers.

For example, if the server sends the headers before doing the hard work (like heavy SQL), you will get a very low TTFB, but it isn't "true".

In your case, TTFB represents the time you spend processing data on the server.

To reduce the TTFB, you need to do the server-side work faster.

like image 145
Daniel T. Sobrosa Avatar answered Oct 03 '22 12:10

Daniel T. Sobrosa


I have met the same problem. My project is running on the local server. I checked my php code.

$db = mysqli_connect('localhost', 'root', 'root', 'smart');

I use localhost to connect to my local database. That maybe the cause of the problem which you're describing. You can modify your HOSTS file. Add the line

127.0.0.1 localhost.

like image 39
CH Chow Avatar answered Oct 03 '22 13:10

CH Chow


TTFB is something that happens behind the scenes. Your browser knows nothing about what happens behind the scenes.

You need to look into what queries are being run and how the website connects to the server.

This article might help understand TTFB, but otherwise you need to dig deeper into your application.

like image 20
AncientSwordRage Avatar answered Oct 03 '22 11:10

AncientSwordRage


If you are using PHP, try using <?php flush(); ?> after </head> and before </body> or whatever section you want to output quickly (like the header or content). It will output the actually code without waiting for php to end. Don't use this function all the time, or the speed increase won't be noticable.

More info

like image 20
Matías Pizarro Avatar answered Oct 03 '22 12:10

Matías Pizarro


I would suggest you read this article and focus more on how to optimize the overall response to the user request (either a page, a search result etc.)

A good argument for this is the example they give about using gzip to compress the page. Even though ttfb is faster when you do not compress, the overall experience of the user is worst because it takes longer to download content that is not zipped.

like image 33
Mike Avatar answered Oct 03 '22 12:10

Mike