MySQL 约束未通过视图传递

Posted

技术标签:

【中文标题】MySQL 约束未通过视图传递【英文标题】:MySQL Restraint Not Being Passed Down Through Views 【发布时间】:2016-09-28 15:13:27 【问题描述】:

我正在使用一个具有多个相互链接的视图的系统。出于某种原因,有一个有问题的观点没有通过链条传递约束(尽管它们有一个共同的关键)。例如,这里是视觉解释语句。 Visual Explain of SQL View

如您所见,主视图基于另外两个视图(反过来,它们又基于另一组视图)。此示例中的问题视图是 ResourcePointsAndCategories(在右侧底部)。在此查询中,我通过基于名为 HostID 的列的 WHERE 子句来限制结果。 HostID 在我看来是支持的;但是,密钥没有被传递,因此,视图正在加载 23,000 行,而不是我想要的 3 行。

任何解释或更正此问题的帮助将不胜感激。谢谢!

编辑:对不起,应该从一开始就包含代码:

这里是主视图:

VIEW `test`.`resourcepointswithlookupsandcategorycountandportalpagescount` AS
SELECT 
    `resourcepointswithlookups`.`URL` AS `URL`,
    `resourcepointswithlookups`.`Format` AS `Format`,
    `resourcepointswithlookups`.`Host` AS `Host`,
    `resourcepointswithlookups`.`ResourceID` AS `ResourceID`,
    `resourcepointswithlookups`.`HostID` AS `HostID`,
    `resourcepointswithlookups`.`PermitID` AS `PermitID`,
    `resourcepointswithlookups`.`ResourceTitle` AS `ResourceTitle`,
    `resourcepointswithlookups`.`FormatID` AS `FormatID`,
    `resourcepointswithlookups`.`TypeID` AS `TypeID`,
    `resourcepointswithlookups`.`Notes` AS `Notes`,
    `resourcepointswithlookups`.`Description` AS `Description`,
    `resourcepointswithlookups`.`ReferenceFirstName` AS `ReferenceFirstName`,
    `resourcepointswithlookups`.`ReferenceLastName` AS `ReferenceLastName`,
    `resourcepointswithlookups`.`ReferenceEmail` AS `ReferenceEmail`,
    `resourcepointswithlookups`.`ReferencePermission` AS `ReferencePermission`,
    `resourcepointswithlookups`.`ReferenceComment` AS `ReferenceComment`,
    `resourcepointswithlookups`.`CreateDate` AS `CreateDate`,
    `resourcepointswithlookups`.`CreateBy` AS `CreateBy`,
    `resourcepointswithlookups`.`LastEditDate` AS `LastEditDate`,
    `resourcepointswithlookups`.`LastEditBy` AS `LastEditBy`,
    `resourcepointswithlookups`.`LastReview` AS `LastReview`,
    `resourcepointswithlookups`.`LastReviewBy` AS `LastReviewBy`,
    `resourcepointswithlookups`.`Type` AS `Type`,
    `resourcepointswithlookups`.`LibraryURL` AS `LibraryURL`,
    `resourcepointswithlookups`.`TypeCollection` AS `TypeCollection`,
    `resourcepointsandcategoriescount`.`CategoryCount` AS `CategoryCount`,
    `portalpagecountwithresourcepointsandportaltitle`.`PortalPageCount` AS `PortalPageCount`,
    `resourcepointswithlookups`.`NormFileStatus` AS `NormFileStatus`,
    `resourcepointswithlookups`.`NormFileStatusDate` AS `NormFileStatusDate`,
    `resourcepointswithlookups`.`FileSystemStatus` AS `FileSystemStatus`,
    `resourcepointswithlookups`.`FileSystemStatusDate` AS `FileSystemStatusDate`,
    `resourcepointswithlookups`.`LanguageName` AS `LanguageName`,
    `resourcepointswithlookups`.`LanguageCode` AS `LanguageCode`
FROM
    ((`test`.`resourcepointswithlookups`
    LEFT JOIN `test`.`portalpagecountwithresourcepointsandportaltitle` ON ((`resourcepointswithlookups`.`ResourceID` = `portalpagecountwithresourcepointsandportaltitle`.`ResourcePointID`)))
    LEFT JOIN `test`.`resourcepointsandcategoriescount` ON ((`resourcepointswithlookups`.`ResourceID` = `resourcepointsandcategoriescount`.`ResourceID`)))

这是问题视图的代码:

VIEW `test`.`resourcepointsandcategoriescount` AS
SELECT 
    `resourcepointsandcategories`.`ResourceID` AS `ResourceID`,
    `resourcepointsandcategories`.`HostID` AS `HostID`,
    COUNT(`resourcepointsandcategories`.`CategoryID`) AS `CategoryCount`
FROM
    `test`.`resourcepointsandcategories`
GROUP BY `resourcepointsandcategories`.`ResourceID`

最终,问题视图所基于的视图(运行良好):

VIEW `test`.`resourcepointsandcategories` AS
SELECT 
    `test`.`resourcepoints`.`ResourceID` AS `ResourceID`,
    `test`.`resourcepoints`.`HostID` AS `HostID`,
    `test`.`lkupegcategories`.`Category` AS `Category`,
    `test`.`resourcepointcategories`.`CategoryID` AS `CategoryID`
FROM
    ((`test`.`resourcepointcategories`
    JOIN `test`.`resourcepoints` ON ((`test`.`resourcepointcategories`.`ResourceID` = `test`.`resourcepoints`.`ResourceID`)))
    LEFT JOIN `test`.`lkupegcategories` ON ((`test`.`lkupegcategories`.`eGCategoryID` = `test`.`resourcepointcategories`.`CategoryID`)))

我正在调用的查询是:

 SELECT * FROM test.resourcepointswithlookupsandcategorycountandportalpagescount WHERE HostID = 4532;

【问题讨论】:

请向我们展示您的一些代码,以便我们提供适当的帮助 添加代码!谢谢! 请提供EXPLAIN SELECT ... 【参考方案1】:

如果我正确阅读了您的观点,您将过滤HostID = 4532 上的第一个(大)视图,而不是过滤其他两个视图。

也许ON 子句需要包含AND res....HostID = res...HostID

【讨论】:

以上是关于MySQL 约束未通过视图传递的主要内容,如果未能解决你的问题,请参考以下文章

自动布局约束未正确放置容器视图

模板未显示通过传递参数使用视图函数从 sqlite 获取的数据(为空白)

MySQL笔记--- 创建表;插入,修改,删除数据;主键,外键约束;事务;索引;视图;三范式;

数据未传递给视图

自定义布局未使用约束布局在列表视图中居中

宽度约束更新后视图未重绘