从 SQL 数据库而不是聊天应用程序的文件进行轮询会提高性能吗?

发布于 2025-01-03 05:11:18 字数 521 浏览 3 评论 0原文

我正在开发一个聊天应用程序,我希望使用 SQL 数据库。

我的问题是,经过几次谷歌搜索后,有人从一个网站告诉我,使用数据库会比使用普通文件(例如文本或 JSON 文件)慢得多,但在其他一些网站上,人们说完全相反。我不了解你们,但在为用户创建网络应用程序时,用户永远是第一位的。

因此,尽管我很想使用 SQL DB,因为 1.) 我对此有很好的经验,2.) 它允许我使应用程序变得更酷(更多功能)。但如果它会减慢用户端的速度(明显的滞后),那么它就是禁忌。

无论哪种方式,我都会使用 AJAX 和 PHP 连续“轮询”服务器以检查文件/数据库(是否有新消息、联系请求等)。

另外,如果您想知道,该应用程序不会像一对一聊天,它将有多个用户可以加入并与所有加入的用户交谈的“房间”。用户还可以请求“私人聊天” ” 与另一个用户,建立一对一的连接。

那么,就性能而言,MySQL 数据库还是无聊的文本/JSON/其他文件?

哦,还有一件事,我不想使用任何第三方库或 API。讨厌依赖别人的工作(多次失望)。

I'm working on a chat application which I would love to use a SQL db for.

My problem is, after a few google searches, i have people telling me from one site, that using a DB would be much slower then using a normal file (e.g Text or JSON file), but then on some other sites, people are saying the complete opposite. And I don't know about you guys, but when it comes to creating web apps for users, the users always come first.

So as much as I'd love to use a SQL DB as 1.) I have good experience with it and 2.) it allows me to make the application much more cooler (more features). but if it would slow things down on the users end (a noticeable lag), then its a no-no.

Either way, I will be "polling" the server continuously with AJAX and PHP to check the file/DB (for new messages, contact requests, ect ect).

Also, incase your wondering, the application wont be like a 1-to-1 chat, it will have "rooms" where multiple users can join and talk with all users joining in. The users will also be able to request a "private chat" with another user, where a 1-to-1 connection opens up.

So, MySQL Database OR a boring TEXT/JSON/OTHER file, in regards to performance?

Oh, one more thing, I don't want to use any third party libraries or APIs. Hate relying on other peoples work (been let down to many times).

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

浪菊怪哟 2025-01-10 05:11:18

如果您想实现 IRC 克隆,我认为您选择了错误的工具。

最好的方法是编写一个自定义 HTTP 服务器来处理内存中的所有内容。没有数据库,没有不断轮询文件。当消息到达时,您只需循环访问正确的内存列表并将消息分派给其他用户。对于浏览器到服务器的连接,我建议“Comet”(带有用于支持它们的浏览器(如果您愿意的话)。

PHP 可能不是首选语言,因为几乎所有使用 PHP 完成的工作都是基于传统的短的、孤立的请求。对于实时为多个客户端提供服务的长时间运行的流程,我建议使用 Python 或 Node.js 之类的工具。

If you're looking to implement an IRC clone, I think you've chosen all the wrong tools.

The best way to do this would be to write a custom HTTP server that handles everything in memory. No databases, no constant polling of files. When a message arrives, you simply loop through the correct in-memory list and dispatch the message to other users. For the browser to server connection, I suggest "Comet" (with web sockets for browsers that support them, if you're feeling up to it).

PHP likely isn't the language of choice for this, because pretty much all work done with PHP is based on traditional short, isolated requests. For a long-running process which serves multiple clients in real time, I'd suggest something like Python or Node.js.

忱杏 2025-01-10 05:11:18

您真的不想将聊天记录存储在文件中,这可能会造成管理噩梦,我建议您使用 MySQL 并确保它可以使用 Sockets 而不是 AJAX 轮询,Sockets 的扩展性非常好。

然而,关于如何将基于套接字的聊天与 MySQL 集成的信息并不多。

我已经做了一些测试,并在这里有一个基本的示例: https://github.com /andrefigueira/PHP-MySQL-Sockets-Chat

它利用 Ratchet (http://socketo.me/) 对于用 PHP 创建聊天服务器。

您可以通过发送服务器 JSON 以及谁正在聊天的信息来将聊天消息发送到数据库(当然,如果您有用户会话)

You don't really want to be storing chats in files, that can create a management nightmare, I would recommend you go with MySQL and to make sure it works probably go with Sockets instead of AJAX polling, Sockets will scale really well.

However there isn't much around about how you can integrate socket based chats with MySQL.

I have done a few tests and have a basic example working here: https://github.com/andrefigueira/PHP-MySQL-Sockets-Chat

It makes use of Ratchet (http://socketo.me/) for the creation of the chat server in PHP.

And you can send chat messages to the DB by sending the server JSON with the information of who is chatting, (if of course you have user sessions)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文