Window Functions Logan Destefani Merazzi Entendendo o que são e como utilizá-las para obter o melhor desempenho Logan Destefani Merazzi
> Get-LocalUser –Name “Logan” | Format-List 20 anos de profissão Mais de 10 anos com SQL Server Disaster Recovery Security Performance MCSA (MCSE a caminho, assim espero) @loganmerazzi /loganmerazzi logan@merazzi.com.br http://blog.merazzi.com.br
Window Function – O que são? Introduzidos no SQL Server 2005 (yep... Há 14 anos) Divididos em: Classificação Agregação Analítica (2012 adiante) VARP LAG CHECKSUM_AGG RANK COUNT_BIG PERCENTILE_CONT PERCENT_RANK SUM MIN VAR NTILE LEAD CUME_DIST DENSE_RANK COUNT FIRST_VALUE LAST_VALUE AVG MAX ROW_NUMBER PERCENTILE_DISC STDEV STDEVP
Para pensar... A cada registro, somar os 3 anteriores e os 3 próximos com o registro atual Running Total: Somar a linha atual, com todas as linhas anteriores Apagar as linhas que estão duplicadas na tabela. Dividir a seu dataset em grupos de 4 .....
Sem Window Function Com Window Function ?????????? Self-Joins Temp Table Cursores Com Window Function <window function> OVER ([PARTITION BY <expression list>] [ORDER BY <expression [ASC|DESC] list>] [ROWS|RANGE <window frame>]) ??????????
Vamos por partes.... <window function> OVER ( [PARTITION BY <expression list>] “Agrupamento” – Nem sempre obrigatório [ORDER BY <expression [ASC|DESC] list>] [ROWS|RANGE <window frame>] UNBOUNDED PRECEDING | <unsigned_value_specification> PRECEDING | CURRENT ROW UNBOUNDED FOLLOWING | <unsigned_value_specification> FOLLOWING | )
Demo
O que cada função analítica suporta Cláusula OVER PARTITION BY ORDER BY ROWS/RANGE CUME_DIST R O X FIRST_VALUE LAG LAST_VALUE LEAD PERCENTILE_CONT PERCENTILE_DISC PERCENT_RANK O=Opcional, R=Requerido, N=Não permite
Vou trocar tudo pra Window Function AGORA!!! Problemas conhecidos.... Views Tabelas derivadas CTE e in-line function Testar, testar, testar!! Paul White https://sqlperformance.com/2013/03/t-sql-queries/the-problem-with-window-functions-and-views Brent Ozar https://www.brentozar.com/archive/2017/09/indexing-windowing-functions-vs/ https://www.brentozar.com/archive/2015/06/indexing-for-windowing-functions/ Dwain Camps https://www.red-gate.com/simple-talk/sql/t-sql-programming/the-performance-of-the-t-sql-window-functions/ Bert Wagner https://www.youtube.com/watch?v=6nEBu6CtUng
Dúvidas?
Muito Obrigado!!