Hay*_*yun 3 mysql optimization execution-time node.js node-mysql
我在共享托管平台上,并希望限制我的应用程序中的查询,以便如果总执行时间超过一定量的可变时间段,那么我可以让应用程序冷却,然后稍后恢复.
要做到这一点,我想知道我的每个查询实时采取多长时间并在应用程序中管理它,而不是通过外部进行分析.
我已经在PHP中看到了在查询之前和之后记录时间的示例(甚至phpMyAdmin都这样做),但这在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;
});
Run Code Online (Sandbox Code Playgroud)