Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/73.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Sql 优化查询_Sql_Oracle - Fatal编程技术网

Sql 优化查询

Sql 优化查询,sql,oracle,Sql,Oracle,我在网上搜索过,试图解决这个问题,但没有成功。这也是我关于SO的第一个SQL问题:-) 我只是一个简单的人,有着简单的疑问——请允许我演示一下 select asy.aim_student_id, ast.aim_test from aim_student_test ast join aim_student_absent asa on (asa.aps_yr = ast.aps_yr and asa.aim_test = ast.aim_test and asa.aim_id = ast

我在网上搜索过,试图解决这个问题,但没有成功。这也是我关于SO的第一个SQL问题:-)

我只是一个简单的人,有着简单的疑问——请允许我演示一下

select asy.aim_student_id, ast.aim_test  
from aim_student_test ast
  join aim_student_absent asa on (asa.aps_yr = ast.aps_yr and asa.aim_test = ast.aim_test and asa.aim_id = ast.aim_id)
  --join aim_student_qst asq on (asq.aps_yr = ast.aps_yr and asq.aim_test = ast.aim_test and asq.aim_id = ast.aim_id)
  join aim_student_yr asy on (asy.aps_yr = ast.aps_yr and asy.aim_student_yr_id = ast.aim_student_yr_id)
    where ast.aps_yr = '2012'
如您所见-加入aim\u学生\u qst已被注释掉

aim_student_qst是列出学生对所有问题的回答的表格。所以一个学生在这张桌子上有大约50个病例。为了测试是什么让我的查询变慢了,我简单地注释了aim_student_qst的加入,果然我的查询加快了

我假设Oracle正在做的是——哦,你想要这些表,让我们把它们放在一个大表中,然后寻找我们想要的。这就是为什么我的查询速度较慢,尽管没有对aim_student_qst做任何其他操作。这是正确的吗

出于我的目的,我只需要为每个学生选择一个问题,而不是全部50个问题。有办法做到这一点吗

谢谢

那么也许是这个

select asy.aim_student_id, ast.aim_test  
from aim_student_test ast
  join aim_student_absent asa on (asa.aps_yr = ast.aps_yr and asa.aim_test = ast.aim_test and asa.aim_id = ast.aim_id)
  join aim_student_qst asq on (asq.aps_yr = ast.aps_yr and asq.aim_test = ast.aim_test and asq.aim_id = ast.aim_id)
  join aim_student_yr asy on (asy.aps_yr = ast.aps_yr and asy.aim_student_yr_id = ast.aim_student_yr_id)
where ast.aps_yr = '2012'
  and asq.question_number = 1 -- column name assumed
  and asq.question_answer is NULL -- column name and value assumed
;

我认为你的假设是错误的。我觉得没有道理,为什么你要加入一个特定学生的答案表,而这个加入不会在加入中包含aim\u sudent\u id。可能实际发生的是你创建了一个巨大的不相关行连接。奇怪的是,每个学生都有两个唯一的ID。其中一个是aim_student_id,另一个是aim_id。如您所见,aim_id用于加入aim_student_qst。此外,我忘了提到的是,没有加入aim_学生qst。运行时间为9.34秒,加入时为547.698秒:比率为1:58.64,接近每个学生的问题数量。关于
aim\u student\u qst
,加入此表的目的是什么?您既不是从中选择,也不是对其进行筛选。您是否需要将结果限制在此表中存在的学生?
aim\u student\u qst.aim\u id
索引了吗?首先-我不知道aim\u student\u qst.aim\u id是否索引了。我怎么检查这样的东西?第二,加入aim_student_qst的目的是选择一个学生提出的问题,并检查回答是否遗漏。好吧,让我们暂时把索引问题放在一边(评论太多了)。您想从
asq
中选择一个问题,但联接不受任何限制。那么你想要哪一个“问题”?您是否专门寻找任何未回答问题、特定问题、第一个问题、随机问题等的实例?这运行得非常快。谢谢你的耐心。有什么好的SQL在线指南可以解释它在幕后是如何工作的吗?我觉得这会帮助我处理这些类型的问题
null
'
在Oracle中是一样的,所以
子句是不必要的;只需检查
null
@nebffa:Tom Kyte有几本关于Oracle的书。多年来,Kyte为DBMS杂志撰写了一篇名为“Ask Tom”的专栏文章,并随后撰写了一些有关Oracle的最佳书籍。他最终被甲骨文聘用,而他的甲骨文数据库架构专家。。。这本书是我推荐的。@AlexPoole你说得对。已编辑应答以删除空值的冗余检查。