NodeJS MySQL:衡量查询执行时间

编程入门 行业动态 更新时间:2024-10-28 06:25:44
本文介绍了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中的示例,其中记录了查询前后的时间(即使phpMyAdmin也会这样做),但这在NodeJS或任何异步运行查询的程序中将不起作用.

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数据库: github/felixge/node-mysql/

For reference I am using this module to query the MySQL db: github/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:衡量查询执行时间

本文发布于:2023-10-17 06:29:42,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1500047.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:执行时间   NodeJS   MySQL

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!