mysql-5.7 - Mysql 慢速多连接查询 - 5.7 版
问题描述
我有以下查询:
SELECT *
FROM `ResearchEntity` AS `ResearchEntity`
LEFT OUTER JOIN `UserEntity` AS `createdBy` ON `ResearchEntity`.`createdById` = `createdBy`.`id`
LEFT OUTER JOIN `Item1Entity` AS `item1` ON `ResearchEntity`.`id` = `Item1`.`researchId`
LEFT OUTER JOIN `Item2Entity` AS `item2` ON `ResearchEntity`.`id` = `Item2`.`researchId`
LEFT OUTER JOIN `Item3Entity` AS `item3` ON `ResearchEntity`.`id` = `Item3`.`researchId`
LEFT OUTER JOIN `Item4Entity` AS `item4` ON `ResearchEntity`.`id` = `Item4`.`researchId`
LEFT OUTER JOIN `Item5Entity` AS `item5` ON `ResearchEntity`.`id` = `Item5`.`researchId`
LEFT OUTER JOIN `Item6Entity` AS `item6` ON `ResearchEntity`.`id` = `Item6`.`researchId`
LEFT OUTER JOIN `Item7Entity` AS `item7` ON `ResearchEntity`.`id` = `Item7`.`researchId`
LEFT OUTER JOIN `Item8Entity` AS `item8` ON `ResearchEntity`.`id` = `Item8`.`researchId`
LEFT OUTER JOIN `Item9Entity` AS `item9` ON `ResearchEntity`.`id` = `Item9`.`researchId`
LEFT OUTER JOIN `Item10Entity` AS `item10` ON `ResearchEntity`.`id` = `Item10`.`researchId`
ORDER BY `ResearchEntity`.`id` DESC
LIMIT 20, 40;
UserEntity
有 4 条记录
Item*Entity
,每条有 15 条记录
当我删除第一个连接时,UserEntity
连接查询运行得非常快,但它在 50 秒内运行。
查询是使用 ORM 在运行时动态构建的。
为什么UserEntity
会造成这么多麻烦?
谢谢
解决方案
尝试更改您的 select * 以从每个实体中选择特定字段,您的查询负载将减少并且运行时间应该更快。
就像是
SELECT ResearchEntity.field1 as ReField1, item1.name as Item1Name, ...
推荐阅读
- wpf - 如何设置数据表列的宽度以及如何在某些条件下设置数据表列的颜色
- java - JFrame.setExtendedState() 在 Mac 上无法正常工作
- excel - 如何发送和接收 UDP 消息
- reactjs - 在嵌套函数中使用钩子
- java - android自定义用户界面
- c++ - 当数据存储为 uint8_t 的向量时,如何传递“char* 数据”?
- python - read_mrz 错误:在模式“i”中找不到读取指定文件的格式
- linux - 将大行拆分为较小的行并在换行符的开头插入特殊字符
- javascript - 通过 JavaScript 中的自定义函数对数组进行排序
- flutter - 如何修复列中的 ListView 并将 RefreshIndicator 添加到 ListView