mySql在节点js中比在phpMyAdmin中慢得多

时间:2017-10-27 08:32:07

标签: mysql node.js performance

我有一个SQL查询,在使用mysql模块的nodeJs中比在phpMyAdmin中的mySql客户端运行要慢得多。

我知道构建JS对象有开销,但我不能相信这是唯一的原因:

查询:

SELECT `Groups`.`id`,
   `Groups`.`title`,
   `Groups`.`owner`,
   `Groups`.`isDefault`,
   `Groups`.`createdAt`,
   `Groups`.`updatedAt`,

   (SELECT COUNT(*)
    FROM `View`
    WHERE `View`.`groupId` = `Groups`.`id`) AS `views`,
   (SELECT COUNT(*)
    FROM `Click`
    WHERE `Click`.`groupId` = `Groups`.`id`) AS `clicks`,

   `Users`.`id` AS `Users.id`,
   `Users.UserGroup`.`id` AS `Users.UserGroup.id`,
   `Users.UserGroup`.`UserId` AS `Users.UserGroup.UserId`,
   `Users.UserGroup`.`GroupId` AS `Users.UserGroup.GroupId`,
   `Users.UserGroup`.`owner` AS `Users.UserGroup.owner`,
   `Users.UserGroup`.`createdAt` AS `Users.UserGroup.createdAt`,
   `Users.UserGroup`.`updatedAt` AS `Users.UserGroup.updatedAt`,
   `Users.Activations`.`id` AS `Users.Activations.id`,
   `Users.Datasets`.`id` AS `Users.Datasets.id`
FROM `Groups` AS `Groups`
LEFT OUTER JOIN (`UserGroup` AS `Users.UserGroup`
  INNER JOIN `User` AS `Users` ON `Users`.`id` = 
  `Users.UserGroup`.`UserId`) ON `Groups`.`id` = `Users.UserGroup`.`GroupId`
LEFT OUTER JOIN `Activation` AS `Users.Activations` ON `Users`.`id` = 
   `Users.Activations`.`userId`
LEFT OUTER JOIN `Dataset` AS `Users.Datasets` ON `Users`.`id` = 
   `Users.Datasets`.`userId`
WHERE `Groups`.`owner` = '939ab639-fd85-42ef-8c46-a7990215e26';

在带有mysql模块的nodeJs中:10秒
 在phpMyAdmin中:0,15秒

如果我测试相同的查询但没有查看和点击的计数(*):

 SELECT `Groups`.`id`,
   `Groups`.`title`,
   `Groups`.`owner`,
   `Groups`.`isDefault`,
   `Groups`.`createdAt`,
   `Groups`.`updatedAt`,

   `Users`.`id` AS `Users.id`,
   `Users.UserGroup`.`id` AS `Users.UserGroup.id`,
   `Users.UserGroup`.`UserId` AS `Users.UserGroup.UserId`,
   `Users.UserGroup`.`GroupId` AS `Users.UserGroup.GroupId`,
   `Users.UserGroup`.`owner` AS `Users.UserGroup.owner`,
   `Users.UserGroup`.`createdAt` AS `Users.UserGroup.createdAt`,
   `Users.UserGroup`.`updatedAt` AS `Users.UserGroup.updatedAt`,
   `Users.Activations`.`id` AS `Users.Activations.id`,
   `Users.Datasets`.`id` AS `Users.Datasets.id`
FROM `Groups` AS `Groups`
LEFT OUTER JOIN (`UserGroup` AS `Users.UserGroup`
  INNER JOIN `User` AS `Users` ON `Users`.`id` = 
  `Users.UserGroup`.`UserId`) ON `Groups`.`id` = `Users.UserGroup`.`GroupId`
LEFT OUTER JOIN `Activation` AS `Users.Activations` ON `Users`.`id` = 
   `Users.Activations`.`userId`
LEFT OUTER JOIN `Dataset` AS `Users.Datasets` ON `Users`.`id` = 
   `Users.Datasets`.`userId`
WHERE `Groups`.`owner` = '939ab639-fd85-42ef-8c46-a7990215e26';

在带有mysql模块的nodeJs中:0.23sec
在phpMyAdmin:0.0008sec

表格中的行数:
点击:500
查看:95k
用户组:40
用户:20
数据集:200
激活:200
Resultrows:5.7k

为什么nodeJS中的第一个查询速度较慢?通过phpMyAdmin可以看出查询本身并不慢。在使用count(*)时,如何构建对象需要花费更多的时间?要构建的对象数量应该几乎相同? 10秒真的是一个停滞不前。由于mySql客户端中的查询速度要快得多,我认为我无法通过索引或其他数据库调优来改进它。 所有具有相同DB的测试都在本地运行。

提前致谢

0 个答案:

没有答案