Como mejorar el performance del Procedimiento alamacenado

09/07/2004 - 21:09 por jose maria | Informe spam
Saludos, tengo un procedimiento almacenado que me consume
el 99% del cpu de mi servidor cada vez que se ejecuta por
un lapso de 8 min.

Si el mismo codigo lo saco del procedimiento almacenado y
lo ejecuto por fuera, hace la misma operacion pero en
30seg.

me gustaria saber si alguien sabe a que se deba esto, si
sql server le da un tratamiento especial al procedimiento
y si es asi como puedo mejorar el performance o configurar
el server. gracias

Preguntas similare

Leer las respuestas

#6 Norman A. Armas
12/07/2004 - 16:11 | Informe spam
Esto lo puede explicar
Cuando usas un procedimiento almacenado con parametros el plan de ejecucion
se guarda con la primera opcion que corre ese sp(o sea con los valores de
los parametros con que se ejecuta por primera vez).
La segunda y otras veces que corre se ira por ese plan de ejecucion sea
optimo o no.
Cuando lo ejecutas desde el query analizer siempre busca el plan de
ejecucion mas optimo para ese caso.
Es un poco largo el tema pero si quieres profundizar el el SQLServer
Magazine de este mes existe un articulo muy bueno de Kalen Delaney
"Optimizing Variables and Parameters" que trata sobre el tema y cuales son
las posibles soluciones.

Creo que esa podria ser la causa, no esta de mas que el le heche un vistazo
a esa opcion.

Saludos,

Norman




"Javier Loria" wrote in message
news:
Hola Norman:
El LIKE '%' no explica porque el procedimiento tarda 16 veces mas que


la
consulta escrita directamente en el query analizer, ni el procesador al
100%.
Saludos,

Javier Loria
Costa Rica
Se aprecia la inclusion de DDL (CREATE, INSERTS, etc.)
que pueda ser copiado y pegado al Query Analizer.
La version de SQL y Service Pack tambien ayuda.

