MrPlough69
IS-IT--Management
Hi All,
I’ve got the following stored procedure which returns a list of football players along with some simple stats (running on SQL Server 2005):
The query plan doesn’t have any table/index scans, so just wondered if it would be possible to improve the SQL (the above is the best performing that I’ve been able to come up with).
Many thanks in advance.
I’ve got the following stored procedure which returns a list of football players along with some simple stats (running on SQL Server 2005):
Code:
CREATE PROCEDURE GetTeamLineup
@UserId INT,
@FixtureId INT,
@TeamId INT
AS
BEGIN
-- SET NOCOUNT ON added to prevent extra result sets from
-- interfering with SELECT statements.
SET NOCOUNT ON;
SELECT mt.Surname,
COALESCE( ulr.lr_Rating, tlr.lr_Rating, mt.MatchRating ),
( SELECT Count( PlayRef )
FROM Scorers
WHERE PlayRef = mt.RefNo
AND FixId = MatchIdNo ) AS GoalsScored,
( SELECT Count( PlayRef )
FROM Bookings
WHERE PlayRef = mt.RefNo
AND FixId = MatchIdNo
AND BookingType = 'Y') AS YellowCards,
( SELECT Count( PlayRef )
FROM Bookings
WHERE PlayRef = mt.RefNo
AND FixId = MatchIdNo
AND BookingType IN ( 'YR', 'R' ) ) AS RedCards,
mt.RefNo
FROM MATCHmagTeams mt
INNER JOIN TeamSquads ts ON ts.RefNo = mt.RefNo
INNER JOIN Teams te ON te.TeamId = mt.TeamId
LEFT JOIN BlockLineups ubl
ON ubl.bli_FixtureId = @FixtureId AND ubl.bli_UserId = @UserId
LEFT JOIN LineupRatings ulr
ON ulr.lr_BlockLineupId = ubl.bli_UniqId AND ulr.lr_PlayerId = mt.RefNo
LEFT JOIN BlockLineups tbl
ON tbl.bli_FixtureId = @FixtureId AND tbl.bli_UserId = dbo.GetUserTemplateUserId( @UserId )
LEFT JOIN LineupRatings tlr
ON tlr.lr_BlockLineupId = tbl.bli_UniqId AND tlr.lr_PlayerId = mt.RefNo
WHERE MatchIdNo = @FixtureId
AND te.TeamId = @TeamId
ORDER BY mt.Surname
END
The query plan doesn’t have any table/index scans, so just wondered if it would be possible to improve the SQL (the above is the best performing that I’ve been able to come up with).
Many thanks in advance.