It is brought in Maseches Brachos (8s) that Rabi Yehoshua ben Levi said to his children that they should come to shul early and stay there until a late hour and that this practice would be beneficial for them to live a long life. Based on this, the Shela Hakadosh explained the request of Dovid Hamelech (Tehillim 23:6): “Ach tov v’chessed yirdefuni kol yemei chayai veshavti beveis Hashem l’orech yamim.” Dovid said: Being that I am strict to go to the “beis Hashem” morning and evening, I am sure that I will merit as a result “l’orech yamim.” Now, all that is left is for me to ask that “ach tov v’chessed yirdefuni kol yemei chayai” – that these days that I merit should be good days filled with chessed and that I should not suffer difficulty and distress. (Shelah Maseches Tamid 26)