MySQL 5.0.75-0ubuntu10.2 中我有一个固定的表格布局:

带有id的表 parent 带有id的表 parent2 带有parentId的表 children1

CREATE TABLE  `Parent` (
  `id` int(11) NOT NULL auto_increment,
  `name` varchar(200) default NULL,
  PRIMARY KEY  (`id`)
) ENGINE=InnoDB
CREATE TABLE  `Parent2` (
  `id` int(11) NOT NULL auto_increment,
  `name` varchar(200) default NULL,
  PRIMARY KEY  (`id`)
) ENGINE=InnoDB
CREATE TABLE  `Children1` (
  `id` int(11) NOT NULL auto_increment,
  `parentId` int(11) NOT NULL,
  PRIMARY KEY  (`id`),
  KEY `parent` (`parentId`)
) ENGINE=InnoDB

子项在其中一个表 Parent Parent2 中有一个父项。当我需要一个孩子时,我会使用这样的查询:

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)

任何人都可以解释为什么(PRIMARY)指数不再使用了吗?如果可能的话,是否有解决此问题的方法而无需更改数据库布局?

谢谢!

有帮助吗?

解决方案

我认为一旦你开始在派生查询中拉出多个列,优化器就会崩溃,因为它可能需要在union上转换数据类型(不是在这种情况下,而是一般情况下)。这也可能是因为您的查询本质上想要成为相关的派生子查询,这是不可能的(来自 dev.mysql.com ):

  

FROM子句中的子查询不能是相关子查询,除非在JOIN操作的ON子句中使用。

您要做的事情(但无效)是:

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'.

你有没有理由不喜欢两个左连接和IFNULLs:

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)

其他提示

我的第一个想法是插入一个“重要”的表中的记录数,并使用ANALYZE TABLE更新统计信息。使用完整扫描而不是通过索引读取包含4条记录的表总是更快! 此外,您可以尝试使用USE INDEX来强制使用索引并查看计划的更改方式。

我还建议您阅读本文档并查看哪些位相关 MYSQL ::使用EXPLAIN优化查询

这篇文章也很有用 7种说服方式MySQL使用正确的索引

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top