# TBD ## Instruções - Conectar (ver slides) - Other Users - While (não todas as tabelas feitas): - GTD10 - Botão direito em TABELA - Table - Copy - Mudar o nome para XTABELA - INCLUDE DATA [X] - Vai parar a Tables(Filtered) - Pode ser preciso refresh ## Propostas de Indexes / Constraints - Tabelas X: - Retirar todos os constraints - Botão direito na tabela - Constraints - Disable All - Tabelas Y: - Não fazer um crl (já vêm com constraints do copy paste) - Tabelas Z - Criar index em ZTIPOSAULA (tipo, #weekly_classes?) - Criar index em ZOCORRENCIAS (ano_letivo, periodo, departamento) ## Perguntas 0) Justificação: São colunas que podem envolver um elevado volume de queries, por isso deverão ser indexadas. Têm elevada selectability 1.1) Query para tabelas: ```sql SELECT UCS.codigo, UCS.designacao, TIPOSAULA.ano_letivo, OCORRENCIAS.inscritos, TIPOSAULA.tipo, TIPOSAULA.turnos FROM UCS JOIN TIPOSAULA ON UCS.codigo = TIPOSAULA.codigo JOIN OCORRENCIAS ON OCORRENCIAS.codigo = TIPOSAULA.codigo WHERE UCS.designacao = 'Bases de Dados' AND UCS.curso = 275; ``` GPT Tries: ```sql SELECT u.codigo AS codigo, u.designacao AS designacao, o.ano_letivo AS ano_letivo, o.inscritos AS inscritos, t.tipo AS tipo, t.turnos AS turnos FROM UCS u JOIN OCORRENCIAS o ON u.codigo = o.codigo JOIN TIPOSAULA t ON o.periodo = t.periodo AND o.ano_letivo = t.ano_letivo AND o.codigo = t.codigo WHERE u.designacao = 'Bases de Dados' AND u.curso = 275; ``` GPT Explanation: - We're selecting specific columns: codigo, designacao, ano_letivo, inscritos, tipo, and turnos. - We're joining the UCS table (u) with the OCORRENCIAS table (o) on the codigo column. - Then, we're joining the resulting set with the TIPOSAULA table (t) on the periodo, ano_letivo, and codigo columns. - We're filtering the results to only include rows where the designacao is 'Bases de Dados' and the curso is 275. 1.2) Resultados ![image](https://hackmd.io/_uploads/ByFK4_VkC.png) GPT Tries: ![image](https://hackmd.io/_uploads/rkjrXlLkC.png) 1.3) Explicação dos execution plans ![image](https://hackmd.io/_uploads/S1oNIMckA.png) 1.4) please check 14 rows for 0.05 seconds 2.1) Query para Tabelas: ```sql SELECT sum(TIPOSAULA.horas_turno * TIPOSAULA.n_aulas), TIPOSAULA.tipo FROM TIPOSAULA JOIN UCS ON UCS.codigo = TIPOSAULA.codigo WHERE UCS.curso = 233 AND TIPOSAULA.ano_letivo = '2004/2005' GROUP BY TIPOSAULA.tipo ``` GPT Tries: ```sql SELECT t.tipo AS tipo, SUM(t.horas_turno * t.n_aulas) AS total_horas FROM XTIPOSAULA t JOIN XOCORRENCIAS o ON t.codigo = o.codigo AND t.periodo = o.periodo AND t.ano_letivo = o.ano_letivo JOIN XUCS u ON o.codigo = u.codigo WHERE u.curso = 233 AND o.ano_letivo = '2004/2005' GROUP BY t.tipo; ``` 2.2) Resultados ![image](https://hackmd.io/_uploads/Skn1OuEy0.png) ![image](https://hackmd.io/_uploads/r1SkNl81C.png) We got it niggers :D 2.3) Execution Plans ![image](https://hackmd.io/_uploads/Sy0r6AcJR.png) 3.1) Query para tabelas a) GPT Tries: ```sql SELECT codigo FROM UCS WHERE codigo NOT IN ( SELECT codigo FROM OCORRENCIAS WHERE ano_letivo = '2003/2004' ); ``` b) ```sql SELECT u.codigo FROM UCS u LEFT JOIN OCORRENCIAS o ON u.codigo = o.codigo AND o.ano_letivo = '2003/2004' WHERE o.codigo IS NULL; ``` 3.2) Resultados: a) ![image](https://hackmd.io/_uploads/r12TNgLyC.png) b) ![image](https://hackmd.io/_uploads/rJ_WSlUJR.png) 4.1) GPT HAS NO IDEA 5.1) ONLY QUERY: GPT Tries: ```sql SELECT o.codigo AS course_code, o.ano_letivo AS academic_year, o.periodo AS period, SUM(CASE WHEN t.tipo = 'OT' THEN t.horas_turno ELSE 0 END) AS total_hours_OT FROM XOCORRENCIAS o JOIN XTIPOSAULA t ON o.codigo = t.codigo AND o.periodo = t.periodo AND o.ano_letivo = t.ano_letivo WHERE o.ano_letivo IN ('2002/2003', '2003/2004') GROUP BY o.codigo, o.ano_letivo, o.periodo; ``` 6.1) Query para as tabelas: GPT Tries: ```sql SELECT u.curso FROM ZUCS u JOIN ZOCORRENCIAS o ON u.codigo = o.codigo JOIN ZTIPOSAULA t ON o.periodo = t.periodo AND o.ano_letivo = t.ano_letivo AND o.codigo = t.codigo GROUP BY u.curso HAVING COUNT(DISTINCT t.tipo) = (SELECT COUNT(DISTINCT tipo) FROM ZTIPOSAULA); ``` 6.2) Resultados: ![image](https://hackmd.io/_uploads/S1_RIxIJA.png)