Club Delphi  
    FTP   CCD     Buscar   Trucos   Trabajo   Foros

Retroceder   Foros Club Delphi > Principal > SQL
Registrarse FAQ Miembros Calendario Guía de estilo Temas de Hoy

Respuesta
 
Herramientas Buscar en Tema Desplegado
  #1  
Antiguo 01-04-2006
Avatar de AGAG4
AGAG4 AGAG4 is offline
Miembro
 
Registrado: ago 2004
Ubicación: Los Mochis, Sinaloa, México
Posts: 1.420
Poder: 21
AGAG4 Va por buen camino
Consulta Super Lenta

Bienvenido Club Delphi, y muchas Felicidades por su nuevo aspecto.

Uso Firebird 1.53 con IBExpert 2006

Buen día tengan compañeros, les informo mi problema, tengo una consulta que tiene la finalidad de Sacar las Ventas Netas = Ventas - (Devoluciones + Notas de Crédito) del Año Actual y el Año Anterior, ahora bien, todo marcha bien hasta toparme con el ORDER BY , del cual quiero ordenar por el TOTAL del Año Actual Descendentemente donde este campo Virtual esta posicionado en el campo 6 de la propia consulta pero si la ejecuto tarda más de 6 minutos, pongo la Sentencia SQL:
Código SQL [-]
SELECT FIRST 20 F.CLAVEART,A.DESART,A.MARCA,A.GRUPO,
  COALESCE(SUM(F.CANTIDAD),0)-
  (SELECT COALESCE(SUM(CANTIDAD),0) FROM TINV_INVMOVIM
   WHERE (TIPOMOV='DM' OR TIPOMOV='DX') AND
   CLAVEART = F.CLAVEART AND
   FECHAMOV BETWEEN '01.01.2006' AND '24.03.2006') CANTIDAD,
  COALESCE(SUM(F.IMPSIVA),0)-
  ((SELECT COALESCE(SUM(IMPORTE),0) FROM TINV_INVMOVIM
    WHERE (TIPOMOV='DM' OR TIPOMOV='DX') AND
    CLAVEART = F.CLAVEART AND
    FECHAMOV BETWEEN '01.01.2006' AND '24.03.2006')+
   (SELECT CASE WHEN COALESCE(SUM(C.IMPARTNC),0)>0 THEN COALESCE(SUM(C.IMPARTNC),0)   ELSE 0 END FROM TCXC_CLIMOVIM C
    RIGHT JOIN TFAC_FACTURAS FF ON (FF.TIPOMOV = SUBSTRING(C.FACTURA FROM 1 FOR 2) AND FF.FOLIOFISCAL= CAST(SUBSTRING(C.FACTURA FROM 3 FOR 6) AS INTEGER))
    RIGHT JOIN TFAC_DFACTURAS FD ON (FD.TIPOMOV = FF.TIPOMOV AND FD.FOLIO = FF.FOLIO AND FD.CLAVEART=F.CLAVEART)
    WHERE (TIPOMOV='NM' OR TIPOMOV='NX') AND
    FECHAMOV BETWEEN '01.01.2006' AND '24.03.2006')) TOTAL,
   (SELECT COALESCE(SUM(IMPSIVA),0) FROM TFAC_DFACTURAS
    WHERE CLAVEART = F.CLAVEART AND TIPOMOV ='FM' AND
    FECHAFAC BETWEEN '01.01.2005' AND '24.03.2005') -
   ((SELECT COALESCE(SUM(IMPORTE),0) FROM TINV_INVMOVIM
     WHERE (TIPOMOV='DM' OR TIPOMOV='DX') AND
     CLAVEART = F.CLAVEART AND
     FECHAMOV BETWEEN '01.01.2005' AND '24.03.2005') +
    (SELECT CASE WHEN COALESCE(SUM(C.IMPARTNC),0)>0 THEN COALESCE(SUM(IMPARTNC),0) ELSE 0 END FROM TCXC_CLIMOVIM C
     RIGHT JOIN TFAC_FACTURAS FF ON (FF.TIPOMOV = SUBSTRING(C.FACTURA FROM 1 FOR 2) AND FF.FOLIOFISCAL = CAST(SUBSTRING(C.FACTURA FROM 3 FOR 6) AS INTEGER))
     RIGHT JOIN TFAC_DFACTURAS FD ON (FD.TIPOMOV = FF.TIPOMOV AND FD.FOLIO = FF.FOLIO AND FD.CLAVEART=F.CLAVEART)
     WHERE (TIPOMOV='NM' OR TIPOMOV='NX') AND
     FECHAMOV BETWEEN '01.01.2005' AND '24.03.2005')) TOTANT,
    (SELECT COALESCE(SUM(CANTIDAD),0) FROM TFAC_DFACTURAS
     WHERE CLAVEART = F.CLAVEART AND
     TIPOMOV ='FM' AND
     FECHAFAC BETWEEN '01.01.2005' AND '24.03.2005') -
    (SELECT COALESCE(SUM(CANTIDAD),0) FROM TINV_INVMOVIM
     WHERE (TIPOMOV='DM' OR TIPOMOV='DX') AND
     CLAVEART = F.CLAVEART AND
     FECHAMOV BETWEEN '01.01.2005' AND '24.03.2005') CANTANT
