查询花费很多时间

时间:2018-06-20 10:14:52

标签: sql sql-server query-optimization

我有三个表Student_details和Student_Marks和subject_Name。

主题名称

select foo.*
from (
select ROW_NUMBER() OVER () as rownum, * from mytable ) as foo
where rownum=5

Student_details

+----+------------------+
| ID |     Sub_Name     |
+----+------------------+
|  1 | Maths            |
|  2 | Physics          |
|  3 | Chemistry        |
|  4 | Biology          |
|  5 | Computer Science |
+----+------------------+

Student_Marks

+----+------------+-----------+-----+-------+--+
| ID | First_Name | Last_Name | Age | Class |  |
+----+------------+-----------+-----+-------+--+
|  1 | Rohit      | Sharma    |  28 |     3 |  |
|  2 | Shikhar    | Dhavan    |  27 |     2 |  |
|  3 | Virat      | Kohli     |  29 |     3 |  |
|  4 | MS         | Dhoni     |  30 |     2 |  |
|  5 | Hardik     | Pandya    |  25 |     3 |  |
+----+------------+-----------+-----+-------+--+

并且我正在使用此查询来获取结果。

+----+------------+------------+---------------+
| ID | Student_Id | Subject_Id | Subject_Marks |
+----+------------+------------+---------------+
|  1 |          1 |          1 |            90 |
|  2 |          1 |          2 |            82 |
|  3 |          1 |          3 |            85 |
|  4 |          1 |          4 |            75 |
|  5 |          1 |          5 |            92 |
|  6 |          2 |          1 |            90 |
|  7 |          2 |          2 |            82 |
|  8 |          2 |          3 |            85 |
|  9 |          2 |          4 |            75 |
| 10 |          2 |          5 |            92 |
| 11 |          3 |          1 |            90 |
| 12 |          3 |          2 |            82 |
| 13 |          3 |          3 |            85 |
| 14 |          3 |          4 |            75 |
| 15 |          3 |          5 |            92 |
+----+------------+------------+---------------+

现在,此查询花费太多时间来获取结果。

如何优化此查询?


编辑:

我正在使用休眠获取结果,因此实际查询是:

select sd.First_Name
      ,sd.Last_Name
      ,sm.Subject_Id
      ,sm.Subject_Marks
from Student_Marks sm
    inner join Student_details sd
       on sm.Student_Id = sd.ID
where sm.Subject_Id in (select ID
                        from Subject_Name
                        where Sub_Name in ('Maths', 'Physics')
                       )
  and Student_Id in (select ID
                     from Student_details
                     where class in (2,3)
                    );

2 个答案:

答案 0 :(得分:0)

尝试一下:

select sd.First_Name
      ,sd.Last_Name
      ,sm.Subject_Id
      ,sm.Subject_Marks
from Student_Marks sm
     inner join Student_details sd on sm.Student_Id = sd.ID
     inner join Subject_Name sn on sm.Subject_Id = sn.ID AND Sub_Name in ('Maths','Physics')
     inner join Student_details on Student_Id = Student_details.ID AND class in (2,3)

答案 1 :(得分:0)

我不了解学生详细信息子查询。为什么要说“给我查找班级为2或3的学生详细信息时找到的学生详细信息”而不是“给我班级为2或3的学生详细信息”?

select
  sd.sd.first_name,
  sd.last_name,
  sm.subject_id,
  sn.sub_name,
  sm.subject_marks
from student_marks sm
join student_details sd on sd.id = sm.student_id and sd.class in (2, 3)
join subject_name sn on sn.id = sm.subject_id and sn.sub_name in ('Maths', 'Physics');

关于subject_name:我加入了表格,因此可以显示其名称。仅选择ID并猜测哪个是数学和哪个物理是没有意义的。

您应该提供以下索引:

create index idx1 on subject_name(sub_name, id);
create index idx2 on student_details(class, id);
create index idx3 on student_marks(student_id, subject_id);
create index idx4 on student_marks(subject_id, student_id);

甚至涵盖索引:

create index idx1 on subject_name(sub_name, id, sub_name);
create index idx2 on student_details(class, id, first_name, last_name);
create index idx3 on student_marks(student_id, subject_id, subject_marks);
create index idx4 on student_marks(subject_id, student_id, subject_marks);

我不会说是使用索引idx3还是idx4。尝试一下并丢弃未使用的。 (当然,DBMS可能会决定根本不使用这些索引;它们只是一个报价。)