学生表:student(stuentId,studentName,height,weight,telphone)
成绩表:grade(studentId,,teacherId,lessonId,lessonName)现在有一个需求:在页面的一个输入框输入studentId 或者 studentName,显示出成绩表的teacherId,lessonId,lessonName三列。写法1:select teacherId,lessonId,lessonName from student a ,grade b where a.studentName = '34514622' or b.stuentId='34514622'
 执行写法1后,内存迅速被吃光,差点死机,几次都是这样;student表有2000多条数据,grade表有50000多条数据。写法2:
select *  from (
   select a.studentId,a.studentName, b.teacherId,b.lessonId,b.lessonName  from student  a inner join grade b on a.stuentId=b.stuentId ) t 
where t.studentName='34500654' or studentId='34500654'
这种写法可以正常执行。请教大家写法1为什么会迅速吃掉内存?

解决方案 »

  1.   


    --因为的结果有问题
    select teacherId,lessonId,lessonName 
     from student a inner join grade b on a.stuentId=b.stuentId
     where a.studentName = '34514622' or b.stuentId='34514622'
      

  2.   

    select teacherId,lessonId,lessonName from student a left join grade b on a.studentid =b.student and (a.studentName = '34514622' or b.stuentId='34514622'
    )--这样试试,看看效率
      

  3.   

    select teacherId,lessonId,lessonName from student a left join grade b 
    on a.studentid =b.studentid where a.studentName = '34514622' or b.stuentId='34514622'
      

  4.   

    --第一种写法少写条件了,下面的试试
    select teacherId,lessonId,lessonName from student a ,grade b 
    where a.stuentId=b.stuentId and
    (a.studentName = '34514622' or b.stuentId='34514622')
      

  5.   

    select
     a.teacherId,b.lessonId,b.lessonName --这里的字段在哪个表自己指定
    from
     student a ,grade b 
    where
     a.studentName = '34514622' or b.stuentId='34514622'
    and
     a.stuentId=b.stuentId --这里的关联条件是两表都有的
      

  6.   

    楼主自己看看use weimei
    create table student
    (studentId int not null primary key ,
     studentName varchar(20),
     height int,
     weight int,
     telphone varchar(11)
    )
    declare @int_s int,@int_g int,@int_sid int
    set @int_s=0
    while @int_s<2000
    begin
    insert into student values(@int_s,'student'+cast(@int_s as varchar),@int_s,@int_s,'phone')
    set @int_s=@int_s+1
    end
    create table grade
    (studentId int not null foreign key references student(studentId),
     int,
    teacherId int,
    lessonId int, 
    lessonName varchar(20)
    )
    set @int_g=0
    set @int_sid=0
    while @int_g<50000
    begin
    if(@int_sid=2000)
    begin
    set @int_sid=0
    end
    insert into grade values(@int_sid,@int_sid,@int_sid,@int_sid,'lession')
    set @int_g=@int_g+1
    set @int_sid=@int_sid+1
    end
    set statistics profile on
    set statistics io on
    set statistics time on
    go
    select teacherId,lessonId,lessonName from student a ,grade b where a.studentName = '2' or b.studentId='2'
    --SQL Server 分析和编译时间: 
    --   CPU 时间 = 47 毫秒,占用时间 = 98 毫秒。
    --
    --(50000 行受影响)
    --表 'Worktable'。扫描计数 1,逻辑读取 572934 次,物理读取 0 次,预读 0 次,lob 逻辑读取 0 次,lob 物理读取 0 次,lob 预读 0 次。
    --表 'grade'。扫描计数 1,逻辑读取 234 次,物理读取 0 次,预读 0 次,lob 逻辑读取 0 次,lob 物理读取 0 次,lob 预读 0 次。
    --表 'student'。扫描计数 1,逻辑读取 13 次,物理读取 0 次,预读 0 次,lob 逻辑读取 0 次,lob 物理读取 0 次,lob 预读 0 次。
    --
    --(5 行受影响)
    --
    --SQL Server 执行时间:
    --   CPU 时间 = 48547 毫秒,占用时间 = 50118 毫秒。select * from (
      select a.studentId,a.studentName, b.teacherId,b.lessonId,b.lessonName from student a inner join grade b on a.studentId=b.studentId ) t  
    where t.studentName='2' or studentId='2'
    --SQL Server 分析和编译时间: 
    --   CPU 时间 = 16 毫秒,占用时间 = 53 毫秒。
    --
    --(25 行受影响)
    --表 'Worktable'。扫描计数 0,逻辑读取 0 次,物理读取 0 次,预读 0 次,lob 逻辑读取 0 次,lob 物理读取 0 次,lob 预读 0 次。
    --表 'grade'。扫描计数 1,逻辑读取 234 次,物理读取 0 次,预读 0 次,lob 逻辑读取 0 次,lob 物理读取 0 次,lob 预读 0 次。
    --表 'student'。扫描计数 1,逻辑读取 13 次,物理读取 0 次,预读 0 次,lob 逻辑读取 0 次,lob 物理读取 0 次,lob 预读 0 次。
    --
    --(4 行受影响)
      

  7.   


    select teacherId,lessonId,lessonName from student a ,grade b where a.studentName = '2' or b.studentId='2' and a.studentId= b.studentId
    --(25 行受影响)
    --表 'Worktable'。扫描计数 1,逻辑读取 572934 次,物理读取 0 次,预读 0 次,lob 逻辑读取 0 次,lob 物理读取 0 次,lob 预读 0 次。
    --表 'grade'。扫描计数 1,逻辑读取 234 次,物理读取 0 次,预读 0 次,lob 逻辑读取 0 次,lob 物理读取 0 次,lob 预读 0 次。
    --表 'student'。扫描计数 1,逻辑读取 13 次,物理读取 0 次,预读 0 次,lob 逻辑读取 0 次,lob 物理读取 0 次,lob 预读 0 次。
    --
    --(5 行受影响)
    --
    --SQL Server 执行时间:
    --   CPU 时间 = 50000 毫秒,占用时间 = 51226 毫秒。select teacherId,lessonId,lessonName from student a ,grade b where a.studentId= b.studentId  and a.studentName = '2' or b.studentId='2'
    --(50000 行受影响)
    --表 'Worktable'。扫描计数 1,逻辑读取 572934 次,物理读取 0 次,预读 0 次,lob 逻辑读取 0 次,lob 物理读取 0 次,lob 预读 0 次。
    --表 'grade'。扫描计数 1,逻辑读取 234 次,物理读取 0 次,预读 0 次,lob 逻辑读取 0 次,lob 物理读取 0 次,lob 预读 0 次。
    --表 'student'。扫描计数 1,逻辑读取 13 次,物理读取 0 次,预读 0 次,lob 逻辑读取 0 次,lob 物理读取 0 次,lob 预读 0 次。
    --
    --(5 行受影响)
    --
    --SQL Server 执行时间:
    --   CPU 时间 = 34860 毫秒,占用时间 = 35736 毫秒。
    select teacherId,lessonId,lessonName from student inner join grade on student.studentId= grade.studentId where studentName = '2' or grade.studentId='2' 
    --(25 行受影响)
    --表 'Worktable'。扫描计数 0,逻辑读取 0 次,物理读取 0 次,预读 0 次,lob 逻辑读取 0 次,lob 物理读取 0 次,lob 预读 0 次。
    --表 'grade'。扫描计数 1,逻辑读取 234 次,物理读取 0 次,预读 0 次,lob 逻辑读取 0 次,lob 物理读取 0 次,lob 预读 0 次。
    --表 'student'。扫描计数 1,逻辑读取 13 次,物理读取 0 次,预读 0 次,lob 逻辑读取 0 次,lob 物理读取 0 次,lob 预读 0 次。
    --
    --(4 行受影响)
    --
    --SQL Server 执行时间:
    --   CPU 时间 = 31 毫秒,占用时间 = 38 毫秒。SQL查询,使用inner join会产生一个临时表,再根据条件进行筛选,而第一种查询会在笛卡尔积表里筛选