FROM TFAC_DFACTURAS F
JOIN TINV_ARTICULOS  A ON (F.CLAVEART = A.CLAVEART)
WHERE F.FECHAFAC BETWEEN '01.01.2006' AND '24.03.2006' AND
F.TIPOMOV ='FM'
GROUP BY F.CLAVEART,A.DESART,A.MARCA,A.GRUPO
ORDER BY 6 DESC

Ahora bien, si cambio el Order By a :
ORDER BY COALESCE(SUM(F.IMPSIVA),0) DESC
tarda 6 Segundos, pero no me ordenará correctamente la consulta debido a que solo estoy ordenando las VENTAS y no estoy considerando las
Devoluciones y Notas de Crédito, mi duda es la siguiente:
Será lo mismo poner ORDER BY TOTAL DESC que ORDER BY 6 DESC ????
en el primero me marca error que columna no se reconoce, lo que pasa es que la columna virtual TOTAL tiene el importe TOTAL NETO que quiero ordenar, mi objetivo es Disminuir el tiempo de la consulta a 10 ó 20 segundos, espero que alguien me haya entendido, si no estoy a sus ordenes.

Nota: Al poner ORDER BY 6 DESC si me hace el ordenamiento correcto pero la consulta es demasiada lenta, con decirles que si quito el ORDER BY 6 DESC y First 20 la consulta tarda 10 Segundos para traerme todos los registros y apenas son 5,000 Registros

Se podrá Crear un INDICE TEMPORAL PARA EL CAMPO CALCULADO TOTAL ????

Agradezco cualquier Ayuda.

Última edición por AGAG4 fecha: 01-04-2006 a las 18:12:10. Razón: Corrección
Responder Con Cita
  #2  
Antiguo 01-04-2006
FOURIER FOURIER is offline
Miembro
 
Registrado: dic 2005
Posts: 40
Poder: 0
FOURIER Va por buen camino
Hola

