I am in Spain now. I started my Trip on March 10th in Madrid. The only place I purchased tickets online the day before, was for the Prado and I am VERY glad I did! Huge lines even at 10:00 in the morning.
Yes, regarding the Alhambra. Take the time to research the website so you have some small idea of the place. You can tour the grounds as long as you want, just be sure you are in line EXACTLY at your appointment time. I have heard they are not forgiving and won’t let you in if you are late. I have tickets in two weeks.
Other than those two places I have not had to purchase tickets in advance. I have been to : Madrid, Toledo, Cordoba, Seville and now Torremolinos (which is near Malaga). Barcelona is last.
I LOVE Spain! It is wonderful. One word of advice: if you don’t speak Spanish, learn some! People here don’t speak very much English. I am glad I speak some Spanish and have been learning more every day. VERY few people speak English!