依靠 LINQ 联合
我有这个链接语句:
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
请确保在
[Document].[DocumentFrom]
、[Document].[DocumentTo]
和[User_UserGroup].[UserGroupID]
上建立索引。通过阅读您的查询,我可以看到您在不同的条件下加入同一个表。
如果您不需要重复的结果,您可以看看这个替代方案:编辑:如果我没有记错的话
.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 resultsyou might look at this alternative: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
.摆脱你的工会并尝试这样的事情。
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.