Mysql 联接将是快速的,并且始终使用索引: (select * from Children1 c join Parent p1 ON p1.id = c.parentId) union (select * from Children1 c join Parent2
联接将是快速的,并且始终使用索引:Mysql 联接将是快速的,并且始终使用索引: (select * from Children1 c join Parent p1 ON p1.id = c.parentId) union (select * from Children1 c join Parent2,mysql,query-optimization,union,subquery,Mysql,Query Optimization,Union,Subquery,联接将是快速的,并且始终使用索引: (select * from Children1 c join Parent p1 ON p1.id = c.parentId) union (select * from Children1 c join Parent2 p2 ON p2.id = c.parentId) 你可能是对的。为什么我要问这个问题:我有一个Hibernate模型,它可以生成这些查询,但比这里复杂得多。我考虑了如何根据MySQL的功能改进Hibernate对union子类继承映射的查
(select * from Children1 c join Parent p1 ON p1.id = c.parentId)
union
(select * from Children1 c join Parent2 p2 ON p2.id = c.parentId)
你可能是对的。为什么我要问这个问题:我有一个Hibernate模型,它可以生成这些查询,但比这里复杂得多。我考虑了如何根据MySQL的功能改进Hibernate对union子类继承映射的查询生成。但是由于Hibernate和MySQL在这个方向上有它们的缺点,我从那时起就放弃了努力,只是重新安排了数据库结构。谢谢你花时间。你可能是对的。为什么我要问这个问题:我有一个Hibernate模型,它可以生成这些查询,但比这里复杂得多。我考虑了如何根据MySQL的功能改进Hibernate对union子类继承映射的查询生成。但是由于Hibernate和MySQL在这个方向上有它们的缺点,我从那时起就放弃了努力,只是重新安排了数据库结构。谢谢你抽出时间。
select * from Children1 c
inner join (
select id as parentId from Parent
union
select id as parentId from Parent2
) p on p.parentId = c.parentId
+----+--------------+------------+-------+---------------+---------+---------+------+------+-----------------------------------------------------+
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
+----+--------------+------------+-------+---------------+---------+---------+------+------+-----------------------------------------------------+
| 1 | PRIMARY | NULL | NULL | NULL | NULL | NULL | NULL | NULL | Impossible WHERE noticed after reading const tables |
| 2 | DERIVED | Parent | index | NULL | PRIMARY | 4 | NULL | 1 | Using index |
| 3 | UNION | Parent2 | index | NULL | PRIMARY | 4 | NULL | 1 | Using index |
| NULL | UNION RESULT | <union2,3> | ALL | NULL | NULL | NULL | NULL | NULL | |
+----+--------------+------------+-------+---------------+---------+---------+------+------+-----------------------------------------------------+
4 rows in set (0.00 sec)
mysql> explain select * from Children1 c inner join ( select id as parentId,name from Parent union select id as parentId,name from Parent2 ) p on p.parentId = c.parentId;
+----+--------------+------------+------+---------------+------+---------+------+------+-----------------------------------------------------+
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
+----+--------------+------------+------+---------------+------+---------+------+------+-----------------------------------------------------+
| 1 | PRIMARY | NULL | NULL | NULL | NULL | NULL | NULL | NULL | Impossible WHERE noticed after reading const tables |
| 2 | DERIVED | Parent | ALL | NULL | NULL | NULL | NULL | 1 | |
| 3 | UNION | Parent2 | ALL | NULL | NULL | NULL | NULL | 1 | |
| NULL | UNION RESULT | <union2,3> | ALL | NULL | NULL | NULL | NULL | NULL | |
+----+--------------+------------+------+---------------+------+---------+------+------+-----------------------------------------------------+
4 rows in set (0.00 sec)
select * from Children1 c
inner join (
select id as parentId from Parent where Parent.id = c.parentId
union
select id as parentId from Parent2 where Parent.id = c.parentId
) p
Result: "Unknown column 'c.parentId' in 'where clause'.
select *, IFNULL(p1.name, p2.name) AS name from Children1 c
left join Parent p1 ON p1.id = c.parentId
left join Parent2 p2 ON p2.id = c.parentId
(select * from Children1 c join Parent p1 ON p1.id = c.parentId)
union
(select * from Children1 c join Parent2 p2 ON p2.id = c.parentId)