Which request seems effective?

I want to know which of the two queries below is faster: -

Select s.*,
       sm.* 
  from tblStudent s
Inner Join (SELECT studentId,SUM(marks) As Sum
              from tblStudentsMarks  
           Group By studentId) as sm on s.StudentID = sm.StudentID;

... or:

Select s.studentId, 
       s.Name,
       SUM(Marks)
  From tblStudent s
Inner Join tblStudentsMarks sm On s.Studentid = sm.StudentId
  Group By s.studentId, s.Name;

EDIT: -

Request rating 1st request: http://img28.imageshack.us/img28/166/1stpicd.jpg

Assessment of the request of the second request: http://img245.imageshack.us/img245/5064/2ndpic.jpg

Thanks in advance:)

+3
source share
7 answers

.
tblStudentMarks, . , , , , , . SORT, , ...

+3

.

+4

. , SQL Server , , (, , ), 2 (join) , . , , . , .

+2

. tblStudentsMarks, , .

CREATE TABLE tblStudent(
studentId INT identity(1,1) primary key,
Name varchar(50),
filler char(2000));

create nonclustered index ix on tblStudent (StudentId, Name);

Insert into tblStudent (Name)
select top 10000 newid()
from sys.objects s1,sys.objects s2,sys.objects s3,sys.objects s4;

CREATE TABLE tblStudentsMarks(
examid int not null
,studentId INT foreign key references tblStudent not null
,marks decimal(5,2) not null
,primary key clustered (studentId, examid))

insert into tblStudentsMarks
select abs(checksum(newid())) as examid, studentId ,abs(checksum(newid()))/10000000 as marks
from tblStudent  cross join (select top 5 1 C from sys.objects) d
where studentid % 3 =0

Execution plans

* Query 1 Select s.studentId, s.Name, sm.Sum, .

+1

, , , wild-card
*.

0

, Select *. Left Join Join ( )
, ( , ). , . , .
( ) , .

0

, , , . , tblStudent.studentId, . SQL . , , TSQL , #temp. ( 18 ). 20 ( ). #temp 1 . . 1/10 , #temp.

    CREATE TABLE #sumMarks ([StudentId] [int] NOT NULL, PRIMARY KEY CLUSTERED ([StudentId] ASC), [MarksSum] [int])
    GO 

    INSERT INTO #sumMarks
    SELECT [studentId], sum(Marks) as [MarksSum] 
    FROM [tblStudentsMarks] with (nolock) 
    group by [tblStudentsMarks].[studentId] 
    ORDER by [tblStudentsMarks].[studentId];
    go
    Select s.studentId, s.Name, sm.[MarksSum]
    From tblStudent s
    Inner Join #sumMarks as sm On s.Studentid = sm.StudentId;
    go

    DROP TABLE #sumMarks;
0

Source: https://habr.com/ru/post/1762915/


All Articles