Mysql
 sql >> डेटाबेस >  >> RDS >> Mysql

ट्रैकिंग डीबी पूछताछ समय - बुकशेल्फ़/नेक्स

मैंने अभी कुछ छोटे परीक्षण कोड लिखे हैं कि कैसे knex के साथ लेन-देन की अवधि का पता लगाया जाए।

https://runkit.com/embed/679qu91ylu4w

/**
 * Calculate transaction durations in knex
 * 
 */
require('sqlite3');
var knex = require("knex")({
  client: 'sqlite', 
  connection: ':memory:', 
  pool: { min: 1, max: 10 }
});

function isTransactionStart(querySpec) {
  return querySpec.sql === 'BEGIN;';
}

function isTransactionEnd(querySpec) {
  return querySpec.sql === 'COMMIT;' || querySpec.sql === 'ROLLBACK;';
}

const transactionDurations = {};

knex.on('query', querySpec => {
  console.log('On query', querySpec);

  if (isTransactionStart(querySpec)) {
    if (transactionDurations[querySpec.__knexUid]) {
      console.error('New transaction started, before earlier was ended');
      return;
    }
    transactionDurations[querySpec.__knexUid] = new Date().getTime();
  }

  if (isTransactionEnd(querySpec)) {
    const startTime = transactionDurations[querySpec.__knexUid];
    if (!startTime) {
      console.error('Transaction end detected, but start time not found');
    }
    const endTime = new Date().getTime();
    transactionDurations[querySpec.__knexUid] = null;
    console.log('TRANSACTION DURATION', endTime - startTime);
  }
}); 

// just as an example of other available events to show when they are called
knex.on('query-response', (res, querySpec) => {
  // console.log('On query response', res, querySpec);
}); 

knex.on('query-error', (err, querySpec) => {
  // console.log('On query error', err, querySpec);
}); 

try {
    a = await Promise.all([
      knex.transaction(trx => {
        return trx.raw('select 1');
      }),
      knex.transaction(trx => {
        return trx.raw('select 2');
      }),
      knex.transaction(trx => {
        return trx.raw('error me');
      })
    ]);
} catch (e) {
  console.log('Got ERROR:', e);
}

दृष्टिकोण के एक ही राजा को क्वेरी टाइमिंग के लिए भी काम करना चाहिए। टाइमर बहीखाता पद्धति को स्मृति लीक होने से बचाने के लिए आपको हालांकि कुछ सफाई कोड जोड़ना चाहिए।

क्वेरी अवधि टाइमर query . में प्रारंभ होना चाहिए घटना और query-response . में रुक गई या query-error निर्भर करता है कि कौन पहले ट्रिगर करता है।

query . का मिलान करने में सक्षम होने के लिए - query-response जोड़ी querySpec.__knexQueryUid विशेषता का उपयोग किया जा सकता है।




  1. Database
  2.   
  3. Mysql
  4.   
  5. Oracle
  6.   
  7. Sqlserver
  8.   
  9. PostgreSQL
  10.   
  11. Access
  12.   
  13. SQLite
  14.   
  15. MariaDB
  1. SQL सर्वर डेटाबेस को MySQL में कैसे निर्यात करें?

  2. वेबसाइट डेटाबेस को कैसे अनुकूलित करें

  3. MySQL में कॉलम के डेटा प्रकार की जांच करने के 4 तरीके

  4. प्राथमिक कुंजी के रूप में तार का प्रदर्शन दंड?

  5. MySQL में एक्सेंट असंवेदनशील खोज क्वेरी