Search code examples
file-read

Is reading from a database faster than reading from a file?


Today I found that a website code was read from a database. I don't know is this was intentionally or just Joomla works this way, but is was weird to me.

So I want to know if there is any performance gain from doing this? Will loading some string from a database will be faster than opening a file?


Solution

  • Almost always: no. In a battle of local file system cache vs. database page pool cache there is no much room to wiggle, but on the battle of file I/O API vs. SQL parsing and execution, the file API will clearly win. If it goes to disk, then a sequential file read will win over a page access, but on the other hand navigating a B-Tree will win over filesystem metadata. For a lengthy (somehow dated) discussion: To BLOB or Not To BLOB: Large Object Storage in a Database or a Filesystem.

    But before jumping to conclusion, one would need to ask why this decision was taken. There are more consideration than 'speed'. Was it done to have a consistent backup/restore? Perhaps integration with HA/DR? As a deployment alternative? Integration with content editing?