Results 1 to 8 of 8

Thread: SubQuery performance degrades using SQL Server as the backend

  1. #1
    EuniceH is offline Novice
    Windows Vista Access 2000
    Join Date
    Oct 2011
    Posts
    9

    Unhappy SubQuery performance degrades using SQL Server as the backend

    This query is slow using MS Access 2000 as a backend, but it gets slower (4 hours or more) using SQL Server 2008 Express as the backend. Is there any way you can see to improving the performance?
    The indexes for tblARTransactions are:
    composite Primary Key (ARTransDate, ContractID, ARTransTypeID, Sequence)
    ARTranactionID
    ARTransDate & TimeStamp
    ContractID
    ContractVehicleID
    DeleteDTS
    EntryDate

    and here's the query
    SELECT a2.*
    FROM tblARTransactions AS a2
    WHERE (( (a2.ARTransDate) > DateAdd("d",-5,Date())) AND ((a2.ContractVehicleID) IN
    (SELECT a1.ContractVehicleID
    FROM tblARTransactions AS a1
    WHERE a2.ARTransDate >= DateAdd("d",-1,a1.ARTransDate) AND a2.ARTransDate <= DateAdd("d",1,a1.ARTransDate)
    AND a2.ARTransTypeID = a1.ARTransTypeID
    AND a2.ARTransactionID <> a1.ARTransactionID
    )))
    ORDER BY a2.ContractID, a2.ARTransDate DESC;

  2. #2
    orange is offline VIP
    Windows XP Access 2003
    Join Date
    Sep 2009
    Location
    Ottawa, Ontario, Canada
    Posts
    4,351
    How many records in the table?
    You are doing a lot of Date manipulation in the query.

    In plain English what is the selection criteria? Maybe someone will have another method of achieving the query.
    Last edited by orange; 04-04-2012 at 03:10 PM.
    Not everyone can access an accdb, you may reach a broader audience by attaching in mdb format.

  3. #3
    EuniceH is offline Novice
    Windows Vista Access 2000
    Join Date
    Oct 2011
    Posts
    9
    Quote Originally Posted by orange View Post
    How many records in the table?
    870,000 records

  4. #4
    orange is offline VIP
    Windows XP Access 2003
    Join Date
    Sep 2009
    Location
    Ottawa, Ontario, Canada
    Posts
    4,351
    In plain English what is the selection criteria? Maybe someone will have another method of achieving the query.
    Not everyone can access an accdb, you may reach a broader audience by attaching in mdb format.

  5. #5
    Eunice.harris is offline Novice
    Windows 7 32bit Access 2000
    Join Date
    Mar 2012
    Posts
    5
    in english, is there another record in tblARTransactions matching this one that
    -is in the last 5 days
    -has the same ContractVehicleID
    -is within 1 day of the other record
    -has the same ARTransTypeID

  6. #6
    orange is offline VIP
    Windows XP Access 2003
    Join Date
    Sep 2009
    Location
    Ottawa, Ontario, Canada
    Posts
    4,351
    You could try these for comparison -- but you know your data better that I do.

    1-
    SELECT A2.*
    FROM tblArTransactions AS A2 INNER JOIN tblArTransactions AS A1 ON A2.ARTransTypeId = A1.ARTransTypeId
    WHERE (((A2.ARTransdate) Between DateAdd("d",-1,[a1].[ARTransDate]) And
    DateAdd("d",1,[a1].[artransdate])) AND
    ((A2.ArTransactionId)<>[a1].[artransactionid]) AND
    ((A2.ARTransdate) Between DateAdd("d",-5,Date()) And Date()) AND
    ((A2.ContractVehicleId)=[a1].[contractvehicleid]));
    2-
    SELECT A2.*
    FROM tblArTransactions AS A2
    WHERE ((A2.ARTransdate) Between DateAdd("d",-5,Date()) And Date()) AND Exists
    ( Select "x" From tblArTransactions AS A1
    Where A2.ARTransTypeId = A1.ARTransTypeId and
    ((A2.ARTransdate) Between DateAdd("d",-1,[a1].[ARTransDate]) And DateAdd("d",1,[a1].[artransdate])) AND
    ((A2.ArTransactionId)<>[a1].[artransactionid]) AND
    ((A2.ContractVehicleId)=[a1].[contractvehicleid])
    );
    Good luck.
    Not everyone can access an accdb, you may reach a broader audience by attaching in mdb format.

  7. #7
    EuniceH is offline Novice
    Windows Vista Access 2000
    Join Date
    Oct 2011
    Posts
    9

    Red face works!

    your 1st query works so well, only takes about 5 seconds to run. Thanks, I assume that putting the "((A2.ARTransdate) Between DateAdd("d",-5,Date()) And Date())" in the inside query helps the performance? I intuitively but mistakenly thought it should be in the outside query.
    Thanks so much!
    EuniceH

  8. #8
    orange is offline VIP
    Windows XP Access 2003
    Join Date
    Sep 2009
    Location
    Ottawa, Ontario, Canada
    Posts
    4,351
    Glad it worked, but I would expect the second query with the Exists to be faster.

    As for the selection, in the first query I didn't use a subquery. The query uses an Inner Join based on ArTransTypeId and resolves the other criteria in the Where clause.

    I just looked at it again and think this could be an improvement (Inner Join on 2 fields)

    SELECT A2.*
    FROM tblArTransactions AS A2 INNER JOIN tblArTransactions AS A1 ON
    (A2.ContractVehicleId = A1.ContractVehicleId) AND
    (A2.ARTransTypeId = A1.ARTransTypeId)
    WHERE
    (((A2.ARTransdate) Between DateAdd("d",-1,[a1].[ARTransDate]) And DateAdd("d",1,[a1].[artransdate]) And (A2.ARTransdate) Between DateAdd("d",-5,Date()) And Date()) AND ((A2.ArTransactionId)<>[a1].[artransactionid]));

    My gut feeling is that the EXISTS query (#2) should be faster... but you can try it and let us know.

    Good luck with your project.
    Not everyone can access an accdb, you may reach a broader audience by attaching in mdb format.

Similar Threads

  1. Replies: 2
    Last Post: 03-24-2012, 12:29 AM
  2. Replies: 5
    Last Post: 11-17-2011, 01:04 PM
  3. Replies: 1
    Last Post: 09-13-2011, 12:52 PM
  4. Replies: 2
    Last Post: 03-21-2011, 11:55 AM
  5. connection performance
    By DanM in forum Programming
    Replies: 0
    Last Post: 08-24-2008, 07:25 AM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Tech Forums: Microsoft Office Forums