SQL Optimization

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • blyxx86
    Contributor
    • Nov 2006
    • 258

    #1

    SQL Optimization

    Hey everyone,
    I am attempting to optimize the sql statements I am currently using.

    I have a series of about 5 complex statements. 3 out of 5 of these run very quickly. (I used a timer function to find the time to process.)

    However the other 2 run very slowly (6-8 seconds, where as the others take only a few milliseconds). I am analyzing the statements and have found a probably root cause and am now working on optimizing them, but I need your help.

    The primary reason I found (from testing) is the "OR" part of each query. When I remove the portion after the "OR" the speed goes to the normal speed of about 39 milliseconds.

    My concern is getting these to run more quickly.

    The reason for the OR statement is to show historical data for orders that have been filled, but were not filled in the past. Any dates in the SQL statement are generated dynamically, so they do not always show today's date.

    Any suggestions?

    [code=sql]
    SELECT DISTINCT tblRequest.Cust omerTracking, tblLocations.Lo cationCode, tblRequest.ReqD ate, tblRequest.ReqT ime, [Prefix] & tblModel_1.Mode lName AS PendingModel, tblRequest.ETA, tblRequest.Pend ingCauses, tblRequestDetai l.Quantity*[stdQuantity] AS ReqQty, qry_Qtys.Outsta nding AS QtyOutstanding, tblRequestType. RequestType, tblInvoice.Invo iceDate, tblInvoice.Trac kingNumber

    FROM tblInvoice RIGHT JOIN (((qry_Qtys INNER JOIN (tblRequestType RIGHT JOIN (tblModel AS tblModel_1 INNER JOIN ((((tblCustomer INNER JOIN tblRequest ON tblCustomer.Cus tomerID = tblRequest.Cust omerID) INNER JOIN tblRequestDetai l ON tblRequest.Requ estID = tblRequestDetai l.RequestID) LEFT JOIN tblLocations ON tblRequest.Loca tionID = tblLocations.Lo cationID) INNER JOIN ((tblModel INNER JOIN tblProduct ON tblModel.ModelI D = tblProduct.Mode lID) INNER JOIN tblModelDetail ON tblModel.ModelI D = tblModelDetail. ModelID) ON tblRequestDetai l.ProductID = tblProduct.Prod uctID) ON tblModel_1.Mode lID = tblModelDetail. IncludesModelID ) ON tblRequestType. RequestTypeID = tblRequest.Requ estTypeID) ON qry_Qtys.Reques tID = tblRequest.Requ estID) INNER JOIN tblProduct AS tblProduct_1 ON (tblModel_1.Mod elID = tblProduct_1.Mo delID) AND (qry_Qtys.Produ ctID = tblProduct_1.Pr oductID)) LEFT JOIN tblInvoiceDetai l ON tblRequest.Requ estID = tblInvoiceDetai l.RequestID) ON tblInvoice.Invo iceID = tblInvoiceDetai l.InvoiceID

    WHERE (((tblRequest.R eqDate)<#2/4/2008#) AND (([tblRequestDetai l].[Quantity]*[stdQuantity])>0) AND ((qry_Qtys.Outs tanding)>0) AND ((tblRequestTyp e.RequestTypeID )<>3) AND ((tblRequestDet ail.Cleared)=0) AND ((tblRequest.Cu stomerID)=1) AND ((tblRequest.Ca nceled)<>-1))
    OR
    (((tblRequest.R eqDate)<#2/4/2008#) AND (([tblRequestDetai l].[Quantity]*[stdQuantity])>0) AND ((qry_Qtys.Outs tanding)>=0) AND ((tblInvoice.In voiceDate)>#2/4/2008#) AND ((tblRequestTyp e.RequestTypeID )<>3) AND ((tblRequestDet ail.Cleared)=0) AND ((tblRequest.Cu stomerID)=1) AND ((tblRequest.Ca nceled)<>-1))

    ORDER BY [Prefix] & tblModel_1.Mode lName, tblRequest.Cust omerTracking;
    [/code]


    I suppose that I could choose to only include this OR statement if the user input date is not today's date. This would be a partial fix, but I would still like to increase the speed otherwise.

    [code=sql]
    SELECT DISTINCT tblRequest.Cust omerTracking, tblLocations.Lo cationCode, tblRequest.ReqD ate, tblRequest.ReqT ime, [Prefix] & tblModel_1.Mode lName AS PendingModel, tblRequest.ETA, tblRequest.Pend ingCauses, tblRequestDetai l.Quantity*[stdQuantity] AS ReqQty, qry_Qtys.Outsta nding AS QtyOutstanding, tblRequestType. RequestType, tblInvoice.Invo iceDate, tblInvoice.Trac kingNumber

    FROM tblInvoice RIGHT JOIN (((qry_Qtys INNER JOIN (tblRequestType RIGHT JOIN (tblModel AS tblModel_1 INNER JOIN ((((tblCustomer INNER JOIN tblRequest ON tblCustomer.Cus tomerID = tblRequest.Cust omerID) INNER JOIN tblRequestDetai l ON tblRequest.Requ estID = tblRequestDetai l.RequestID) LEFT JOIN tblLocations ON tblRequest.Loca tionID = tblLocations.Lo cationID) INNER JOIN ((tblModel INNER JOIN tblProduct ON tblModel.ModelI D = tblProduct.Mode lID) INNER JOIN tblModelDetail ON tblModel.ModelI D = tblModelDetail. ModelID) ON tblRequestDetai l.ProductID = tblProduct.Prod uctID) ON tblModel_1.Mode lID = tblModelDetail. IncludesModelID ) ON tblRequestType. RequestTypeID = tblRequest.Requ estTypeID) ON qry_Qtys.Reques tID = tblRequest.Requ estID) INNER JOIN tblProduct AS tblProduct_1 ON (tblModel_1.Mod elID = tblProduct_1.Mo delID) AND (qry_Qtys.Produ ctID = tblProduct_1.Pr oductID)) LEFT JOIN tblInvoiceDetai l ON tblRequest.Requ estID = tblInvoiceDetai l.RequestID) ON tblInvoice.Invo iceID = tblInvoiceDetai l.InvoiceID

    WHERE (((tblRequest.R eqDate)<#2/4/2008#) AND (([tblRequestDetai l].[Quantity]*[stdQuantity])>0) AND ((qry_Qtys.Outs tanding)>0) AND ((tblRequestTyp e.RequestTypeID )<>3) AND ((tblRequestDet ail.Cleared)=0) AND ((tblRequest.Cu stomerID)=1) AND ((tblRequest.Ca nceled)<>-1))
    IF inputDate < Date() THEN --FOR EXAMPLE
    OR
    (((tblRequest.R eqDate)<#2/4/2008#) AND (([tblRequestDetai l].[Quantity]*[stdQuantity])>0) AND ((qry_Qtys.Outs tanding)>=0) AND ((tblInvoice.In voiceDate)>#2/4/2008#) AND ((tblRequestTyp e.RequestTypeID )<>3) AND ((tblRequestDet ail.Cleared)=0) AND ((tblRequest.Cu stomerID)=1) AND ((tblRequest.Ca nceled)<>-1))
    END IF
    ORDER BY [Prefix] & tblModel_1.Mode lName, tblRequest.Cust omerTracking;
    [/code]
  • FishVal
    Recognized Expert Specialist
    • Jun 2007
    • 2656

    #2
    Hi, blyxx.

    Two points:
    • boolean expressions may be treated in terms of boolean algebra
      Expr1 * Expr2 * Expr3 + Expr1 * Expr2 * Expr4 = Expr1 * Expr2 * (Expr3 + Expr4)
      * - AND
      + - OR
      This may give some optimization, though Access might itself optimizes it (not sure)
    • try to filter records before joining (in separate queries)


    Regards,
    Fish

    Comment

    • blyxx86
      Contributor
      • Nov 2006
      • 258

      #3
      Originally posted by FishVal
      Hi, blyxx.

      Two points:
      • boolean expressions may be treated in terms of boolean algebra
        Expr1 * Expr2 * Expr3 + Expr1 * Expr2 * Expr4 = Expr1 * Expr2 * (Expr3 + Expr4)
        * - AND
        + - OR
        This may give some optimization, though Access might itself optimizes it (not sure)
      • try to filter records before joining (in separate queries)


      Regards,
      Fish
      I don't think I've ever seen boolean algebra. I will have to experiment with it to figure it out.

      However, how would you suggest filtering records in this statement? I know it's somewhat messy (it's the final query that feeds to the reporting page on the web server).

      Do you suggest making queries like... "qry_AllShipmen ts" "qry_Models"... etc? Is that what you mean?

      Comment

      • FishVal
        Recognized Expert Specialist
        • Jun 2007
        • 2656

        #4
        Originally posted by blyxx86
        I don't think I've ever seen boolean algebra. I will have to experiment with it to figure it out.
        Without getting deep into the question - AND/OR are like */- in regular algebra. In this particular case this means that expression like
        Expr1 AND Expr2 AND Expr3 OR Expr1 AND Expr2 AND Expr4
        equals to
        Expr1 AND Expr2 AND (Expr3 OR Expr4)

        However, how would you suggest filtering records in this statement? I know it's somewhat messy (it's the final query that feeds to the reporting page on the web server).

        Do you suggest making queries like... "qry_AllShipmen ts" "qry_Models"... etc? Is that what you mean?
        Yea. This is what I've meant. I'm not sure whether it will help, but it looks like worth to try.

        Regards,
        Fish

        Comment

        • blyxx86
          Contributor
          • Nov 2006
          • 258

          #5
          Originally posted by FishVal
          Without getting deep into the question - AND/OR are like */- in regular algebra. In this particular case this means that expression like
          Expr1 AND Expr2 AND Expr3 OR Expr1 AND Expr2 AND Expr4
          equals to
          Expr1 AND Expr2 AND (Expr3 OR Expr4)

          Yea. This is what I've meant. I'm not sure whether it will help, but it looks like worth to try.

          Regards,
          Fish
          I did a few small tables to figure out the logic behind boolean algebra. I understood that it is basically multiplying 0's and 1's... I managed to get:
          [code=sql]
          (([tblRequestDetai l].[Cleared]+[tblRequest].[Canceled])=0)
          [/code]

          I do understand the boolean algebra, now that I have used it once. It just sounds more complicated than it really is.

          I will take a look at what separate queries I can create to consolidate them into to improve the time.

          However, I do believe I have overcome a hurdle.. Of course it was a design flaw on my part. :)

          Below is the new sql...
          [code=sql]
          SELECT DISTINCT tblRequest.Cust omerTracking, tblLocations.Lo cationCode, tblRequest.ReqD ate, tblRequest.ReqT ime, [Prefix] & tblModel_1.Mode lName AS PendingModel, tblRequest.ETA, tblRequest.Pend ingCauses, tblRequestDetai l.Quantity*[stdQuantity] AS ReqQty, qry_Qtys.Outsta nding AS QtyOutstanding, tblRequestType. RequestType, tblInvoice.Invo iceDate, tblInvoice.Trac kingNumber

          FROM tblInvoice RIGHT JOIN (((qry_Qtys INNER JOIN (tblRequestType RIGHT JOIN (tblModel AS tblModel_1 INNER JOIN ((((tblCustomer INNER JOIN tblRequest ON tblCustomer.Cus tomerID = tblRequest.Cust omerID) INNER JOIN tblRequestDetai l ON tblRequest.Requ estID = tblRequestDetai l.RequestID) LEFT JOIN tblLocations ON tblRequest.Loca tionID = tblLocations.Lo cationID) INNER JOIN ((tblModel INNER JOIN tblProduct ON tblModel.ModelI D = tblProduct.Mode lID) INNER JOIN tblModelDetail ON tblModel.ModelI D = tblModelDetail. ModelID) ON tblRequestDetai l.ProductID = tblProduct.Prod uctID) ON tblModel_1.Mode lID = tblModelDetail. IncludesModelID ) ON tblRequestType. RequestTypeID = tblRequest.Requ estTypeID) ON qry_Qtys.Reques tID = tblRequest.Requ estID) INNER JOIN tblProduct AS tblProduct_1 ON (tblModel_1.Mod elID = tblProduct_1.Mo delID) AND (qry_Qtys.Produ ctID = tblProduct_1.Pr oductID)) LEFT JOIN tblInvoiceDetai l ON tblRequest.Requ estID = tblInvoiceDetai l.RequestID) ON tblInvoice.Invo iceID = tblInvoiceDetai l.InvoiceID

          WHERE (((tblRequest.R eqDate)<'2/4/2008') AND (([tblRequestDetai l].[Quantity]*[stdQuantity])>0) AND ((qry_Qtys.Outs tanding)>=0) AND ((tblInvoice.In voiceDate)>'2/4/2008' Or (tblInvoice.Inv oiceDate) Is Null) AND ((tblRequestTyp e.RequestTypeID )<>3) AND ((tblRequest.Cu stomerID)=1) AND (([tblRequestDetai l].[Cleared]+[tblRequest].[Canceled])=0))

          ORDER BY [Prefix] & tblModel_1.Mode lName, tblRequest.Cust omerTracking;
          [/code]

          The changed line:
          [code=sql]
          AND ((tblInvoice.In voiceDate)>'2/4/2008' Or (tblInvoice.Inv oiceDate) Is Null)
          [/code]

          Instead of recreating an entirely new OR statement, I followed the concept behind the algebraic approach and simply 'factored' out the other things in the WHERE clause.

          I now have all 5 queries running in under 1 second. Some as little as 19 milliseconds. A much needed improvement over the past running time. The query times used to be less when the database was only used by 1-2 people, but now that it's being used on a production server. It makes a huge difference.

          Thank you for the insight! I will still have to experiment with the separate queries for the joins, but am not sure if I will be able to yet. I may save that part for when I migrate over to MS SQL Server. (This week?? SCARY).

          Comment

          Working...