首页 > 解决方案 > 带有 Sequelize 的 Express JS 需要更多时间来执行查询

问题描述

我有以下查询,需要0.0039 秒。在 phpMyAdmin 中。在带有 Sequelize 的 express js 框架中,相同的查询需要6 秒。总记录为267121。

SELECT pm.item_type,
       pm.product_type,
       pm.product_image,
       pm.product_id,
       pm.store_id,
       pm.item_type,
       pm.product_name,
       pm.product_description,
       spd.product_price
FROM product_master AS pm
JOIN store_products_detail spd ON pm.product_id = spd.product_id
WHERE spd.product_store_id IN(907)

我在 expressJS 框架中花费了以下时间。

query execution start time 2018-11-22T07:06:24.148Z

query execution end time 2018-11-22T07:06:30.249Z

在此处输入图像描述

问题:为什么 expressJS 框架需要很长时间来执行查询,而在 phpMyAdmin 中却不需要太多时间。

在此处输入图像描述

标签: mysqlnode.jsexpresssequelize.js

解决方案


这是因为 PHPMyAdmin 具有隐式LIMIT 25(在较新版本中);所以它只获取 25 行。当您的应用程序代码获取所有行时,它们都是 267121。这是要传输的相当大的数据包。您可以进一步检查此答案:https ://stackoverflow.com/a/53030883/2469308

您应该LIMIT ..在您的应用程序代码中使用。

SELECT pm.item_type,
       pm.product_type,
       pm.product_image,
       pm.product_id,
       pm.store_id,
       pm.item_type,
       pm.product_name,
       pm.product_description,
       spd.product_price
FROM product_master AS pm
JOIN store_products_detail spd ON pm.product_id = spd.product_id
WHERE spd.product_store_id IN(907)
LIMIT 25

为了提高性能,您将需要以下索引:

  • product_idproduct_master表中
  • (product_id, product_store_id)store_products_detail表中

推荐阅读