依靠 LINQ 联合

发布于 2024-08-26 17:09:22 字数 1510 浏览 6 评论 0原文

我有这个链接语句:

List<UserGroup> domains = UserRepository.Instance.UserIsAdminOf(currentUser.User_ID);

query = (from doc in _db.Repository<Document>()
         join uug in _db.Repository<User_UserGroup>() on doc.DocumentFrom equals uug.User_ID
         where domains.Contains(uug.UserGroup)
         select doc)
.Union(from doc in _db.Repository<Document>()
       join uug in _db.Repository<User_UserGroup>() on doc.DocumentTo equals uug.User_ID
       where domains.Contains(uug.UserGroup)
       select doc);

运行此语句不会导致任何问题。但是当我想计算结果集时,查询突然运行得很慢。

totalRecords = query.Count();

这个查询的结果是:

SELECT COUNT([t5].[DocumentID])
FROM (
    SELECT [t4].[DocumentID], [t4].[DocumentFrom], [t4].[DocumentTo]
    FROM (
        SELECT [t0].[DocumentID], [t0].[DocumentFrom], [t0].[DocumentTo
        FROM [dbo].[Document] AS [t0]
        INNER JOIN [dbo].[User_UserGroup] AS [t1] ON [t0].[DocumentFrom] = [t1].[User_ID]
        WHERE ([t1].[UserGroupID] = 2) OR ([t1].[UserGroupID] = 3) OR ([t1].[UserGroupID] = 6)
        UNION
        SELECT [t2].[DocumentID], [t2].[DocumentFrom], [t2].[DocumentTo]
        FROM [dbo].[Document] AS [t2]
        INNER JOIN [dbo].[User_UserGroup] AS [t3] ON [t2].[DocumentTo] = [t3].[User_ID]
        WHERE ([t3].[UserGroupID] = 2) OR ([t3].[UserGroupID] = 3) OR ([t3].[UserGroupID] = 6)
        ) AS [t4]
    ) AS [t5]

任何人都可以帮助我提高计数查询的速度吗?

提前致谢!

I'm having this link statement:

List<UserGroup> domains = UserRepository.Instance.UserIsAdminOf(currentUser.User_ID);

query = (from doc in _db.Repository<Document>()
         join uug in _db.Repository<User_UserGroup>() on doc.DocumentFrom equals uug.User_ID
         where domains.Contains(uug.UserGroup)
         select doc)
.Union(from doc in _db.Repository<Document>()
       join uug in _db.Repository<User_UserGroup>() on doc.DocumentTo equals uug.User_ID
       where domains.Contains(uug.UserGroup)
       select doc);

Running this statement doesn't cause any problems. But when I want to count the resultset the query suddenly runs quite slow.

totalRecords = query.Count();

The result of this query is :

SELECT COUNT([t5].[DocumentID])
FROM (
    SELECT [t4].[DocumentID], [t4].[DocumentFrom], [t4].[DocumentTo]
    FROM (
        SELECT [t0].[DocumentID], [t0].[DocumentFrom], [t0].[DocumentTo
        FROM [dbo].[Document] AS [t0]
        INNER JOIN [dbo].[User_UserGroup] AS [t1] ON [t0].[DocumentFrom] = [t1].[User_ID]
        WHERE ([t1].[UserGroupID] = 2) OR ([t1].[UserGroupID] = 3) OR ([t1].[UserGroupID] = 6)
        UNION
        SELECT [t2].[DocumentID], [t2].[DocumentFrom], [t2].[DocumentTo]
        FROM [dbo].[Document] AS [t2]
        INNER JOIN [dbo].[User_UserGroup] AS [t3] ON [t2].[DocumentTo] = [t3].[User_ID]
        WHERE ([t3].[UserGroupID] = 2) OR ([t3].[UserGroupID] = 3) OR ([t3].[UserGroupID] = 6)
        ) AS [t4]
    ) AS [t5]

Can anyone help me to improve the speed of the count query?

Thanks in advance!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

刘备忘录 2024-09-02 17:09:22

请确保在 [Document].[DocumentFrom][Document].[DocumentTo][User_UserGroup].[UserGroupID] 上建立索引。

通过阅读您的查询,我可以看到您在不同的条件下加入同一个表。
如果您不需要重复的结果,您可以看看这个替代方案:

var query = from doc in _db.Repository<Document>()
            from uug in _db.Repository<User_UserGroup>() 
            //join wether DocumentFrom or DocumentTo equals User_ID
            where (
                     (doc.DocumentFrom == uug.User_ID) ||
                     (doc.DocumentTo == uug.User_ID)
                  ) &&
                  //same check on this                      
                  domains.Contains(uug.UserGroup)
            select doc;

//execute it
var list = query.ToList();

编辑:如果我没有记错的话.Union会在两个查询,所以我的替代方案是一个有效的查询。
如果您确实想合并两个结果集,您可能需要查看 .Concat

Be sure to have indexes on [Document].[DocumentFrom], [Document].[DocumentTo] and [User_UserGroup].[UserGroupID].

By reading your query I can see you join the same table on different conditions.
If you don't need duplicate results you might look at this alternative:

var query = from doc in _db.Repository<Document>()
            from uug in _db.Repository<User_UserGroup>() 
            //join wether DocumentFrom or DocumentTo equals User_ID
            where (
                     (doc.DocumentFrom == uug.User_ID) ||
                     (doc.DocumentTo == uug.User_ID)
                  ) &&
                  //same check on this                      
                  domains.Contains(uug.UserGroup)
            select doc;

//execute it
var list = query.ToList();

EDIT: If I am not mistaken .Union picks distinct elements between the two queries, so my alternative IS a valid one.
If you truly want to merge the two result sets you might want to look at .Concat.

神妖 2024-09-02 17:09:22

摆脱你的工会并尝试这样的事情。

query = (来自 _db.Repository().Where(x=> _db.Repository().Any(y=>y.User_ID==x.DocumentFrom ||y) 中的文档.User_ID==x.DocumentTo ))

抱歉,我不能保证它可以编译...但基本上使用Where any any 来完成你的工作。

Get rid of your unions and try something like this.

query = (from doc in _db.Repository<Document>().Where(x=> _db.Repository<User_UserGroup>().Any(y=>y.User_ID==x.DocumentFrom ||y.User_ID==x.DocumentTo ))

Sorry i can't guarantee it compiles... but basically use Where any any to do your job.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文