node-mysql2 icon indicating copy to clipboard operation
node-mysql2 copied to clipboard

typeCast is up to 10x slower

Open gbelur opened this issue 7 years ago • 3 comments

When the typeCast option is used, queries tend to run up to 10x slower under load. A perf profile shows that most of the additional time is spent in column_defintion.js obtaining the schema and table name in the code below:

const addString = function(name) {
  Object.defineProperty(ColumnDefinition.prototype, name, {
    get: function() {
      const start = this['_' + name + 'Start'];
      const end = start + this['_' + name + 'Length'];
      return StringParser.decode(
        this._buf.slice(start, end),
        this.encoding === 'binary' ? this._clientEncoding : this.encoding
      );
    }
  });
};

These fields are meant to be computed lazily, but end up getting recomputed for every field access as part of the 'wrap' function implementation that needs field.schema, field.table and field.name

function wrap(field, type, packet, encoding) {
    return {
      type: type,
      length: field.columnLength,
      db: field.schema,
      table: field.table,
      name: field.name,
      string: function() {
        return packet.readLengthCodedString(encoding);
      },
      buffer: function() {
        return packet.readLengthCodedBuffer();
      },
      geometry: function() {
        return packet.parseGeometryValue();
      }
    };
  }

Given that these fields are used for every column, can these be precomputed?

gbelur avatar Nov 28 '18 01:11 gbelur

@gbelur Can you try with 2.3.3-rc0 to see if it is ok now?

testn avatar Nov 09 '21 06:11 testn

I thought I fixed this already @sidorares

testn avatar Jun 03 '23 04:06 testn

I thought so as well Need to test and close if can't confirm

sidorares avatar Jun 03 '23 04:06 sidorares