mysql - 带有 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 中却不需要太多时间。
解决方案
这是因为 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_id
在product_master
表中(product_id, product_store_id)
在store_products_detail
表中
推荐阅读
- java - 如何替换 ArrayList 的特定部分?
- php - 通过表单的 SQL 注入在 PHP 5.6 中不起作用
- oracle - 如何以 root 用户身份登录 Oracle 的 rman
- php - php将文件保存到相对路径
- amazon-web-services - 在 dynamodb 中存储具有超过 15 个属性的对象时,最好将其作为对象存储在 value 中还是将它们分布在列中?
- php - PHP 检查一个变量是否等于其他三个变量之一
- visual-studio-code - 如何使用 npm 安装 VScode 扩展?
- javascript - React,Word 加载项 - ClientAuthError:AcquireToken_In_Progress:登录调用期间出错 - 登录已在进行中
- linux - Bash grep -r -H 显示而不等到进程结束
- api - Postman 在调用 HES Impact API 时出错