155

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?

Web_Designer
  • 72,308
  • 93
  • 206
  • 262
govindpatel
  • 2,539
  • 2
  • 19
  • 20
  • 9
    Your question shows a lack of understanding what is going on here. This is time waiting for the server, so whether you use AngularJS or some other framework is irrelevant. If you want to improve the server side code, you actually have to show us the code. – dirkk Feb 18 '15 at 10:31
  • @govindpatel, if my answer below have helped you or you think it answers the question, please mark it as the correct answer ;) – Daniel T. Sobrosa Mar 23 '18 at 14:07
  • For simple table sorting (assuming the table data has already been fetched and you're just resorting the data by a different property) it will be much faster to do it client-side than to send out another request for sorted data. – Richik SC Jun 03 '19 at 16:31

5 Answers5

153

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.

Sam
  • 7,252
  • 16
  • 46
  • 65
Daniel T. Sobrosa
  • 1,664
  • 1
  • 11
  • 10
  • 2
    To further diagnose the timings happening during TTFB, you can use server-side timing methods (e.g. setup timers, or debug log) to debug the time spent on each logic. – Raptor Aug 06 '15 at 07:36
  • 1
    Have a look to this article, it explains the problem in details and gives advice for possible solutions: http://www.websiteoptimization.com/speed/tweak/time-to-first-byte/ – LucaM Sep 09 '15 at 07:56
  • 1
    Beware of treating TTFB as the most important point: https://blog.cloudflare.com/ttfb-time-to-first-byte-considered-meaningles/ – Owen Blacker Oct 07 '15 at 11:18
  • If this answer help you @govindpatel, mark it as the right response, please ;) – Daniel T. Sobrosa Oct 18 '16 at 15:55
20

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.

BuZZ-dEE
  • 6,075
  • 12
  • 66
  • 96
CH Chow
  • 417
  • 4
  • 6
17

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.

AncientSwordRage
  • 7,086
  • 19
  • 90
  • 173
5

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

AncientSwordRage
  • 7,086
  • 19
  • 90
  • 173
4

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.

Raptor
  • 53,206
  • 45
  • 230
  • 366
Mike
  • 3,017
  • 1
  • 34
  • 47
  • Be sure to check out the comments section of that article. Many folks give compelling reasons to be concerned with TTFB. – Zack Macomber Sep 13 '18 at 14:10