Chapter 18. Improving Database Efficiency

This quick chapter is for people making database-enabled PHP web sites who suspect that they are doing things awkwardly or inefficiently. Maybe you are new to databases, or maybe you know there must be a way to speed things up just because your pages are loading unacceptably slowly.

We offer some tips and tricks for making things run faster, and we show you some common ways that database systems can save you from writing unnecessary PHP code. As usual, some of our code examples will use MySQL functions, although the lessons are mostly general and independent of particular database implementations.

Note

This chapter will do little to help you get your database-enabled code working in the first place. For a guide to common errors, gotchas, and problems with PHP/database code, see Chapter 19.

Connections — Reduce, Reuse, Recycle

One important thing to realize is that establishing an initial connection with a database is never a cheap operation in terms of resource usage and time. Unless your PHP script is doing some unusually computationally intensive work, the overall database interaction will be the most time- and resource-intensive part of your code, and it is frequently true that the establishment of a connection is the most expensive (in terms of resource usage) part of code that interacts with a database, even if the connection is only established ...

Get PHP6 and MySQL® 6 Bible now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.