gpt4 book ai didi

mysql - Node MySQL : measure query execution time

转载 作者:可可西里 更新时间:2023-11-01 07:58:47 24 4
gpt4 key购买 nike

我在一个共享托管平台上,想限制我的应用程序中的查询,这样如果在可变时间段内总执行时间超过一定数量,我可以让应用程序冷却下来,然后在稍后恢复.

为此,我想实时了解我的每个查询需要多长时间,并在应用程序内管理它,而不是通过外部分析。

我在 PHP 中看到过在查询前后记录时间的示例 ( even phpMyAdmin does this ),但这在 NodeJS 或任何异步运行查询的程序中不起作用。

所以问题是:我将如何在 NodeJS 中获取查询的实际执行时间?

作为引用,我正在使用此模块查询 MySQL 数据库:https://github.com/felixge/node-mysql/

最佳答案

一个选项是在查询之前加上时间戳,在查询之后加上时间戳,然后像这样检查差异:

// get a timestamp before running the query
var pre_query = new Date().getTime();
// run the job
connection.query(query, function(err, rows, fields) {
// get a timestamp after running the query
var post_query = new Date().getTime();
// calculate the duration in seconds
var duration = (post_query - pre_query) / 1000;
});

关于mysql - Node MySQL : measure query execution time,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26141129/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com