简体   繁体   English

php读取文件并从mysql读取数据哪一个更快

[英]php reading a file and reading data from mysql which one is faster

It's a real php interview question. 这是一个真正的PHP面试问题。 I know the answer is not just which one is faster. 我知道答案不仅仅是哪一个更快。 We can answer it in many aspects. 我们可以在很多方面回答这个问题。 Can anyone give me some suggestions please? 请问有人能给我一些建议吗?

files: 文件:

  • reading file: fast 阅读文件:快
  • predicting format/codepage: slow, painstaking, error prone 预测格式/代码页:缓慢,艰苦,容易出错
  • file permissions management 文件权限管理
  • multiple write access not possible 多次写访问不可能
  • locking mechanism strategy required 需要锁定机制策略
  • parsing file: relatively fast. 解析文件:比较快。 depending on data complexity 取决于数据的复杂性
  • file seek of file in directory with many other files(1000+): extremely slow as the OS will iterate through the file list in directory to find your requested file with a binary search(if you're lucky) 文件在目录中查找包含许多其他文件(1000+):非常慢,因为操作系统将遍历目录中的文件列表以通过二进制搜索找到您请求的文件(如果您很幸运)
  • reading not possible when other is writing 当其他人写作时,阅读不可能
  • threaded fork issues 螺纹叉问题
  • large filesizes if stored in text 如果存储在文本中,则大文件大小
  • In short: only use files for static data like configuration files. 简而言之:仅将文件用于配置文件等静态数据。 Never for dynamic data 从不用于动态数据

Database: 数据库:

  • better management of all of the above 更好地管理上述所有方面
  • compact storage 紧凑的存储
  • fast lookup engine 快速查找引擎
  • easy combining of related fact 容易结合相关事实
  • easy to share access with other machines/programs 易于与其他机器/程序共享访问
  • rollback mechanisms built in. 内置的回滚机制。
  • Don't use for configuration that remain static. 不要用于保持静态的配置。

This is an example of an interview question with no correct answer. 这是一个没有正确答案的面试问题的例子。 A case could be made for either of these things. 可以针对这些事情中的任何一个做出案例。

For files you might say they're quick to load, that there's a lot of kernel optimization around fetching them from disk and providing them to a user process, and even more around sending them directly from disk to a socket via something like sendfile . 对于文件,您可能会说它们可以快速加载,有很多内核优化可以从磁盘中获取它们并将它们提供给用户进程,甚至可以通过sendfile等方式将它们直接从磁盘发送到套接字。 That would be true. 那是真的。

Then for databases you could say that frequently accessed data is stored in memory so there's no I/O round-trip to disk, that could be faster, especially if you're comparing reading parts of a file using a suboptimal structure versus records in a database. 然后对于数据库,你可以说频繁访问的数据存储在内存中,因此没有I / O往返磁盘,这可能会更快,特别是如果你使用次优结构比较文件中的记录而不是数据库。 This is an algorithmic concern as well. 这也是一个算法问题。

So it really depends on what kinds of files and what sort of read/write access patterns are involved. 所以它实际上取决于涉及哪种类型的文件和哪种读/写访问模式。 To say either of these things is faster is to miss the point of the question. 要说这些事情中的任何一个更快就是错过了问题的重点。

Reading one file = fast. 读一个文件=快。

Reading many / big files = slow. 读多个/大文件=慢。

Reading singular small entries from database = waste of I/O. 从数据库中读取单个小条目=浪费I / O.

Combining many entries within the database = faster than file accesses. 组合数据库中的许多条目=比文件访问更快。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM