MySQL约束不通过视图传递

时间:2016-09-28 15:13:28

标签: mysql database performance views where

我正在使用一个系统,它有多个视图链接在一起。出于某种原因,存在一个有问题的观点,即没有通过链条向下传递约束(尽管它们具有共同的密钥)。例如,这是视觉解释声明。 Visual Explain of SQL View

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

非常感谢任何帮助解释或纠正这个问题。谢谢!

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

这是主视图:

  $('.myDiv').layout({
    resizeWhileDragging: true,
    sizable: false,
    animatePaneSizing: true,
    fxSpeed: 'slow',
    west__spacing_closed: 0,
    west__spacing_open: 0,
    north__spacing_closed: 0,
    north__spacing_open: 0,

  });

这是问题视图的代码:

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`)))

1 个答案:

答案 0 :(得分:0)

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

也许ON条款需要包含AND res....HostID = res...HostID