title: Why is my first AWS RDS query taking longer than the rest using Sequelize?
body: <p>I am developing an endpoint in Node/Koa which will make several queries to a POSTGRES DB hosted in AWS RDS. The code looks something like this.</p>
  <pre><code>const handleGetRequest = async (req, res) =&gt; {
    console.time(&quot;time:gettingShopName&quot;);
    const existingShop = await models.Shops.findOne({
      where: {
        shopName: &quot;ingbe123.myshopify.com&quot;,
      },
    });
    console.timeEnd(&quot;time:gettingShopName&quot;);
  
    console.time(`time:gettingPlans`);
    const plan = await models.Plans.findOne({
      where: {
        name: &quot;Pro&quot;,
      },
    });
    console.timeEnd(`time:gettingPlans`);
  
    return res.send({
      plan,
      existingShop,
    });
  };
  </code></pre>
  <p>The issue is that the first model query (<code>models.Shops.findOne</code>) is taking more than 3 seconds to respond, when the query <code>models.Plans.findOne</code> is only taking about 200ms.</p>
  <p>I've already tested that the query is not the issue, since if I delete the first query, then the second query will also take around 3 seconds.</p>
  <p>So I suspect it may have something to do with how Sequelize is handling connections to the DB.</p>
  <p>Here is my <code>models/index.js</code> file:</p>
  <pre><code>const { Sequelize } = require(&quot;sequelize&quot;);
  const path = require(&quot;path&quot;);
  const fs = require(&quot;fs&quot;);
  
  const sequelize = new Sequelize(process.env.POSTGRES_URI, {
    define: {
      freezeTableName: true,
      underscored: true,
      timestamps: false,
    },
    ssl: true,
    dialectOptions: {
      ssl: {
        require: true,
        rejectUnauthorized: false, // &lt;&lt;&lt;&lt;&lt;&lt; YOU NEED THIS
      },
    },
    operatorAliases: Sequelize.Op,
  });
  
  const db = {};
  fs.readdirSync(path.resolve(&quot;server/models/&quot;))
    .filter(
      (file) =&gt; file.indexOf(&quot;.&quot;) !== 0 &amp;&amp; file !== &quot;index.js&quot; &amp;&amp; file !== &quot;views&quot;
    )
    .forEach((file) =&gt; {
      const model = require(`./${file}`)(sequelize, Sequelize);
  
      db[model.name] = model;
    });
  
  Object.keys(db).forEach((model) =&gt; {
    if (&quot;associate&quot; in db[model]) {
      db[model].associate(db);
    }
  });
  
  db.Sequelize = Sequelize;
  db.sequelize = sequelize;
  
  module.exports = db;
  </code></pre>
  <p>And my two models in case needed:</p>
  <p><code>models/Shops.js</code></p>
  <pre><code>const Sequelize = require(&quot;sequelize&quot;);
  
  module.exports = (sequelize) =&gt; {
    const Shops = sequelize.define(
      &quot;Shops&quot;,
      {
        id: {
          type: Sequelize.UUID,
          autoIncrement: true,
          primaryKey: true,
        },
        shopName: {
          type: Sequelize.TEXT,
          field: &quot;shop_name&quot;,
        },
        isInstalled: {
          type: Sequelize.BOOLEAN,
          field: &quot;is_installed&quot;,
        },
        planId: {
          type: Sequelize.UUID,
          field: &quot;plan_id&quot;,
        },
        updatedAt: {
          type: Sequelize.DATE,
          field: &quot;updated_at&quot;,
          defaultValue: Sequelize.NOW,
        },
        createdAt: {
          type: Sequelize.DATE,
          field: &quot;created_at&quot;,
          defaultValue: Sequelize.NOW,
        },
        deletedAt: {
          type: Sequelize.DATE,
          field: &quot;deleted_at&quot;,
        },
      },
      {
        tableName: &quot;shops&quot;,
      }
    );
  
    Shops.associate = (models) =&gt; {
      Shops.hasMany(models.Sessions, { as: &quot;sessions&quot;, foreignKey: &quot;shopId&quot; });
      Shops.hasMany(models.ShopsMergers, {
        as: &quot;shopMergers&quot;,
        foreignKey: &quot;shopId&quot;,
      });
      Shops.belongsTo(models.Plans, { as: &quot;plan&quot;, foreignKey: &quot;planId&quot; });
    };
  
    return Shops;
  };
  
  </code></pre>
  <p><code>models/Plans.js</code></p>
  <pre><code>const Sequelize = require(&quot;sequelize&quot;);
  
  module.exports = (sequelize) =&gt; {
    const Plans = sequelize.define(
      &quot;Plans&quot;,
      {
        id: {
          type: Sequelize.UUID,
          autoIncrement: true,
          primaryKey: true,
        },
        name: {
          type: Sequelize.TEXT,
          field: &quot;name&quot;,
        },
        price: {
          type: Sequelize.FLOAT,
          field: &quot;price&quot;,
        },
        mergeLimits: {
          type: Sequelize.INTEGER,
          field: &quot;merge_limits&quot;,
        },
        isDefault: {
          type: Sequelize.BOOLEAN,
          field: &quot;is_default&quot;,
          defaultValue: &quot;false&quot;,
        },
        terms: {
          type: Sequelize.TEXT,
          field: &quot;terms&quot;,
        },
        description: {
          type: Sequelize.JSON,
          field: &quot;description&quot;,
        },
        operationLimit: {
          type: Sequelize.INTEGER,
          field: &quot;operation_limit&quot;,
        },
      },
      {
        tableName: &quot;plans&quot;,
      }
    );
  
    Plans.associate = (models) =&gt; {
      Plans.hasOne(models.Shops, { as: &quot;shop&quot;, foreignKey: &quot;planId&quot; });
    };
  
    return Plans;
  };
  
  </code></pre>