所以我有一个MS SQL数据库,我一直专门使用LINQ to Entities,因为我理解LINQ,而不是SQL。我正在考虑学习更多SQL以及可能使用存储过程以加快速度的想法。但是,我不清楚如何在SQL中编写以下内容,因此我正在研究LinqPad生成的内容作为起点。
以下查询根据标题在过去7天内在特定“频道”或频道中“排队”的次数以及该标题的前3名“演员”根据其订单成功获得最受欢迎的标题
var dateStart = DateTime.Now.AddDays(-7);
var dateNow = DateTime.Now;
var channels = new[] { 1 };
var query = (from p in TitleQueues
where p.QueueTime >= dateStart
&& p.QueueTime <= dateNow
group p by p.TitleId
into grouped
from t in Titles
let cast = (from tc in t.TitlePeoples
from td in tc.TitlePersonTitlePeopleDepartments
where td.Department.Type == "Actors"
orderby tc.Order
select new { tc.Person.Name, tc.Person.Id }).Take(3)
where t.Id == grouped.Key
from g in t.TitleSources
where
channels.Contains(g.TitleProviderId) && g.AvailableFrom <= dateNow &&
g.AvailableTo >= dateNow
&& t.Rank != null
orderby grouped.Count() descending , t.Rank
select new {
t.Id,
t.Name,
t.ReleaseYear,
t.ShortSynopsis,
t.TitleTypeId,
t.MPAARating,
t.Runtime,
t.IMDbTop250Rank,
t.CombinedRating,
cast
}).Take(10);
query.Dump();
这是LinqPad根据上面的Linq查询生成的SQL:
-- Region Parameters
DECLARE @p0 DateTime2 = '2011-12-28 12:13:29.4306979'
DECLARE @p1 DateTime2 = '2012-01-04 12:13:29.4306979'
DECLARE @p2 Int = 1
DECLARE @p3 DateTime2 = '2012-01-04 12:13:29.4306979'
DECLARE @p4 DateTime2 = '2012-01-04 12:13:29.4306979'
-- EndRegion
SELECT TOP (10) [t2].[Id], [t2].[Name], [t2].[ReleaseYear], [t2].[ShortSynopsis], [t2].[TitleTypeId], [t2].[MPAARating], [t2].[Runtime], [t2].[IMDbTop250Rank], [t2].[CombinedRating]
FROM (
SELECT [t0].[TitleId]
FROM [TitleQueues] AS [t0]
WHERE ([t0].[QueueTime] >= @p0) AND ([t0].[QueueTime] <= @p1)
GROUP BY [t0].[TitleId]
) AS [t1]
CROSS JOIN [Titles] AS [t2]
CROSS JOIN [TitleSources] AS [t3]
WHERE ([t3].[TitleProviderId] IN (@p2)) AND ([t3].[AvailableFrom] <= @p3) AND ([t3].[AvailableTo] >= @p4) AND ([t2].[Rank] IS NOT NULL) AND ([t2].[Id] = [t1].[TitleId]) AND ([t3].[TitleId] = [t2].[Id])
ORDER BY (
SELECT COUNT(*)
FROM [TitleQueues] AS [t4]
WHERE ([t1].[TitleId] = [t4].[TitleId]) AND ([t4].[QueueTime] >= @p0) AND ([t4].[QueueTime] <= @p1)
) DESC, [t2].[Rank]
GO
-- Region Parameters
DECLARE @p0 VarChar(1000) = 'Actors'
DECLARE @x1 Int = 130323
-- EndRegion
SELECT TOP (3) [t3].[Name], [t3].[Id]
FROM [TitlePeople] AS [t0]
CROSS JOIN [TitlePeopleDepartments] AS [t1]
INNER JOIN [Departments] AS [t2] ON [t2].[Id] = [t1].[DepartmentId]
INNER JOIN [People] AS [t3] ON [t3].[Id] = [t0].[PersonId]
WHERE ([t2].[Type] = @p0) AND ([t0].[TitleId] = @x1) AND ([t1].[TitlePersonId] = [t0].[Id])
ORDER BY [t0].[Order]
GO
-- Region Parameters
DECLARE @p0 VarChar(1000) = 'Actors'
DECLARE @x1 Int = 127948
-- EndRegion
SELECT TOP (3) [t3].[Name], [t3].[Id]
FROM [TitlePeople] AS [t0]
CROSS JOIN [TitlePeopleDepartments] AS [t1]
INNER JOIN [Departments] AS [t2] ON [t2].[Id] = [t1].[DepartmentId]
INNER JOIN [People] AS [t3] ON [t3].[Id] = [t0].[PersonId]
WHERE ([t2].[Type] = @p0) AND ([t0].[TitleId] = @x1) AND ([t1].[TitlePersonId] = [t0].[Id])
ORDER BY [t0].[Order]
GO
-- Region Parameters
DECLARE @p0 VarChar(1000) = 'Actors'
DECLARE @x1 Int = 90578
-- EndRegion
SELECT TOP (3) [t3].[Name], [t3].[Id]
FROM [TitlePeople] AS [t0]
CROSS JOIN [TitlePeopleDepartments] AS [t1]
INNER JOIN [Departments] AS [t2] ON [t2].[Id] = [t1].[DepartmentId]
INNER JOIN [People] AS [t3] ON [t3].[Id] = [t0].[PersonId]
WHERE ([t2].[Type] = @p0) AND ([t0].[TitleId] = @x1) AND ([t1].[TitlePersonId] = [t0].[Id])
ORDER BY [t0].[Order]
GO
-- Region Parameters
DECLARE @p0 VarChar(1000) = 'Actors'
DECLARE @x1 Int = 129887
-- EndRegion
SELECT TOP (3) [t3].[Name], [t3].[Id]
FROM [TitlePeople] AS [t0]
CROSS JOIN [TitlePeopleDepartments] AS [t1]
INNER JOIN [Departments] AS [t2] ON [t2].[Id] = [t1].[DepartmentId]
INNER JOIN [People] AS [t3] ON [t3].[Id] = [t0].[PersonId]
WHERE ([t2].[Type] = @p0) AND ([t0].[TitleId] = @x1) AND ([t1].[TitlePersonId] = [t0].[Id])
ORDER BY [t0].[Order]
GO
-- Region Parameters
DECLARE @p0 VarChar(1000) = 'Actors'
DECLARE @x1 Int = 130546
-- EndRegion
SELECT TOP (3) [t3].[Name], [t3].[Id]
FROM [TitlePeople] AS [t0]
CROSS JOIN [TitlePeopleDepartments] AS [t1]
INNER JOIN [Departments] AS [t2] ON [t2].[Id] = [t1].[DepartmentId]
INNER JOIN [People] AS [t3] ON [t3].[Id] = [t0].[PersonId]
WHERE ([t2].[Type] = @p0) AND ([t0].[TitleId] = @x1) AND ([t1].[TitlePersonId] = [t0].[Id])
ORDER BY [t0].[Order]
GO
-- Region Parameters
DECLARE @p0 VarChar(1000) = 'Actors'
DECLARE @x1 Int = 121981
-- EndRegion
SELECT TOP (3) [t3].[Name], [t3].[Id]
FROM [TitlePeople] AS [t0]
CROSS JOIN [TitlePeopleDepartments] AS [t1]
INNER JOIN [Departments] AS [t2] ON [t2].[Id] = [t1].[DepartmentId]
INNER JOIN [People] AS [t3] ON [t3].[Id] = [t0].[PersonId]
WHERE ([t2].[Type] = @p0) AND ([t0].[TitleId] = @x1) AND ([t1].[TitlePersonId] = [t0].[Id])
ORDER BY [t0].[Order]
GO
-- Region Parameters
DECLARE @p0 VarChar(1000) = 'Actors'
DECLARE @x1 Int = 121957
-- EndRegion
SELECT TOP (3) [t3].[Name], [t3].[Id]
FROM [TitlePeople] AS [t0]
CROSS JOIN [TitlePeopleDepartments] AS [t1]
INNER JOIN [Departments] AS [t2] ON [t2].[Id] = [t1].[DepartmentId]
INNER JOIN [People] AS [t3] ON [t3].[Id] = [t0].[PersonId]
WHERE ([t2].[Type] = @p0) AND ([t0].[TitleId] = @x1) AND ([t1].[TitlePersonId] = [t0].[Id])
ORDER BY [t0].[Order]
GO
-- Region Parameters
DECLARE @p0 VarChar(1000) = 'Actors'
DECLARE @x1 Int = 125377
-- EndRegion
SELECT TOP (3) [t3].[Name], [t3].[Id]
FROM [TitlePeople] AS [t0]
CROSS JOIN [TitlePeopleDepartments] AS [t1]
INNER JOIN [Departments] AS [t2] ON [t2].[Id] = [t1].[DepartmentId]
INNER JOIN [People] AS [t3] ON [t3].[Id] = [t0].[PersonId]
WHERE ([t2].[Type] = @p0) AND ([t0].[TitleId] = @x1) AND ([t1].[TitlePersonId] = [t0].[Id])
ORDER BY [t0].[Order]
GO
-- Region Parameters
DECLARE @p0 VarChar(1000) = 'Actors'
DECLARE @x1 Int = 91239
-- EndRegion
SELECT TOP (3) [t3].[Name], [t3].[Id]
FROM [TitlePeople] AS [t0]
CROSS JOIN [TitlePeopleDepartments] AS [t1]
INNER JOIN [Departments] AS [t2] ON [t2].[Id] = [t1].[DepartmentId]
INNER JOIN [People] AS [t3] ON [t3].[Id] = [t0].[PersonId]
WHERE ([t2].[Type] = @p0) AND ([t0].[TitleId] = @x1) AND ([t1].[TitlePersonId] = [t0].[Id])
ORDER BY [t0].[Order]
GO
-- Region Parameters
DECLARE @p0 VarChar(1000) = 'Actors'
DECLARE @x1 Int = 121903
-- EndRegion
SELECT TOP (3) [t3].[Name], [t3].[Id]
FROM [TitlePeople] AS [t0]
CROSS JOIN [TitlePeopleDepartments] AS [t1]
INNER JOIN [Departments] AS [t2] ON [t2].[Id] = [t1].[DepartmentId]
INNER JOIN [People] AS [t3] ON [t3].[Id] = [t0].[PersonId]
WHERE ([t2].[Type] = @p0) AND ([t0].[TitleId] = @x1) AND ([t1].[TitlePersonId] = [t0].[Id])
ORDER BY [t0].[Order]
这是存储过程的一个很好的起点吗?如果你为此写一个proc,它会是什么样的?
答案 0 :(得分:0)
如果可以,请尝试使用SQL事件探查器观察发送到数据库的实际SQL。 也许这将提供一个更好的起点。