MYSQL – number of rows returned equals number of connections

MySQL

Does my query get sent to the database once and I get a list of all the results in one shot which I then loop through, or do I have to request the next row from the DB each time?

Essentially, does reducing the number of rows I expect to return mean less connections/calls to the DB meaning my DB will be able to handle more connections at once, or is the number of database connections not dependent on the number of returned rows?

Best Answer

A connection survives until one of these:

  • You explicitly call disconnect(), or
  • Your program terminates, or
  • A network glitch causes the disconnect.

Normally, you should code your app to connect once near the beginning and disconnect when finished. (There are rare use cases for having multiple connections open at once.)

Most APIs have an obscure setting that determines whether you get all the rows (of a SELECT) in one "round trip" to the server, or whether they are fed to you individually (maybe in small chunks??) in many round trips. There are advantages and disadvantages of either. 99.9% of the time, I am happy getting the entire resultset on one round trip, even if it is 100K rows.