Network comes before application. Ping is at the network layer ... nic's talking to one other ... you should always get faster response times in the ms range depending upon hops, but that's not the same as any php code talking the DB server. While it appears you are one hop away from the DB server ... have you done a traceroute from where moodle is hosted to the IP address of the DB server?
You've not mentioned what the DB server is ... but if MySQL, there is a config option to 'skip networking' ... mysql, by default, will use DNS on every query. thus moodle output (php on a web server making queries of the DB) might be slower. So suggest turning off networking in MySQL and in the moodle config use the IP address of the DB host, not any FQDN for the DB host.
Also, is the DB box a dedicated DB box .... just you? OR is it hosting DB's for other clients? Makes a diff also.
Uhhh ... why are you hosting the DB on a server outside your network?
'spirit of sharing', Ken