I would say that most of the time it makes a very small difference. The only time it would make a noticeable difference is if your script takes some non-trivial amount of time to complete processing after the last database access is completed, in which case disconnecting from the database would probably be a good idea. Other than that, what you want to avoid is repeatedly connecting/disconnecting within the same script; and anything that takes a fair amount of time (such as requests to external APIs or involved loops through an array, etc.) should be done before you connect to the DB or after you close the DB connection, as appropriate.