加入两个查询时LINQ查询丢失选择

时间:2017-07-18 10:25:52

标签: c# mysql linq join

只是稍微设置上下文,我试图使用mysql查询使用Late row lookup,如本文所示

https://explainextended.com/2009/10/23/mysql-order-by-limit-performance-late-row-lookups/

但这是另一天的故事,但想法是你在表上进行密钥搜索,然后将其加入到整个表中以强制进行后期行查找,问题来自我的LINQ查询联合起来。

- 关键搜索查询 -

致电代码

IQueryable<int> keySearch = _defaultQueryFactory.Load(ContextEnums.ClientContext, MapEntityToDTO(), whereStatement, clientID).OrderBy(orderBy).Skip(startRow).Take(pageSize).Select(x => x.ID);

结果查询

SELECT
`Extent1`.`Sys_InvoiceID`
FROM `tblinvoice` AS `Extent1`
 WHERE 3 = `Extent1`.`FK_StatusID`
 ORDER BY 
`Extent1`.`InvoiceDate` ASC LIMIT 0,430

- 全桌搜索 -

致电代码

IQueryable<InvoiceDTOModel> tableSearch = _defaultQueryFactory.Load(ContextEnums.ClientContext, MapEntityToDTO(), null, clientID, true).OrderBy(orderBy);

结果查询

   SELECT
    `Extent1`.`ID`, 
    `Extent1`.`C1`, 
    `Extent1`.`C2`, 
    `Extent1`.`C3`, 
    `Extent1`.`C4`, 
    `Extent1`.`C5`, 
    `Extent1`.`C6`, 
    `Extent2`.`SID`, 
    `Extent2`.`S1, 
    `Extent2`.`S2`, 
    `Extent2`.`S3`, 
    `Extent3`.`EID`, 
    `Extent3`.`E1`, 
    `Extent4`.`DID`, 
    `Extent4`.`D1`, 
    `Extent4`.`D2`, 
    `Extent4`.`D3`, 
    `Extent4`.`D4`, 
    `Extent4`.`D5`    
    FROM `tbl1` AS `Extent1` INNER JOIN `tbl2` AS `Extent2` ON `Extent1`.`SID` = `Extent2`.`SID` INNER JOIN `tbl3` AS `Extent3` ON `Extent1`.`EID` = `Extent3`.`EID` LEFT OUTER JOIN `tbl4` AS `Extent4` ON `Extent1`.`ID` = `Extent4`.`DID`
     ORDER BY 
    `Extent1`.`C4` ASC

- 加入两个人 -

致电代码

keySearch.Join(tableSearch, key => key, table => table.ID, (key, table) => table).OrderBy(orderBy).ToListAsync();

结果查询

 SELECT
    `Join3`.`ID`, 
    `Join3`.`C1`, 
    `Join3`.`C1`, 
    `Join3`.`C1`, 
    `Join3`.`C1`, 
    `Join3`.`C1`, 
    `Join3`.`C1`, 
    `Join3`.`SID`, 
    `Join3`.`S1, 
    `Join3`.`S2`, 
    `Join3`.`S3`, 
    `Join3`.`EID`, 
    `Join3`.`E1`, 
    `Join3`.`DID`, 
    `Join3`.`D1`, 
    `Join3`.`D2`, 
    `Join3`.`D3`, 
    `Join3`.`D4`, 
    `Join3`.`D5`   
    FROM ( 
        `Extent1`.`ID`, 
        `Extent1`.`C1`, 
        `Extent1`.`C2`, 
        `Extent1`.`C3`, 
        `Extent1`.`C4`, 
        `Extent1`.`C5`, 
        `Extent1`.`C6` 
    FROM `tblinvoice` AS `Extent1`
     WHERE 3 = `Extent1`.`EID`
     ORDER BY 
    `Extent1`.`C4` ASC LIMIT 0,430) AS `Limit1` INNER JOIN (SELECT
    `Extent1`.`ID`, 
    `Extent1`.`C1`, 
    `Extent1`.`C2`, 
    `Extent1`.`C3`, 
    `Extent1`.`C4`, 
    `Extent1`.`C5`, 
    `Extent1`.`C6`, 
    `Extent2`.`SID`, 
    `Extent2`.`S1, 
    `Extent2`.`S2`, 
    `Extent2`.`S3`, 
    `Extent3`.`EID`, 
    `Extent3`.`E1`, 
    `Extent4`.`DID`, 
    `Extent4`.`D1`, 
    `Extent4`.`D2`, 
    `Extent4`.`D3`, 
    `Extent4`.`D4`, 
    `Extent4`.`D5`    
    FROM `tbl1` AS `Extent2` INNER JOIN `tbl2` AS `Extent3` ON `Extent2`.`SID` = `Extent3`.`SID` INNER JOIN `tblstatus` AS `Extent4` ON `Extent2`.`EID` = `Extent4`.`EID` LEFT OUTER JOIN `tbl3` AS `Extent5` ON `Extent2`.`ID` = `Extent5`.`DID`) AS `Join3` ON `Limit1`.`ID` = `Join3`.`ID`
     ORDER BY 
    `Join3`.`C4` ASC

基本上内部选择带回来

FROM ( 
            `Extent1`.`ID`, 
            `Extent1`.`C1`, 
            `Extent1`.`C2`, 
            `Extent1`.`C3`, 
            `Extent1`.`C4`, 
            `Extent1`.`C5`, 
            `Extent1`.`C6` 
        FROM `tblinvoice` AS `Extent1`
         WHERE 3 = `Extent1`.`EID`
         ORDER BY 
        `Extent1`.`C4` ASC LIMIT 0,430) AS `Limit1`

而不是

FROM (                
           `Extent1`.`ID`,                
            FROM `tblinvoice` AS `Extent1`
             WHERE 3 = `Extent1`.`EID`
             ORDER BY 
            `Extent1`.`C4` ASC LIMIT 0,430) AS `Limit1`

- 注 -

实际查询选择了大约15列,我只是将它缩短到这个例子,它随着数据集的大小增加而对搜索产生影响,它不应该选择所有字段但是我怀疑我的加入是错误的。

非常感谢任何帮助。

0 个答案:

没有答案