[Resolvido] COnsulta demorando 16 segundos
Boa tarde galera,
Estou com uma dificuldade no sql eu acho.
tenho que fazer um somatório de atividades por usuário.
Hoje estou fazendo da seguinte forma.
seleciono os usuário específicos e dentro do loop faço outras consultas.
segue o código.
<%
'Seleciona o nome dos alunos da turma passada por querystring
Set rsAlunosdaTurma = dbconn.execute("SELECT nome, usuario_id_fk "& _
"FROM dbcepciv.tbl_turma_usuario as turma "& _
"INNER JOIN app.usuario ON turma.usuario_id_fk = usuario.id "& _
"WHERE idturma_fk = "&turma&""& _
"ORDER BY nome ASC;")
i = 0
Do while not rsAlunosdaTurma.eof
response.Write("<tr>")
response.Write("<td style='padding-left:15px; font-size:12px; color:#1E5FBA;background-color:#fff; line-height:20px'>")
qtd = len(rsAlunosdaTurma("nome"))
if qtd > 25 then
response.Write("<a style='text-align:center; color:#1E5FBA; ' href='tdativ.asp?trm="&request.QueryString("trm")&"&idud="&EncryptString(idunidade)&"&di="&EncryptString(rsAlunosdaTurma("usuario_id_fk"))&"'>"&left(rsAlunosdaTurma("nome"),25)&"...</a>")
else
response.Write("<a style='text-align:center; color:#1E5FBA; ' href='tdativ.asp?trm="&request.QueryString("trm")&"&idud="&EncryptString(idunidade)&"&di="&EncryptString(rsAlunosdaTurma("usuario_id_fk"))&"'>"&rsAlunosdaTurma("nome")&"</a>")
end if
response.Write("</td>")
'Conta quantas atividades objetiva já foram respondidas por usuario por turma
sql = "SELECT count(distinct a.idatividade_pk) as total FROM dbcepciv.tbl_resposta as r INNER JOIN "&_
"( dbcepciv.tbl_atividade as a INNER JOIN (dbcepciv.tbl_unidade as u INNER JOIN "&_
"(dbcepciv.tbl_modulo as m INNER JOIN dbcepciv.tbl_turma as t "&_
"ON m.idmodulo_pk = t.idmodulo_fk) ON u.idmodulo_fk = m.idmodulo_pk) "&_
"ON a.idunidade_fk = u.idunidade_pk) ON r.idatividade_fk = a.idatividade_pk "& _
"WHERE usuario_id_fk = "&rsAlunosdaTurma("usuario_id_fk")&" AND idunidade_pk = "&idunidade&" "&_
"AND idturma_pk = "&turma&" AND a.tipo = 'objetiva'"
Set rsContasobjetivas = dbconn.execute(sql)
if rsContasobjetivas.eof Then
Total = 0
else
Total = rsContasobjetivas("total")
end if
rsContasobjetivas.close
Set rsContasobjetivas = Nothing
response.Write("<td style='text-align:center; color:#1E5FBA; border-right:1px solid #666; border-left:1px solid #666;background-color:#fff'><a style='text-align:center; color:#1E5FBA; ' href='tdativ.asp?trm="&request.QueryString("trm")&"&idud="&EncryptString(idunidade)&"&di="&EncryptString(rsAlunosdaTurma("usuario_id_fk"))&"'>"&total&"</a></td>")
'Conta quantas atividades subjetiva e upload já foram respondidas por usuario por turma
sql = "SELECT count(distinct a.idatividade_pk) as total FROM dbcepciv.tbl_resposta as r INNER JOIN "&_
"( dbcepciv.tbl_atividade as a INNER JOIN (dbcepciv.tbl_unidade as u INNER JOIN "&_
"(dbcepciv.tbl_modulo as m INNER JOIN dbcepciv.tbl_turma as t "&_
"ON m.idmodulo_pk = t.idmodulo_fk) ON u.idmodulo_fk = m.idmodulo_pk) "&_
"ON a.idunidade_fk = u.idunidade_pk) ON r.idatividade_fk = a.idatividade_pk "& _
"WHERE usuario_id_fk = "&rsAlunosdaTurma("usuario_id_fk")&" AND idunidade_pk = "&idunidade&" "&_
"AND idturma_pk = "&turma&" AND (a.tipo = 'subjetiva' OR a.tipo = 'upload')"
Set rsContassubjetivas = dbconn.execute(sql)
if rsContassubjetivas.eof Then
Totalsubjetivas = 0
else
Totalsubjetivas = rsContassubjetivas("total")
end if
rsContassubjetivas.close
Set rsContassubjetivas = Nothing
response.Write("<td style='text-align:center; color:#1E5FBA; border-right:1px solid #666; background-color:#f0f0f0; '><a style='text-align:center; color:#1E5FBA; ' href='tdativ.asp?trm="&request.QueryString("trm")&"&idud="&EncryptString(idunidade)&"&di="&EncryptString(rsAlunosdaTurma("usuario_id_fk"))&"'>"&totalsubjetivas&"</a></td>")
'Conta quantas atividades objetiva já foram respondidas por usuario por turma
sql = "SELECT count(distinct a.idatividade_pk) as total FROM dbcepciv.tbl_feedback as f INNER JOIN (dbcepciv.tbl_resposta as r INNER JOIN "&_
"( dbcepciv.tbl_atividade as a INNER JOIN (dbcepciv.tbl_unidade as u INNER JOIN "&_
"(dbcepciv.tbl_modulo as m INNER JOIN dbcepciv.tbl_turma as t "&_
"ON m.idmodulo_pk = t.idmodulo_fk) ON u.idmodulo_fk = m.idmodulo_pk) "&_
"ON a.idunidade_fk = u.idunidade_pk) ON r.idatividade_fk = a.idatividade_pk) ON r.idresposta_pk = f.idresposta_fk "& _
"WHERE r.usuario_id_fk = "&rsAlunosdaTurma("usuario_id_fk")&" AND idunidade_pk = "&idunidade&" "&_
"AND idturma_pk = "&turma&" AND (a.tipo = 'subjetiva' OR a.tipo = 'upload')"
Set rsContasfeedback = dbconn.execute(sql)
if rsContasfeedback.eof Then
Totalfeedback = 0
else
Totalfeedback = rsContasfeedback("total")
end if
rsContasfeedback.close
Set rsContasfeedback = Nothing
response.Write("<td style='text-align:center; color:#1E5FBA; border-right:1px solid #666; background-color:#f0f0f0; '><a style='text-align:center; color:#1E5FBA; ' href='tdativ.asp?trm="&request.QueryString("trm")&"&idud="&EncryptString(idunidade)&"&di="&EncryptString(rsAlunosdaTurma("usuario_id_fk"))&"'>"&Totalfeedback&"</a></td>")
'Quantidade de respostas subjetivas/upload não tiveram feedback
aguardandoFeedback = Cint(Totalsubjetivas) - Cint(Totalfeedback)
response.Write("<td style='text-align:center; color:#1E5FBA; '><a style='text-align:center; color:#1E5FBA; ' href='tdativ.asp?trm="&request.QueryString("trm")&"&idud="&EncryptString(idunidade)&"&di="&EncryptString(rsAlunosdaTurma("usuario_id_fk"))&"'>"&aguardandoFeedback&"</a></td>")
response.Write("</tr>")
rsAlunosdaTurma.movenext
loop
rsAlunosdaTurma.close
Set rsAlunosdaTurma = Nothing
fim_tempo = Timer 'Pega o tempo
tempo_decorrido = fim_tempo - Inicio_tempo
If tempo_decorrido = 0 then
Response.write "menos de 0.005"
Else
response.write (Cint(Round(tempo_decorrido,4)))
End if
response.write " Segundos"
%>
Discussão (2)
Carregando comentários...