开发者

What is more expensive for template reading: Database query or File reading?

开发者 https://www.devze.com 2023-01-28 05:04 出处:网络
My question is fairly simple; I need to read out some templates (in PHP) and send them to the client.

My question is fairly simple; I need to read out some templates (in PHP) and send them to the client.

For this kind of data, specifically text/html and text/javascript; is it more expensive to read them out a MySQL database or out of files?

Kind regards

Tom

inb4 security; I'm aware.

PS: I read other开发者_如何学运维 topics about similar questions but they either had to do with other kind of data, or haven't been answered.


Reading from a database is more expensive, no question.

Where do the flat files live? On the file system. In the best case, they've been recently accessed so the OS has cached the files in memory, and it's just a memory read to get them into your PHP program to send to the client. In the worst case, the OS has to copy the file from disc to memory before your program can use it.

Where does the data in a database live? On the file system. In the best case, they've been recently accessed so MySQL has that table in memory. However, your program can't get at that memory directly, it needs to first establish a connection with the server, send authentication data back and forth, send a query, MySQL has to parse and execute the query, then grab the row from memory and send it to your program. In the worst case, the OS has to copy from the database table's file on disk to memory before MySQL can get the row to send.

As you can see, the scenarios are almost exactly the same, except that using a database involves the additional overhead of connections and queries before getting the data out of memory or off disc.


There are many factors that would affect how expensive both are.

  • I'll assume that since they are templates, they probably won't be changing often. If so, flat-file may be a better option. Anything write-heavy should be done in a database.

  • Reading a flat-file should be faster than reading data from the database.

  • Having them in the database usually makes it easier for multiple people to edit.

You might consider using memcache to store the templates after reading them, since reading from memory is always faster than reading from a db or flat-file.


It really doesnt make enough difference to worry you. What sort of volume are you working with? Will you have over a million page views a day? If not I'd say pick whichever one is easiest for you to code with and maintain and dont worry about the expense of the alternatives until it becomes a problem.

Specifically, if your templates are currently in file form I would leave them there, and if they are currently in DB form I'd leave them there.

0

精彩评论

暂无评论...
验证码 换一张
取 消