平面文件与数据库-速度? [英] Flat file vs database - speed?

查看:86
本文介绍了平面文件与数据库-速度?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在制作一个聊天程序,我需要一个存储消息的地方.客户端将每隔x秒与服务器联系一次,并获取最后收到的消息ID,然后服务器将在客户端已加入的房间中找到ID大于此ID的所有消息.

I'm making a chat program, and I need a place to store messages. The client would contact the server every x seconds with the last received message id, and the server would find all messages with a id higher than that, in the rooms that the client has joined.

由于我不会永远存储东西,因此我考虑使用仅包含最后40条左右消息的平面文件(每个房间一个,以及直接消息).但是我认为,通过比较数字,数据库会更快.

As I'm not going to store things forever, I'm thinking of using flat files (one per room, as well as direct messages) with only the last 40 or so messages. However I think with comparing numbers a database would be faster.

我应该使用哪种数据存储方式?

What method of data storage should I use?

推荐答案

平面文件可能会更快一些,但从长远来看,它最终会带来更多的错误,因为您不只是执行SELECT * FROM messages WHERE room=nnn AND ID > yyy,而是将必须加载文件,解析文件,扫描每一行以获取消息ID,转到正确的消息,然后将其读出.

The flat file may be a bit faster, but it will end up being more buggy in the long run, because instead of just doing a SELECT * FROM messages WHERE room=nnn AND ID > yyy, you will have to load the file, parse it, scan each row for the message ID, go to the right message, and then read it out.

那只是第一个问题.文本文件不支持多个用户的书写(如果两个人同时张贴到同一房间该怎么办?),并且很容易损坏.

That's only the first problem. A text file doesn't support writing by multiple users (what if two people are posting to the same room at the same time?), and can easily become corrupt.

考虑到所有问题,我想说最好还是使用数据库,即使它是SQLite之类的简单对象,它也具有出色的PHP支持.但是,考虑到多用户情况,MySQL可能是一个更好的选择.另外,MySQL具有出色的缓存功能,因此在大多数情况下,最近的数据将直接来自RAM,并且提供的速度比任何一种在PHP中扫描文本文件的速度都要快.

All things considered, I'd say it's better across the board to use a DB, even if it's something simple like SQLite, which has excellent PHP support. However, consider the many-users condition, MySQL is probably a much better choice. Also, MySQL has excellent caching capabilities, so most of the time, the recent data will come directly from RAM, and will be served faster than you could scan a text file in PHP either way.

这篇关于平面文件与数据库-速度?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