Pues Yo Probaria
Código Delphi [-]
Select First 20 F.claveart,a.desart,a.marca,a.grupo,  Coalesce(sum(f.cantidad),0)-  (select Coalesce(sum(cantidad),0) From 
Tinv_invmovim   Where (tipomov='dm' Or Tipomov='dx') And   Claveart = F.claveart And   Fechamov Between '01.01.2006' And 
'24.03.2006') cantidad,  Coalesce(sum(f.impsiva),0)-  ((select Coalesce(sum(importe),0) From Tinv_invmovim    Where 
(tipomov='dm' Or Tipomov='dx') And    Claveart = F.claveart And    Fechamov Between '01.01.2006' And '24.03.2006')+   (select 
Case When Coalesce(sum(c.impartnc),0)>0 Then Coalesce(sum(c.impartnc),0)   Else 0 End From Tcxc_climovim C    Right Join 
Tfac_facturas Ff On (ff.tipomov = Substring(c.factura From 1 For 2) And Ff.foliofiscal= Cast(substring(c.factura From 3 For 6) As 
Integer))    Right Join Tfac_dfacturas Fd On (fd.tipomov = Ff.tipomov And Fd.folio = Ff.folio And Fd.claveart=f.claveart)    Where 
(tipomov='nm' Or Tipomov='nx') And    Fechamov Between '01.01.2006' And '24.03.2006')) total,   (select 
Coalesce(sum(impsiva),0) From Tfac_dfacturas    Where Claveart = F.claveart And Tipomov ='fm' And    Fechafac Between 
'01.01.2005' And '24.03.2005') -   ((select Coalesce(sum(importe),0) From Tinv_invmovim     Where (tipomov='dm' Or Tipomov='dx')
 And     Claveart = F.claveart And     Fechamov Between '01.01.2005' And '24.03.2005') +    (select Case When 
Coalesce(sum(c.impartnc),0)>0 Then Coalesce(sum(impartnc),0) Else 0 End From Tcxc_climovim C     Right Join Tfac_facturas Ff 
On (ff.tipomov = Substring(c.factura From 1 For 2) And Ff.foliofiscal = Cast(substring(c.factura From 3 For 6) As Integer))     Right
 Join Tfac_dfacturas Fd On (fd.tipomov = Ff.tipomov And Fd.folio = Ff.folio And Fd.claveart=f.claveart)     Where (tipomov='nm' Or 
Tipomov='nx') And     Fechamov Between '01.01.2005' And '24.03.2005')) totant,    (select Coalesce(sum(cantidad),0) From 
Tfac_dfacturas     Where Claveart = F.claveart And     Tipomov ='fm' And     Fechafac Between '01.01.2005' And '24.03.2005') -    
(select Coalesce(sum(cantidad),0) From Tinv_invmovim     Where (tipomov='dm' Or Tipomov='dx') And     Claveart = F.claveart And    
 Fechamov Between '01.01.2005' And '24.03.2005') cantantfrom Tfac_dfacturas Fjoin Tinv_articulos  A On (f.claveart = 
A.claveart)where F.fechafac Between '01.01.2006' And '24.03.2006' Andf.tipomov ='fm'group By 
F.claveart,a.desart,a.marca,a.grupoorder By   Coalesce(sum(f.cantidad),0)-  (select Coalesce(sum(cantidad),0) From 
Tinv_invmovim   Where (tipomov='dm' Or Tipomov='dx') And   Claveart = F.claveart And   Fechamov Between '01.01.2006' And 
'24.03.2006') cantidad,  Coalesce(sum(f.impsiva),0)-  ((select Coalesce(sum(importe),0) From Tinv_invmovim    Where 
(tipomov='dm' Or Tipomov='dx') And    Claveart = F.claveart And    Fechamov Between '01.01.2006' And '24.03.2006')+   (select 
Case When Coalesce(sum(c.impartnc),0)>0 Then Coalesce(sum(c.impartnc),0)   Else 0 End From Tcxc_climovim C    Right Join 
Tfac_facturas Ff On (ff.tipomov = Substring(c.factura From 1 For 2) And Ff.foliofiscal= Cast(substring(c.factura From 3 For 6) As 
Integer))    Right Join Tfac_dfacturas Fd On (fd.tipomov = Ff.tipomov And Fd.folio = Ff.folio And Fd.claveart=f.claveart)    Where 
(tipomov='nm' Or Tipomov='nx') And    Fechamov Between '01.01.2006' And '24.03.2006')) total,   (select 
Coalesce(sum(impsiva),0) From Tfac_dfacturas    Where Claveart = F.claveart And Tipomov ='fm' And    Fechafac Between 
'01.01.2005' And '24.03.2005') -   ((select Coalesce(sum(importe),0) From Tinv_invmovim     Where (tipomov='dm' Or Tipomov='dx')
 And     Claveart = F.claveart And     Fechamov Between '01.01.2005' And '24.03.2005') +    (select Case When 
Coalesce(sum(c.impartnc),0)>0 Then Coalesce(sum(impartnc),0) Else 0 End From Tcxc_climovim C     Right Join Tfac_facturas Ff 
On (ff.tipomov = Substring(c.factura From 1 For 2) And Ff.foliofiscal = Cast(substring(c.factura From 3 For 6) As Integer))     Right
 Join Tfac_dfacturas Fd On (fd.tipomov = Ff.tipomov And Fd.folio = Ff.folio And Fd.claveart=f.claveart)     Where (tipomov='nm' Or 
Tipomov='nx') And     Fechamov Between '01.01.2005' And '24.03.2005')) totant,    (select Coalesce(sum(cantidad),0) From 
Tfac_dfacturas     Where Claveart = F.claveart And     Tipomov ='fm' And     Fechafac Between '01.01.2005' And '24.03.2005') -    
(select Coalesce(sum(cantidad),0) From Tinv_invmovim     Where (tipomov='dm' Or Tipomov='dx') And     Claveart = F.claveart And    
 Fechamov Between '01.01.2005' And '24.03.2005') Desc

Última edición por marcoszorrilla fecha: 01-04-2006 a las 22:26:01.
Responder Con Cita
  #3  
Antiguo 01-04-2006
FOURIER FOURIER is offline
Miembro
 
Registrado: dic 2005
Posts: 40
Poder: 0
FOURIER Va por buen camino
Pues Yo Probaria

