What's quicker; including another file or querying a MySQL database in PHP?

后端 未结 12 1684
闹比i
闹比i 2021-01-12 04:29

In PHP, which is quicker; using include(\'somefile.php\') or querying a MySQL database with a simple SELECT query to get the same information?

相关标签:
12条回答
  • 2021-01-12 04:55

    Why not do it both ways and see which is faster? Both solutions are pretty trivial.

    0 讨论(0)
  • 2021-01-12 04:57

    It's very hard/impossible to give an exact answer, as there are too many unknown variables - what if the filesystem is mounted on an NFS that resides on the other side of the world? Or you have the whole MySQL database in memory. The size of the database should be factored in too.

    But, on a more answer-y note, a safe guess would be that MySQL is faster, given good indexes, good database structure/normalization and not too fancy/complex queries. I/O operations are always expensive (read: slow), while, as previously mentioned, the whole dataset is already cached in memory by MySQL.

    Besides, I imagine you thought of doing further string manipulation with those included files, which makes things even more troublesome - I'm convinced MySQL's string searching algorithms are much better optimized than what you could come up with in PHP.

    0 讨论(0)
  • 2021-01-12 05:02

    If you expect the number of terms to become larger at a later date, you're better off using MySQL with a fulltext search field.

    0 讨论(0)
  • 2021-01-12 05:05

    definitely include as long as the file isn't too big and you end up using too much memory in which case a database would be recommended

    0 讨论(0)
  • 2021-01-12 05:06

    If you use a PHP bytecode cache like APC or Xcache, including the file is likely to be faster. If you're using PHP and you want performance, a bytecode cache is absolutely a requirement.

    It sounds like you're considering keeping static data around in a PHP script that you include, to avoid hitting the database. You're basically doing a rudimentary cache. This can work okay, as long as you have some way to refresh that file if/when the data does change. You might also look want to learn about the MySQL Query Cache to make SQL queries against static data faster. Or Memcached for keeping static data in memory.

    0 讨论(0)
  • 2021-01-12 05:09

    The difference in time is more down to the system design than the underlying technique I'd dare say. Both a MySQL result and a file can be cached in memory, and the performance difference there would be so small it is neglectable.

    Instead I would ask myself what the difference in maintenance would be. Are you likely to ever change the data? If not, just pop it in a plain file. Are you likely to change bits of the content ever so often? If so, a database is way easier to manipulate. Same thing for the structure of the data, if it needs "restructuring", maybe it is more efficient to put it in a database?

    So: Do what you feel is most convenient for you and the future maintainer of the code and data. :-)

    0 讨论(0)
提交回复
热议问题