Results 1 to 14 of 14

Thread: [Solved] Slow SQL & DoEvents...

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Sep 2005
    Location
    Tijuana,Mexico
    Posts
    109

    Resolved [Solved] Slow SQL & DoEvents...

    Hi!.. I have a question for you guys... but first the data.. I'm working with SQL Server DB, under Windows 2K. The DB has around 5K of records


    I have this SQL statement:

    VB Code:
    1. DoEvents
    2. sql = "SELECT BeOrdenTrabajo.Orden, BeOrdenTrabajo.ID, Rack, (Fecha3TFIn - FechaWO) as TotalTime, BeOrdenTrabajo.Carga, Descarga, " _
    3.    & " (SELECT TOP 1 BeModelos.Modelo FROM BeRequisicion,BeModelos WHERE BeOrdenTrabajo.Orden " _
    4.    & " = BeRequisicion.orden AND BeRequisicion.Modelo = BeModelos.Modelo GROUP BY BeModelos.Modelo) as MyModel, " _
    5.    & " (SELECT TOP 1 BeModelos.TiempoBI FROM BeRequisicion,BeModelos WHERE BeOrdenTrabajo.Orden " _
    6.    & " = BeRequisicion.orden AND BeRequisicion.Modelo = BeModelos.Modelo GROUP BY BeModelos.TiempoBI) as MyBI, " _
    7.    & " (DateAdd(Hour,-(SELECT TOP 1 BeModelos.TiempoBI FROM BeRequisicion,BeModelos WHERE BeOrdenTrabajo.Orden " _
    8.    & " = BeRequisicion.orden AND BeRequisicion.Modelo = BeModelos.Modelo GROUP BY BeModelos.TiempoBI)," _
    9.    & "(Fecha3TFIn - FechaWO))) as Dif FROM BeOrdenTrabajo WHERE  Proceso > 3 ORDER BY Dif DESC"
    10. RSBIData.CursorLocation = adUseClient
    11. RSBIData.Open sql, frmDisplay.eabscn, adOpenStatic, adLockOptimistic

    While doing this process I'm trying to show a blinking label to indicates that the program is doing something (For that I use a timer) so I use a DoEvents just before the SQL.

    What I expected was to have a runnign process, slow process, and a blinking label telling that the program is working even if it looks like is freezed. But what I have is the running process, slow of course, and a freezed label so the program looks like dead... then my questions are:...

    a) Is there a way to improve the speed of the SQL statement (Right now takes around 15-20 seconds to complete)
    b) What can I do to show the make the label "blink" while processing the SQL
    Last edited by G-Hawk; Dec 15th, 2006 at 04:56 PM. Reason: Solved

  2. #2
    PowerPoster lintz's Avatar
    Join Date
    Mar 2003
    Location
    The 19th Hole
    Posts
    2,697

    Re: Slow SQL & DoEvents....

    Add your "blinking label" code to your timer.

  3. #3

    Thread Starter
    Lively Member
    Join Date
    Sep 2005
    Location
    Tijuana,Mexico
    Posts
    109

    Re: Slow SQL & DoEvents....

    Hi Lintz... I forgot to tell that my "blinking label" code is already on the timer... the situation is that the timer is not fired while the SQL statement is running...

  4. #4
    PowerPoster lintz's Avatar
    Join Date
    Mar 2003
    Location
    The 19th Hole
    Posts
    2,697

    Re: Slow SQL & DoEvents....

    What interval is your timer set to? Does the Timer code get triggered at all?

  5. #5

    Thread Starter
    Lively Member
    Join Date
    Sep 2005
    Location
    Tijuana,Mexico
    Posts
    109

    Re: Slow SQL & DoEvents....

    Well... the timer is set to a 500ms.. but the strange thing I just realized because of your question is that if I let the timer enabled then the timer gets triggered as soons as I enter the form where the timer is .. but if I set enable to false.. and then I set it to TRUE when I need it then the timer never gets triggered... .. so now I have another question... Why this is happenig?... (That took me completely unguard... )

  6. #6
    PowerPoster lintz's Avatar
    Join Date
    Mar 2003
    Location
    The 19th Hole
    Posts
    2,697

    Re: Slow SQL & DoEvents....

    One option I thought of is to move your query to the timer. (that way you'll know it will get triggered)

    VB Code:
    1. Dim DoQuery As Boolean
    2.  
    3. Sub Form_Load()
    4.  
    5. 'Do other stuff here before query
    6.  
    7. Timer1.Interval = 500'set interval
    8. Timer1.Enabled = True 'start timer
    9.  
    10. End Sub
    11.  
    12. Sub Timer1_Timer()
    13.  
    14. If DoQuery = False Then
    15. DoQuery = True 'set to true so we don't continue to call it
    16.  
    17. DoEvents
    18.  
    19. sql = "SELECT BeOrdenTrabajo.Orden, BeOrdenTrabajo.ID, Rack, (Fecha3TFIn - FechaWO) as TotalTime, BeOrdenTrabajo.Carga, Descarga, " _
    20.    & " (SELECT TOP 1 BeModelos.Modelo FROM BeRequisicion,BeModelos WHERE BeOrdenTrabajo.Orden " _
    21.    & " = BeRequisicion.orden AND BeRequisicion.Modelo = BeModelos.Modelo GROUP BY BeModelos.Modelo) as MyModel, " _
    22.    & " (SELECT TOP 1 BeModelos.TiempoBI FROM BeRequisicion,BeModelos WHERE BeOrdenTrabajo.Orden " _
    23.    & " = BeRequisicion.orden AND BeRequisicion.Modelo = BeModelos.Modelo GROUP BY BeModelos.TiempoBI) as MyBI, " _
    24.    & " (DateAdd(Hour,-(SELECT TOP 1 BeModelos.TiempoBI FROM BeRequisicion,BeModelos WHERE BeOrdenTrabajo.Orden " _
    25.    & " = BeRequisicion.orden AND BeRequisicion.Modelo = BeModelos.Modelo GROUP BY BeModelos.TiempoBI)," _
    26.    & "(Fecha3TFIn - FechaWO))) as Dif FROM BeOrdenTrabajo WHERE  Proceso > 3 ORDER BY Dif DESC"
    27. RSBIData.CursorLocation = adUseClient
    28. RSBIData.Open sql, frmDisplay.eabscn, adOpenStatic, adLockOptimistic
    29.  
    30. DoQuery = False ' reset
    31. Timer1.Enabled = False ' stop timer
    32. End If
    33.  
    34. DoEvents
    35. 'add label flickering code here
    36.  
    37. End Sub

  7. #7
    MS SQL Powerposter szlamany's Avatar
    Join Date
    Mar 2004
    Location
    Connecticut
    Posts
    18,263

    Re: Slow SQL & DoEvents....

    That query has some problems.

    SELECT TOP 1 is never used with GROUP BY - they are mutually exclusive...

    Why all those sub-queries - they are very expensive.

    5000 rows is a truly small amount for MS SQL to handle.

    You should concentrate on re-working that QUERY in QUERY ANALYZER so that it runs as it should - in under a second - and the problem will go away.

    Also - since this is MS SQL SERVER start using proper JOIN syntax.

    Don't do:

    FROM TABLEA, TABLEB

    instead do:

    FROM TABLEA LEFT JOIN TABLEB ON TABLEB.KEYCOL=TABLEA.KEYCOL

    and also make sure that you have proper indexes on the JOIN columns.

    *** Read the sticky in the DB forum about how to get your question answered quickly!! ***

    Please remember to rate posts! Rate any post you find helpful - even in old threads! Use the link to the left - "Rate this Post".

    Some Informative Links:
    [ SQL Rules to Live By ] [ Reserved SQL keywords ] [ When to use INDEX HINTS! ] [ Passing Multi-item Parameters to STORED PROCEDURES ]
    [ Solution to non-domain Windows Authentication ] [ Crazy things we do to shrink log files ] [ SQL 2005 Features ] [ Loading Pictures from DB ]

    MS MVP 2006, 2007, 2008

  8. #8

    Thread Starter
    Lively Member
    Join Date
    Sep 2005
    Location
    Tijuana,Mexico
    Posts
    109

    Re: Slow SQL & DoEvents....

    @ Lintz:
    I just left some code, without the SQL statement but... still once I enable the timer never gets triggered!!! I'm confused... why this could be happening?... I don't know what is the problem with the timer?... there is something I have been missing regarding to that?...

    @ Szlamany:
    Tnaks for the reply I also believe that the query must have something wrong... so I follow your advice... but now that I'm trying to check the Query the SQL Query Analyser tells me that there is an error "Identifier to long maximum length is 128"... any idea about that (I have never use SQL Query Analyser)

  9. #9

    Thread Starter
    Lively Member
    Join Date
    Sep 2005
    Location
    Tijuana,Mexico
    Posts
    109

    Re: Slow SQL & DoEvents....

    @ Szlamany... I already make it work... I mean I could parse my Query but what's next I couldn't see any "improve query"... or something like that...

  10. #10
    Addicted Member
    Join Date
    Jul 2006
    Posts
    147

    Re: Slow SQL & DoEvents....

    Were all those sub querys really needed? probley sigh..
    theres no way I will try figure that out.

    Why have you only used the table name on certain columns?
    "SELECT BeOrdenTrabajo.Orden, BeOrdenTrabajo.ID, Rack,"

    Don't kill me if this is a stupid question... but why do you select the value from
    BeModelos.Modelo
    which you already got while joining to
    BeRequisicion.Modelo

    It is hard for me without knowing your tables/data etc... sigh to new to this
    wouldnt this work? (kill me for not using join later.)

    Code:
    (SELECT BeRequisicion.Modelo
    FROM BeRequisicion, BeOrdenTrabajo
    WHERE BeOrdenTrabajo.Orden  = BeRequisicion.orden 
    GROUP BY BeRequisicion.Modelo)
    Last edited by Ishamael; Dec 14th, 2006 at 10:03 PM.

  11. #11
    MS SQL Powerposter szlamany's Avatar
    Join Date
    Mar 2004
    Location
    Connecticut
    Posts
    18,263

    Re: Slow SQL & DoEvents....

    Quote Originally Posted by G-Hawk
    @ Szlamany... I already make it work... I mean I could parse my Query but what's next I couldn't see any "improve query"... or something like that...
    There is no improve query function in QUERY ANALYZER.

    It's just a place where it's easy for you to type in a QUERY and run it. Make simple changes to it and get it to fulfill your needs.

    What I expected back from you was the start of a better structured query - with proper JOIN/ON syntax - with no sub-queries. Once you got the basic query I was going to ask some questions about the table design and help you get the other data - the stuff you sub-query for now...

    It's not going to happen instantly - but you need to take the first step - which is to start with a simpler query in QA that we all can understand and help you expand.

    *** Read the sticky in the DB forum about how to get your question answered quickly!! ***

    Please remember to rate posts! Rate any post you find helpful - even in old threads! Use the link to the left - "Rate this Post".

    Some Informative Links:
    [ SQL Rules to Live By ] [ Reserved SQL keywords ] [ When to use INDEX HINTS! ] [ Passing Multi-item Parameters to STORED PROCEDURES ]
    [ Solution to non-domain Windows Authentication ] [ Crazy things we do to shrink log files ] [ SQL 2005 Features ] [ Loading Pictures from DB ]

    MS MVP 2006, 2007, 2008

  12. #12

    Thread Starter
    Lively Member
    Join Date
    Sep 2005
    Location
    Tijuana,Mexico
    Posts
    109

    Re: Slow SQL & DoEvents....

    @ Szlamany...

    Tnx! for your advices... I already came up with this Query which, as you said, run in less than a second..

    VB Code:
    1. "SELECT BeOrdenTrabajo.Orden, ID, Rack, (Fecha3TFIn - FechaWO) as TotalTime, Carga, Descarga, Modelo,
    2. (datediff(Hour,FechaBin,FechaBout)) as NewBI,
    3. (DateAdd(Hour,- (datediff(Hour,FechaBin,FechaBout)),(Fecha3TFIn - FechaWO))) as Dif
    4. FROM BeOrdenTrabajo LEFT JOIN BeRequisicion ON BeOrdenTrabajo.Orden = BeRequisicion.orden
    5. WHERE Proceso > 3 ORDER BY Dif DESC"

    I appreciate your time and support... Regards!

    Hopefully I'll do the same with other Querys that I have under the same circumstances and I'll then be OK... but if I'm unable to solve I know who can help me with that... thanks...

    Now the only thing missing is the reason why the timer is not fired once I enable it... (even though I might not need it anymore It would be nice to know)...

  13. #13
    MS SQL Powerposter szlamany's Avatar
    Join Date
    Mar 2004
    Location
    Connecticut
    Posts
    18,263

    Re: [Almost Solved] Slow SQL & DoEvents (Timer)....

    I'm very impressed - that query is so much easier to look at and as you said - runs in a second - that's the way to go!

    *** Read the sticky in the DB forum about how to get your question answered quickly!! ***

    Please remember to rate posts! Rate any post you find helpful - even in old threads! Use the link to the left - "Rate this Post".

    Some Informative Links:
    [ SQL Rules to Live By ] [ Reserved SQL keywords ] [ When to use INDEX HINTS! ] [ Passing Multi-item Parameters to STORED PROCEDURES ]
    [ Solution to non-domain Windows Authentication ] [ Crazy things we do to shrink log files ] [ SQL 2005 Features ] [ Loading Pictures from DB ]

    MS MVP 2006, 2007, 2008

  14. #14

    Thread Starter
    Lively Member
    Join Date
    Sep 2005
    Location
    Tijuana,Mexico
    Posts
    109

    Re: [Almost Solved] Slow SQL & DoEvents (Timer)....

    Tnx!... I would make this thread solved and I will open a different one for the timer issue, which was not suppossed to be a problem when I started this thread... anyway thanks to all...

    C-ya!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width