It's not entirely clear to me what you're asking, but I get the impression you're talking about a web server like Apache or nginx running on the same leased server as your MySql database in a hosting provider's data center. You're worried about the connection from the web server to the database, and the only difference between the two choices is what name you use in the connection string.
Given that scenario, it shouldn't matter which one you use. What can matter is how the two names are routed. Localhost
will always be routed through the loopback interface (barring some very strange configuration). mysql.example.com
can end up going out a physical interface before looping back in an upstream router our switch somewhere. You want to avoid that scenario: there's an extra hop there, the potential to use metered bandwidth, and also the fact the loopback interface often emulate 10Gbps connections (or better; at least 40Gbps is possible), while your physical interface might only be 1Gbps.
However, this isn't really a property of using the wrong name. It's a property of not having things configured so that this connection uses the loopback interface.
On the other hand, you may be talking about connecting from your local office to a MySql database hosted in a remote data center somewhere. In this case, then yes: it will be much slower, because traffic to the database must pass through your WAN connection and the internet, rather than just the local network (or your personal loopback interface, considering that you are comparing this to localhost).
However, this isn't what I'd call a "production" deployment. What I generally try to do is host all of the parts of an application on the same physical network. If it's a client server app, where the clients runs on user desktops inside a company network, then the database server should also live on that network (note that it doesn't have to be localhost: just not going out through a WAN conneciton). If it's a web app, then the database should be in the same data center (not the same server, but same network switch segment) as the web server, but those two servers can easily be a thousand miles away from the people viewing the web pages.