2016-09-28 52 views
-1

我正在使用一个系统,该系统具有彼此链接的多个视图。出于某种原因,有一个问题的观点并没有通过链条(尽管它们有一个共同的关键)通过限制。例如,这里是视觉解释说明。 Visual Explain of SQL ViewMySQL约束不能通过视图传递

正如您所看到的,主视图基于另外两个视图(而这些视图又基于另一组视图)。这个例子中的问题视图是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; 
+0

请向我们展示一些您的代码,以便我们可以提供帮助 – legrandviking

+0

添加代码!谢谢! –

+0

请提供'EXPLAIN SELECT ...' –

回答

0

如果我读了你的观点正确,您正在筛选上HostID = 4532第一(大)的看法,但它并不过滤其他两种观点。

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