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

后端 未结 12 1700
闹比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 05:12

    I recently had this issue. I had some data in mysql that I was querying on every page request. For my data set, it was faster to write a fixed record length file than to use MySQL.

    There were a few different factors that made a file faster than MySQL for me:

    1. File size was small -- under 100kb of text data
    2. I was randomly picking and not searching -- indexes made no difference
    3. Connection time -- opening the file and reading it in was faster than connecting to the database when the server load was high. This was especially true since the OS cached the file in memory

    Bottom line was that I benchmarked it and compared results. For my workload, the file system was faster. I suspect if my data set ever grows, that will change. I'm going to be keeping an eye on performance and I'm ready to change how it works in the future.

提交回复
热议问题