有人将数据库数据存储在 PHP $_SESSION 中吗?

发布于 2025-01-04 00:50:38 字数 616 浏览 2 评论 0原文

我刚刚加入一个项目,并一直在检查代码。我们需要将大量数据导出到 Excel 供内部用户使用。在给定时间大约有 5 个人可以访问此功能。为了输出到 Excel,我发现:

  • 从数据库检索数据,存储在 $_SESSION
  • 显示数据的 HTML 页面视图

  • 当用户想要导出时< /p>

    • 从 $_SESSION 检索数据库数据
    • 在 CSV 内存中创建字符串
    • 使用 Excel 作为文件类型打印 HTTP 标头
    • 打印出 CSV 格式的字符串

$_SESSION 中的存储正在发生即使用户不打算导出。这让我觉得效率非常低,因为 $_SESSION 变量的大小可能会爆炸,因为每个数据库表检索最多可以达到 30MB表,$_SESSION 的过期时间设置为 24 小时。因此,系统中最多可能有 5 个用户,并且 $_SESSION 变量最多为 150MB。 听起来不错吗?

还有其他人见过这样的事情吗?这就像盗版 Memcache 吗?将数据写入每隔几个小时更新一次的平面文件不是最好吗?

I just joined a project, and have been going over the code. We need to export a lot of data out to Excel for our internal users. There are roughly 5 people who would have access to this functionality at a given time. In order to output to Excel, here's what I found:

  • retrieve data from DB, store in $_SESSION
  • show HTML page view of data

  • when the user wants to export

    • retrieve the DB data from $_SESSION
    • create a string in memory of a CSV
    • print the HTTP Headers with Excel as the filetype
    • print out the CSV formatted strings

This storage in $_SESSION is happening even when the user is not going to export. This strikes me as terribly inefficient, since the $_SESSION variable could explode in size, since each of the DB table retrievals can be up to 30MB per table, and the expiration on $_SESSION is set to 24 hours. So potentially, there could up to 5 users in the system, with up to 150MB $_SESSION variables. Sound about right?

Has anyone else ever seen something like this? Is this like a bootleg Memcache? Wouldn't it be best to just write the data to a flat-file that's updated once every few hours?

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

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

发布评论

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

评论(4

z祗昰~ 2025-01-11 00:50:38

我确实在会话中存储数据库一些数据,例如我在每个页面上使用的 ID 或小对象。

但是,当涉及到无法为每个页面加载即时提取的较大数据集时,我通常更喜欢将它们存储在 MEMORY/HEAP 表(或临时文件)中,并且只在会话中存储 ID,这样我将能够轻松提取它们。

您可能想看一下有关会话最大大小的问题:
PHP 会话的最大大小

I do store database some data in session, like ID or small object that I use on every page.

But when it come to larger dataset that I can't extract on the fly for each page load, I often prefer to store them in a MEMORY/HEAP table ( or a temporary file ), and just store an ID in the session so I'll be able to extract them easily.

You might want to take a look at this question about session max size:
Maximum size of a PHP session

≈。彩虹 2025-01-11 00:50:38

我也看到过这个,这是一个坏主意。有时,您希望在屏幕上显示数据表,但又希望将其导出,但没有充分的理由将其填充到会话内存中。如果操作系统需要交换并且会话被写入文件,那么您就会遇到文件 IO 速度问题,因此在某些情况下,它可能比对数据库的新查询慢。

I have seen this as well and it is a bad idea. Sometimes you want to display on screen a table of data but also make it available for export, but there is no good reason for stuffing into session memory. If the OS needs to swap and the session gets written to file then you have file IO speed issues, so in some cases it is likely slower than a fresh query to the database.

世界如花海般美丽 2025-01-11 00:50:38

您的示例中的 $_SESSION 用于存储不需要的数据,以确保页面视图之间的一致性,因此这是毫无意义的。它用于存储诸如last_page_viewed之类的内容,而不是复制数据库本身。这样做的唯一原因是,如果数据库调用获取数据的成本非常昂贵,即使您描述的存储效率低下,它也会提高性能。这是不太可能的,而且听起来像是懒惰的编码。

如果他们想要导出,应该有一个 zip 函数,可以使用相同的 SQL 读取所有数据,并根据需要将其打包成 excel 文件。理想情况下使用 MVC,以便可以将相同的代码输入到 HTML 或拉链功能中;)

$_SESSION in your example is being used to store data which is not needed in order to ensure consistency across page views, so this is pointless. It's there to store stuff like last_page_viewed, not to duplicate the DB itself. The only reason to do it like that is if the DB calls to get the data are so hideously expensive that even with the storage inefficiency you describe, it improves performance. This is very unlikely and it sounds like lazy coding.

If they want to export, there should be a zip function that reads all the data using the same SQL and packages it into an excel file on demand. Ideally using MVC so that the same code can be fed into the HTML or the zipper function ;)

救星 2025-01-11 00:50:38

如果您的数据库不经常更新,您的解决方案可能会起作用,否则您的用户可能会获得过时的数据。 (而且我认为无论如何都不值得存储在会话数据中。)

正如您在此处所解释的,我认为您将在 LAN 中使用它,并且并发用户数不超过 5 个。如果我是对的,为什么你不直接从数据库中读取数据库并在 HTML 上显示它(我猜你可以使用分页并且不想在单个 HTML 页面中显示所有 30MB 数据)再次导出全部当用户请求时,数据直接从数据库传输到 Excel :)

Your solution could work if your database not update frequently otherwise your users may get outdated data. (And I don't think it's worth to store in the session data anyway.)

As you explained here, I think you are going to use this in a LAN and you don't have more than 5 concurrent users. If I'm right about that why don't you just read database straightly from the database and show it on HTML(I guess you can use paging and don't want to show all 30MB data in a single HTML page) again Export all the data to Excel straight from the DB when user request it :)

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