martes, 28 de agosto de 2007

Obtener las n estadisticas mas altas de un objeto.

A un compañero se le presento la necesidad de obtener, por objeto, unicamente las N estadísticas mas altas de la tabla v$segment_statistics. La respuesta fue localizada a traves de la funcion row_number.

Esta funcion permite hacer una numeracion de los resultados y a su vez segmentar esa numeracion en funcion a los mismos.

En base a esa necesidad nació top_statistics.sql

rem Purpose: Check the top N statistics by object for
rem the currently-running database
rem
rem Author: Alfredo Miranda {amiranda.ve(at)gmail.com}
rem Caracas - Venezuela
rem
rem Modifications: Jose Manuel Betancourt {josemanuelb(at)gmail.com}
rem Caracas - Venezuela
rem
rem Updates:
rem 28/08/2007 - Creacion
rem 28/08/2007 - Parametrizacion y documentacion
rem

set pages 9999
set lines 132
break on owner on object_type on object_name on subobject_name skip 1
col orden noprint
col owner format a8
col object_type format a10
col object_name format a19
col subobject_name format a19
col statistic_name format a19

accept cant prompt 'Numero de estadisticas a mostrar:'
spool top_statistics.lst

select owner, object_type, object_name, subobject_name, statistic_name, value, orden
from (
select OBJ#, statistic_name, value , row_number()
over (partition by OBJ# order by value desc) as orden
from v$segment_statistics
where
owner not in ('SYS','SYSTEM')
) segstat, sys.dba_objects dbaobj
where
orden < &cant +1 and dbaobj.object_id = obj# order by owner,object_type,object_name , value desc / spool off


Como resultado podran ver algo similar a esto:
















Haciendo click aqui podran ver todo lo relacionado a row_number.