npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

mysql-query-time

v1.1.5

Published

measuring MySQL query execution time

Downloads

5

Readme

MySQL query time

Table of contents

Why MySQL Query Time

If you have ever developed backend, you may have thought about database performance. There are a lot to consider about performance, however SQL query execution time is one of the most important things. This module will help you measure query execution time in your project.

Installation

npm install -D mysql-query-time

How to Use

1. Set MySQL

First of all you need to set MySQL using setMysql(). This function must be executed only once each time the database is started.

Also you can do it yourself without setMysql() fallowing MySQL Documentation(22.11 Performance Schema General Table Characteristics)

UPDATE performance_schema.setup_actors
SET ENABLED = 'NO', HISTORY = 'NO'
WHERE HOST = '%' AND USER = '%';

INSERT INTO performance_schema.setup_actors
(HOST,USER,ROLE,ENABLED,HISTORY)
VALUES('localhost','test_user','%','YES','YES');

UPDATE performance_schema.setup_instruments
SET ENABLED = 'YES', TIMED = 'YES'
WHERE NAME LIKE '%statement/%';

UPDATE performance_schema.setup_instruments
SET ENABLED = 'YES', TIMED = 'YES'
WHERE NAME LIKE '%stage/%';

UPDATE performance_schema.setup_consumers
SET ENABLED = 'YES'
WHERE NAME LIKE '%events_statements_%';

UPDATE performance_schema.setup_consumers
SET ENABLED = 'YES'
WHERE NAME LIKE '%events_stages_%';

2. Set .env file

Make .env file and set some environment variables about your database.
If you set MySQL yourself instead of setMysql(), you don't need to set variables about MASTER.

for example

MASTER_HOST='localhost'
MASTER='root'
MASTER_PASSWORD='root_password'

TESTER='cloer'
TESTER_HOST='localhost'
TESTER_PASSWORD='my_password'
TEST_DATABASE='my_database'

3. Import mysql-query-time and call createTest()

Import the package and call createTest(). It returns an function called Test Function

for example

const { setMysql, createTest } = require('mysql-query-time');

try {
  /* 
  setMysql() function must be executed only once each time the database is started.
  When you run this script again, you should change the line to a comment.
  */
  await setMysql();
  const test = await createTest();
  const result = await test('SELECT * FROM User1 WHERE userId=1');
  console.log(result.queryResult);
  console.log(result.timeResult);
} catch (err) {
  console.error(err);
}

Documentation

1. setMysql()

setMysql() sets MySQL. This function must be executed only once each time the database is started. When database restarts, this function should be executed.

2. createTest()

createTest() makes 2 connections and returns Test Function. About connections, one is for the schema you want to test, the other is for performance_schema. And send these connections to Test Function

3. Test Function

Test Function is a result of createTest(). This function take 2 arguments. first one is query and second one is callback that is error handler. query is required. This function sends the query to your schema and also sends a query requesting execution time to performance_schema. It returns an object that contains queryResult and timeResult. You can get query execution time from timeResult.