Raold
Raold

Reputation: 1493

Mysql connection - is faster to connect via IP then domain?

Question

Is faster this

$servername = "125.125.55.10";
$username = "username";
$password = "password";
$conn = new mysqli($servername, $username, $password);

or this?

$servername = "mysql.server.com";
$username = "username";
$password = "password";
$conn = new mysqli($servername, $username, $password);

or it has no effect to speed?

I do not know if it has any practical use, but millisecond is milisecond....

Thank you

Upvotes: 3

Views: 1494

Answers (2)

strangeqargo
strangeqargo

Reputation: 1272

The thing is, the first time your web-server will access mysql-database, it will cache resolved hostname. The problem with using IPs is that it's basically hardcode, it's hard to grep, IP can change far more frequently then hostname, etc. More than that: spending your time on premature optimizations like

  • connect using hostname/ip
  • connect using unix socket/tcp socket
  • use persistent connections or not?

is a waste of time, if your tables/scripts are not optimized.

what to look for in database (sql/nosql) performance:

  • learn to use your dbms indexes / learn "EXPLAIN query"
  • never update huge number of rows one by one, update in batch-mode only
  • while developing a project, generate a LOT (>500k of rows) of fake data and insert it to database, this way you will know when your 'perfect query' is slow before your production server suffers

Upvotes: 1

James Paterson
James Paterson

Reputation: 2915

Yes, it could be considered slightly faster, as there is no need to resolve the domain to an IP address (DNS lookup). However, most modern technologies will cache the IP address when a connection is made, meaning a DNS lookup does not have to be made for every connection to the same address. So the difference is neglible, apart from the first time you connect to a site, and even then only for a few seconds at most.

Upvotes: 4

Related Questions