在生产中记录 SQL 查询?

发布于 2024-09-03 21:19:13 字数 120 浏览 2 评论 0原文

我在是否也在生产中记录 SQL 查询时遇到了困境。

我不知道用PHP写文件有多慢。也许一些基准测试可以给出一些答案,但我想看看你们之前是怎么想的。

什么会或不会使该过程变慢?或者说它取决于什么?

I'm having a dilemma on whether or not to log SQL queries in production as well.

I don't know how slow writing files is in PHP. Probably some benchmarks could give some answers, but I wanted to see what you guys think before.

What does or does not make the process slow? Or what things could it depend on?

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

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

发布评论

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

评论(4

郁金香雨 2024-09-10 21:19:13

大多数数据库都有用于记录查询和慢速查询的内置选项,因此您不需要通过 PHP 进行记录。您不应该记录生产中的所有查询,除非遇到问题并且这是故障排除过程的一部分。您可以而且应该记录缓慢的查询,以便您可以了解可能导致生产站点速度减慢的原因。

如果您的框架支持它,则仅当页面花费一定时间来生成时才可以记录查询(这就是我所做的)。然后,您有条件地记录日志,可能会发现正在运行的查询数量过多。

Most databases have built-in options for logging queries and slow queries, so you shouldn't need log through PHP. You should not log all queries in production unless you are having problems and it's part of a troubleshooting process. You can and should log slow queries so you can see what may be slowing down your production site.

If you framework supports it, you can log queries only if the page took a certain amount of time to generate (this is what I do). Then you are logging conditionally and may discover an excessive number of queries being run.

陪你搞怪i 2024-09-10 21:19:13

您有几个选择:

  • 让数据库记录查询
  • 使用静态方法创建记录器类,该方法使用缓存的文件句柄进行写入......这非常快。另外,您可以将此类设置为查看配置中的日志变量,以忽略传入的 sql 查询或将其记录到文件中。假设您正在使用数据库 API,您可以扩展查询函数以包含这行额外的代码(可选)日志记录

You have a couple options:

  • have your database log the queries
  • Create a logger class with a static method that uses a cached file handle to write to.. this is pretty fast. Also, you can set this class up to look at a log variable in config to just ignore the incoming sql query or log it to file. Assuming you are using a database API, you can just extend the query function to include this extra line of code for (optional) logging
星星的軌跡 2024-09-10 21:19:13

根据记录(您没有指定数据库),Postgresql 有一个 一堆与日志记录相关的选项。其中,我使用log_min_duration_statement来记录运行时间超过N秒的查询。对于分析很有用,不会填充日志文件并干扰性能。我敢打赌大多数数据库都有类似的东西。

For the record (you didn't specify your DB), Postgresql has a bunch of options related with logging. Among them, I use log_min_duration_statement to log the queries that ran for more than N seconds. Useful for profiling, without filling your log files and interfering with performance. I'd bet most databases have something similar.

七颜 2024-09-10 21:19:13

嗯,速度慢的第一件事是通过访问数据库进行磁盘 IO。最好的答案是你在一些重要的情况下尝试一下(记住,对于小 n 来说一切都很快),并询问一些利益相关者性能是否可以接受。这可能不是您想要的答案,但这确实是最好的答案。

Well, the number 1 thing that would be slow would be disk IO via hitting the db. The best answer is for you to try it in some non-trivial cases (remember, everything is fast for small n) and ask some stakeholders if the performance is acceptable. It might not be the answer you are after, but it's really the best answer.

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