mysql – Is it safe to query my database for every page load?

mysql – Is it safe to query my database for every page load?

As with all things, the answer is it depends. 🙂

Most web sites you visit queries something from a database on every page load. If the queries are crafted well, they look up just the data they need, and avoid scanning through a big database. You might like my presentation How to Design Indexes, Really (video) to help with this.

Another strategy is to use a fast cache in RAM for data that is needed frequently. RAM is thousands of times faster than disk drives. You might like to familiarize yourself with the Numbers Everyone Should Know. Those numbers are just examples, but the intention is to get programmers to think about the fact that moving data around has different cost as you use RAM vs. disk vs.network vs. CPU.

P.S.: Please dont buy into the myth that youre not good at computers because youre a woman. Everyone starts out as a novice, no matter what their gender or background. Only through practice and study do any of us learn this stuff. I recommend seeing Hidden Figures, the story of the women who did pioneering math and programming for NASA.

Another notable woman is Margaret Hamilton, who practically invented the profession of software engineering.

Yes you are OK to query the database on every page load.

Think about websites like Facebook. When you visit the site it needs to know who you are – it gets that from a database. It needs to know all of the status updates that its going to show you – it gets that from a database. When you hit the bottom of the news feed and it gets more for you to read – it gets that from a database.

mysql – Is it safe to query my database for every page load?

Thats normal. Most web applications have to query the database for each page load (usually several times), since most of the page content comes from the database.

If youre concerned about performance, think about this: is the query different for each page? Or is it loading the same data over and over? If it keeps querying the same thing (like the current users name), you can improve performance by storing the data in the applications session state. But if its different (like how many unread messages the user has), youll need to run the query each time.

Leave a Reply

Your email address will not be published. Required fields are marked *