Newbie: how to speed up this simple query

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • François Dejaeghere
    New Member
    • Aug 2007
    • 3

    Newbie: how to speed up this simple query

    Hello dear readers,

    I have 2 tables, let's say
    first table: "Appointmen t", which has 3 fields (I've simplified the schema for better understanding)
    ID as GUID
    (the key)
    Length as Float
    TaskID as GUID
    ((external key)

    The second table is called "Task" and has 2 fields (again, simplified schema)
    ID as GUID (key)
    RequiredLength as Float

    Now the problem:
    In a kind of "Tasks to plane view", I'm pretty sure I can speed things up since I recall a Sum on each criteria .. I'v tried to use alias, but propably missed something because I only get error message on return ("Invalid Column name").

    Here is my query[code=mysql]
    SELECT Task.ID, Task.RequiredLe ngth,
    (SELECT SUM(Length) AS Expr1
    FROM Appointment
    WHERE (TaskID = Task.ID)
    GROUP BY TaskID) AS PlannedLength
    FROM Task
    WHERE ((SELECT SUM(Length) AS Expr1
    FROM Appointment AS Appointment_1
    WHERE (TaskID = Task.ID)
    GROUP BY TaskID) < RequiredLength)
    OR ((SELECT SUM(Length) AS Expr1
    FROM Appointment AS Appointment_1
    WHERE (TaskID = Task.ID)
    GROUP BY TaskID) ) IS NULL

    I have tried the following query, but it does not work :
    SELECT Task.ID, Task.RequiredLe ngth,
    (SELECT SUM(Length) AS Expr1
    FROM Appointment
    WHERE (TaskID = Task.ID)
    GROUP BY TaskID) AS PlannedLength
    FROM Task
    WHERE ((PlannedLength < RequiredLength) OR (PlannedLength IS NULL))[/code]
    '************** *************** *************** *************** ******
    Any help would be greatly appreciated, I'm a real newbie :
    Thank for your answers,
    Happy coding
  • code green
    Recognized Expert Top Contributor
    • Mar 2007
    • 1726

    #2
    A sub query is inherently slow.
    They can usually be replaced by a much faster JOIN.
    This looks like it could be re-written as a JOIN.
    If you are prepared to make an attempt I will help you along.

    Comment

    • François Dejaeghere
      New Member
      • Aug 2007
      • 3

      #3
      Thank you Code green,

      As I said, any help would ge appreciated, so feel free to show me a few guideline or a straight solution for that query

      Thank you in advance
      Regards

      Comment

      • code green
        Recognized Expert Top Contributor
        • Mar 2007
        • 1726

        #4
        Are you really trying to improve the performance of this query?
        I doubt it works.
        I can't re-write it because I can't work out what you are trying to do.

        Comment

        • François Dejaeghere
          New Member
          • Aug 2007
          • 3

          #5
          Ok, thank you for your help anyway

          Comment

          • code green
            Recognized Expert Top Contributor
            • Mar 2007
            • 1726

            #6
            I would like to have a go but I just don't have time to study your query.
            Can you describe what you are trying to achieve?

            Comment

            • pbmods
              Recognized Expert Expert
              • Apr 2007
              • 5821

              #7
              Heya, François.

              Please use CODE tags when posting source code. See the REPLY GUIDELINES on the right side of the page next time you post.

              Comment

              Working...