Código Delphi [-]
Select First 20 F.claveart,a.desart,a.marca,a.grupo, Coalesce(sum(f.cantidad),0)- (select Coalesce(sum(cantidad),0) From Tinv_invmovim Where (tipomov='dm' Or Tipomov='dx') And Claveart = F.claveart And Fechamov Between '01.01.2006' And '24.03.2006') cantidad, Coalesce(sum(f.impsiva),0)- ((select Coalesce(sum(importe),0) From Tinv_invmovim Where (tipomov='dm' Or Tipomov='dx') And Claveart = F.claveart And Fechamov Between '01.01.2006' And '24.03.2006')+ (select Case When Coalesce(sum(c.impartnc),0)>0 Then Coalesce(sum(c.impartnc),0) Else 0 End From Tcxc_climovim C Right Join Tfac_facturas Ff On (ff.tipomov = Substring(c.factura From 1 For 2) And Ff.foliofiscal= Cast(substring(c.factura From 3 For 6) As Integer)) Right Join Tfac_dfacturas Fd On (fd.tipomov = Ff.tipomov And Fd.folio = Ff.folio And Fd.claveart=f.claveart) Where (tipomov='nm' Or Tipomov='nx') And Fechamov Between '01.01.2006' And '24.03.2006')) total, (select Coalesce(sum(impsiva),0) From Tfac_dfacturas Where Claveart = F.claveart And Tipomov ='fm' And Fechafac Between '01.01.2005' And '24.03.2005') - ((select Coalesce(sum(importe),0) From Tinv_invmovim Where (tipomov='dm' Or Tipomov='dx') And Claveart = F.claveart And Fechamov Between '01.01.2005' And '24.03.2005') + (select Case When Coalesce(sum(c.impartnc),0)>0 Then Coalesce(sum(impartnc),0) Else 0 End From Tcxc_climovim C Right Join Tfac_facturas Ff On (ff.tipomov = Substring(c.factura From 1 For 2) And Ff.foliofiscal = Cast(substring(c.factura From 3 For 6) As Integer)) Right Join Tfac_dfacturas Fd On (fd.tipomov = Ff.tipomov And Fd.folio = Ff.folio And Fd.claveart=f.claveart) Where (tipomov='nm' Or Tipomov='nx') And Fechamov Between '01.01.2005' And '24.03.2005')) totant, (select Coalesce(sum(cantidad),0) From Tfac_dfacturas Where Claveart = F.claveart And Tipomov ='fm' And Fechafac Between '01.01.2005' And '24.03.2005') - (select Coalesce(sum(cantidad),0) From Tinv_invmovim Where (tipomov='dm' Or Tipomov='dx') And Claveart = F.claveart And Fechamov Between '01.01.2005' And '24.03.2005') cantantfrom Tfac_dfacturas Fjoin Tinv_articulos A On (f.claveart = A.claveart)where F.fechafac Between '01.01.2006' And '24.03.2006' Andf.tipomov ='fm'group By F.claveart,a.desart,a.marca,a.grupoorder By Coalesce(sum(f.cantidad),0)- (select Coalesce(sum(cantidad),0) From Tinv_invmovim Where (tipomov='dm' Or Tipomov='dx') And Claveart = F.claveart And Fechamov Between '01.01.2006' And '24.03.2006') cantidad, Coalesce(sum(f.impsiva),0)- ((select Coalesce(sum(importe),0) From Tinv_invmovim Where (tipomov='dm' Or Tipomov='dx') And Claveart = F.claveart And Fechamov Between '01.01.2006' And '24.03.2006')+ (select Case When Coalesce(sum(c.impartnc),0)>0 Then Coalesce(sum(c.impartnc),0) Else 0 End From Tcxc_climovim C Right Join Tfac_facturas Ff On (ff.tipomov = Substring(c.factura From 1 For 2) And Ff.foliofiscal= Cast(substring(c.factura From 3 For 6) As Integer)) Right Join Tfac_dfacturas Fd On (fd.tipomov = Ff.tipomov And Fd.folio = Ff.folio And Fd.claveart=f.claveart) Where (tipomov='nm' Or Tipomov='nx') And Fechamov Between '01.01.2006' And '24.03.2006')) total, (select Coalesce(sum(impsiva),0) From Tfac_dfacturas Where Claveart = F.claveart And Tipomov ='fm' And Fechafac Between '01.01.2005' And '24.03.2005') - ((select Coalesce(sum(importe),0) From Tinv_invmovim Where (tipomov='dm' Or Tipomov='dx') And Claveart = F.claveart And Fechamov Between '01.01.2005' And '24.03.2005') + (select Case When Coalesce(sum(c.impartnc),0)>0 Then Coalesce(sum(impartnc),0) Else 0 End From Tcxc_climovim C Right Join Tfac_facturas Ff On (ff.tipomov = Substring(c.factura From 1 For 2) And Ff.foliofiscal = Cast(substring(c.factura From 3 For 6) As Integer)) Right Join Tfac_dfacturas Fd On (fd.tipomov = Ff.tipomov And Fd.folio = Ff.folio And Fd.claveart=f.claveart) Where (tipomov='nm' Or Tipomov='nx') And Fechamov Between '01.01.2005' And '24.03.2005')) totant, (select Coalesce(sum(cantidad),0) From Tfac_dfacturas Where Claveart = F.claveart And Tipomov ='fm' And Fechafac Between '01.01.2005' And '24.03.2005') - (select Coalesce(sum(cantidad),0) From Tinv_invmovim Where (tipomov='dm' Or Tipomov='dx') And Claveart = F.claveart And Fechamov Between '01.01.2005' And '24.03.2005') Desc
Responder Con Cita
  #4  
