NodeJS MySQL:衡量查询执行时间 [英] NodeJS MySQL: measure query execution time

查看:422
本文介绍了NodeJS MySQL:衡量查询执行时间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用一个共享的托管平台,并且希望限制我的应用程序中的查询,以便在可变时间段内如果总执行时间超过一定量,那么我可以使该应用程序冷静下来,然后在以后继续运行

I'm on a shared hosting platform and would like to throttle the queries in my app so that if the total execution time exceeds a certain amount over a variable time period then I can make the app cool off and then resume later on.

为此,我想了解我的每个查询要花多长时间,并在应用程序内进行管理,而不是在外部进行分析.

To do this I would like to find out how long each of my queries take in real time and manage it within the app and not by profiling it externally.

我已经看到了PHP中的示例,其中记录了查询前后的时间(

I've seen examples in PHP where the time is recorded before and after the query (even phpMyAdmin does this), but this won't work in NodeJS or anything that runs the query asynchronously.

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

So the question is: how would I go about getting the actual execution time of a query in NodeJS?

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

For reference I am using this module to query the MySQL db: https://github.com/felixge/node-mysql/

推荐答案

一种选择是在查询之前进行时间戳记,在查询之后进行时间戳记,然后检查差异,如下所示:

One option is just to timestamp before the query and timestamp after the query and check the difference like this:

// 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;
});

这篇关于NodeJS MySQL:衡量查询执行时间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