What is the idea behind the touch move rule? Where did this rule originate? Why does it exist?

I just recently learned about it, and in my casual coffeeshop chess games, it has never been mentioned or enforced.
If you're allowed to touch a piece, you can also pick it up and land it on another square, look how the new position would look like, and return it to it's original square if you don't like the new position. Repeat this over and over again, and it becomes very annoying for your opponent.

This explains why the rule might exist, but not since when. I do not know the answer to that question.