Antiguo 02-04-2006
Sick boy Sick boy is offline
Miembro
 
Registrado: may 2003
Ubicación: Cantabria
Posts: 245
Poder: 21
Sick boy Va por buen camino
Pues yo me he perdido !!!!

El primer mensaje se lee, pero los otros dos no hay quien los entienda.

Una pregunta, si usas FireBird, por que no pruebas los MDO?? Me parece que iran más rapido.

Cita:
Se podrá Crear un INDICE TEMPORAL PARA EL CAMPO CALCULADO TOTAL ????
No lo creo, lo unico que se me ocurre es crear una vista que incluya el campo total y realizar un indice descendente sobre él.

Cita:
Nota: Al poner ORDER BY 6 DESC si me hace el ordenamiento correcto pero la consulta es demasiada lenta, con decirles que si quito el ORDER BY 6 DESC y First 20 la consulta tarda 10 Segundos para traerme todos los registros y apenas son 5,000 Registros
El problema esta, casi seguro, en el DESC. IB/FB no es muy eficiente ordenando descendentemente a no ser que tengas un indice.

Prueba dejando el ORDER BY 6, y comprueba si la velocidad es suficiente.

Si el DESC es obligatorio en tu caso, lo unico que se me ocurre para ganar velocidad son vistas.
Responder Con Cita
  #5  
Antiguo 03-04-2006
Avatar de AGAG4
AGAG4 AGAG4 is offline
Miembro
 
Registrado: ago 2004
Ubicación: Los Mochis, Sinaloa, México
Posts: 1.420
Poder: 21
AGAG4 Va por buen camino
ok

Gracias por su tiempo en elaborar sus respuestas, pero he intentado crear vistas, y no puedo como verán uso 3 tablas para determinar las Ventas Netas, donde los campos que estan dentro del Where son IGUALES como el TIPOMOV por lo que para una vista mandarle el VALOR a este campo a cada TABLA ignoro y no tengo ídea como hacerlo, aparte en las vistas no se pueden mandar parámetros como los SP, las vistas son producto de 1 ó varias tablas pero con diferentes campos, espero haberme explicado.

Gracias por su tiempo.
Responder Con Cita
Respuesta



Normas de Publicación
no Puedes crear nuevos temas
no Puedes responder a temas
no Puedes adjuntar archivos
no Puedes editar tus mensajes

El código vB está habilitado
Las caritas están habilitado
Código [IMG] está habilitado
Código HTML está deshabilitado
Saltar a Foro

Temas Similares
Tema Autor Foro Respuestas Último mensaje
Primera consulta, mas lenta que el caballo del malo papulo Conexión con bases de datos 20 23-09-2005 13:46:24
Problamas consulta SQL gabsanar Firebird e Interbase 2 07-02-2005 09:33:05
Consulta muy lenta Walterdf Conexión con bases de datos 2 25-08-2004 18:37:57
Urgente!!! Problema con Consulta SQL PaLaSaca SQL 2 06-05-2004 19:56:50
lenta la consulta. digital Conexión con bases de datos 2 10-09-2003 15:38:13


La franja horaria es GMT +2. Ahora son las 17:41:53.


Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Traducción al castellano por el equipo de moderadores del Club Delphi
Copyright 1996-2007 Club Delphi