Only prepare a query once per name. Use the cached id afterwards

This makes it easy to lazily prepare queries, rather than trying to
prepare them once at the beginning.