Norman A. Armas escribio:
> El problema lo tienes con LIKE '%
>
> cuando usas ese tipo de consulta SQL no puede usar indices para
> realizar la busqueda,
> del la unica forma que usa es si el % esta el final de la cadena no al
> principio.
>
>
> wrote in message
> news:2982e01c465fa$36ac08d0$
>>
>>
>> create procedure proc_ant_saldo
>> @id char(20), @cte_numero varchar(20)='%', @dep_id varchar
>> (20)='%',
>> @fecha_fin datetime='31-12-9999'
>> as
>>
>> insert into antiguedad
>> select @id, a.id as factura_id, cargo_t, a.folio_fiscal,
>> a.dep_id, a.cte_numero, a.fecha_venc, a.fecha_pago,
>> a.fecha_salvta as fecha, forma_pago_t, a.total,
>> 'saldo_doc'ÊSE
>> WHEN exists (select top 1 factura_id from abn_fac where
>> factura_id=a.id and stafa>0 and fecha<=@fecha_fin and
>> (fecha_canc is null or fecha_canc>@fecha_fin)) and exists
>> (select top 1 factura_id from cargo_factura where
>> factura_id=a.id and fecha<=@fecha_fin) and exists (select
>> top 1 id from factura where cargo_t='F' and
>> numero=a.anticipo_fac and imprime=0) THEN round(a.total-
>> a.anticipo-(select sum(importe) from abn_fac where
>> factura_id=a.id and stafa>0 and fecha<=@fecha_fin and
>> (fecha_canc is null or fecha_canc>@fecha_fin))+(select sum
>> (importe) from cargo_factura where factura_id=a.id and
>> fecha<=@fecha_fin),2)
>> WHEN exists (select top 1 factura_id from abn_fac where
>> factura_id=a.id and stafa>0 and fecha<=@fecha_fin and
>> (fecha_canc is null or fecha_canc>@fecha_fin)) and exists
>> (select top 1 factura_id from cargo_factura where
>> factura_id=a.id and fecha<=@fecha_fin) and not exists
>> (select top 1 id from factura where cargo_t='F' and
>> numero=a.anticipo_fac and imprime=0) THEN round(a.total-
>> (select sum(importe) from abn_fac where factura_id=a.id
>> and stafa>0 and fecha<=@fecha_fin and (fecha_canc is null
>> or fecha_canc>@fecha_fin))+(select sum(importe) from
>> cargo_factura where factura_id=a.id and
>> fecha<=@fecha_fin),2)
>> WHEN exists (select top 1 factura_id from abn_fac where
>> factura_id=a.id and stafa>0 and fecha<=@fecha_fin and
>> (fecha_canc is null or fecha_canc>@fecha_fin)) and not
>> exists (select top 1 factura_id from cargo_factura where
>> factura_id=a.id and fecha<=@fecha_fin) and exists (select
>> top 1 id from factura where cargo_t='F' and
>> numero=a.anticipo_fac and imprime=0) THEN round(a.total-
>> a.anticipo-(select sum(importe) from abn_fac where
>> factura_id=a.id and stafa>0 and fecha<=@fecha_fin and
>> (fecha_canc is null or fecha_canc>@fecha_fin)),2)
>> WHEN exists (select top 1 factura_id from abn_fac where
>> factura_id=a.id and stafa>0 and fecha<=@fecha_fin and
>> (fecha_canc is null or fecha_canc>@fecha_fin)) and not
>> exists (select top 1 factura_id from cargo_factura where
>> factura_id=a.id and fecha<=@fecha_fin) and not exists
>> (select top 1 id from factura where cargo_t='F' and
>> numero=a.anticipo_fac and imprime=0) THEN round(a.total-
>> (select sum(importe) from abn_fac where factura_id=a.id
>> and stafa>0 and fecha<=@fecha_fin and (fecha_canc is null
>> or fecha_canc>@fecha_fin)),2)
>> WHEN not exists (select top 1 factura_id from abn_fac
>> where factura_id=a.id and stafa>0 and fecha<=@fecha_fin
>> and (fecha_canc is null or fecha_canc>@fecha_fin)) and
>> exists (select top 1 factura_id from cargo_factura where
>> factura_id=a.id and fecha<=@fecha_fin) and exists (select
>> top 1 id from factura where cargo_t='F' and
>> numero=a.anticipo_fac and imprime=0) THEN round(a.total-
>> a.anticipo+(select sum(importe) from cargo_factura where
>> factura_id=a.id and fecha<=@fecha_fin),2)
>> WHEN not exists (select top 1 factura_id from abn_fac
>> where factura_id=a.id and stafa>0 and fecha<=@fecha_fin
>> and (fecha_canc is null or fecha_canc>@fecha_fin)) and
>> exists (select top 1 factura_id from cargo_factura where
>> factura_id=a.id and fecha<=@fecha_fin) and not exists
>> (select top 1 id from factura where cargo_t='F' and
>> numero=a.anticipo_fac and imprime=0) THEN round(a.total+
>> (select sum(importe) from cargo_factura where
>> factura_id=a.id and fecha<=@fecha_fin),2)
>> WHEN not exists (select top 1 factura_id from abn_fac
>> where factura_id=a.id and stafa>0 and fecha<=@fecha_fin
>> and (fecha_canc is null or fecha_canc>@fecha_fin)) and not
>> exists (select top 1 factura_id from cargo_factura where
>> factura_id=a.id and fecha<=@fecha_fin) and exists (select
>> top 1 id from factura where cargo_t='F' and
>> numero=a.anticipo_fac and imprime=0) THEN round(a.total-
>> a.anticipo,2)
>> else a.total
>> END,
>> a.stfac, 1 as orden, b.cliente_t
>> from factura as a, cliente as b where
>> a.cte_numero=b.numero and a.fecha_salvta<=@fecha_fin
>> and /*a.stfac>1 and*/ a.cargo_t='F' and a.cte_numero like
>> @cte_numero and a.dep_id like @dep_id
>>
>> union all
>>
>> select @id, a.id as factura_id, cargo_t, a.folio_fiscal,
>> a.dep_id, a.cte_numero, a.fecha_venc, a.fecha_pago,
>> a.fecha as fecha, forma_pago_t, a.total,
>> 'saldo_doc'ÊSE
>> WHEN exists (select top 1 factura_id from abn_fac where
>> factura_id=a.id and stafa>0 and fecha<=@fecha_fin and
>> (fecha_canc is null or fecha_canc>@fecha_fin)) THEN round
>> (a.total-(select sum(importe) from abn_fac where
>> factura_id=a.id and stafa>0 and fecha<=@fecha_fin and
>> (fecha_canc is null or fecha_canc>@fecha_fin)),2)
>> ELSE a.total
>> END,
>> a.stfac, 1 as orden,
>> b.cliente_t
>> 02/Jun/2004)
>> from factura as a, cliente as b where
>> a.cte_numero=b.numero and a.fecha<=@fecha_fin /*and
>> a.stfac>1*/ and a.cargo_t in
>> ('R','C','D','U','O','X','H','M','Y','W') and a.cte_numero
>> like @cte_numero and a.dep_id like @dep_id
>>
>>
>>
>> union all
>>
>> select @id, null as factura_id, abono_t, a.folio,
>> a.dep_id, a.cte_numero, null as fecha_venc, null as
>> fecha_pago, a.fecha, null as forma_pago_t, a.total,
>> 'saldo_doc'ÊSE
>> WHEN exists (select top 1 factura_id from abn_fac where
>> abn_numero=a.numero and abn_abono_t=a.abono_t and
>> fecha<=@fecha_fin and (fecha_canc is null or
>> fecha_canc>@fecha_fin)) THEN round(a.total-(select sum
>> (importe) from abn_fac where abn_numero=a.numero and
>> abn_abono_t=a.abono_t and fecha<=@fecha_fin and
>> (fecha_canc is null or fecha_canc>@fecha_fin)),2)
>> ELSE a.total
>> END,
>> a.stabn, 3 as orden, b.cliente_t
>> from view_fecha_abono as a, cliente as b where
>> a.cte_numero=b.numero and a.fecha<=@fecha_fin and
>> a.stabn>0 and a.cte_numero like @cte_numero and a.dep_id
>> like @dep_id
>> return
>>
>> GO
>>
>>> Hola Jose:
>>> Fuerza la recompilacion del procedimiento almacenado:
>>> => >>> EXEC sp_recompile MiProcedimientoAlmacenado
>>> => >>> Si no te funciona podrias, postear el codigo del
>> Procedimiento?
>>>
>>>
>>> Javier Loria
>>> Costa Rica
>>> Se aprecia la inclusion de DDL (CREATE, INSERTS, etc.)
>>> que pueda ser copiado y pegado al Query Analizer.
>>> La version de SQL y Service Pack tambien ayuda.
>>> jose maria escribio:
>>>> Saludos, tengo un procedimiento almacenado que me
>> consume
>>>> el 99% del cpu de mi servidor cada vez que se ejecuta
>> por
>>>> un lapso de 8 min.
>>>>
>>>> Si el mismo codigo lo saco del procedimiento almacenado
>> y
>>>> lo ejecuto por fuera, hace la misma operacion pero en
>>>> 30seg.
>>>>
>>>> me gustaria saber si alguien sabe a que se deba esto, si
>>>> sql server le da un tratamiento especial al
>> procedimiento
>>>> y si es asi como puedo mejorar el performance o
>> configurar
>>>> el server. gracias
>>>
>>>
>>> .


email Siga el debate Respuesta Responder a este mensaje
Ads by Google
Help Hacer una pregunta AnteriorRespuesta Tengo una respuesta
Search Busqueda sugerida