Re: Slow Query Issue In SP

Tech-Archive recommends: Fix windows errors by optimizing your registry

From: Hugo Kornelis (hugo_at_pe_NO_rFact.in_SPAM_fo)
Date: 02/07/05


Date: Tue, 08 Feb 2005 00:47:37 +0100

On 7 Feb 2005 14:00:41 -0800, Paul Moore wrote:

>I have an issue that I keep banging my head on. Basic business problem
>is that I need to return all records in a given time period and any
>records which have not been completed. There are several different
>queries inside of the SP which all have the same basic problem with the
>or statement.
>
>
>Select t1.col1, t3.col2, count(*), sum(col3)
>>>From table1 t1 inner join table2 t2 on (t1.pkey = t2.pkey)
> inner join table3 t3 on (t2.col1 = t3.col1 and t2.col2 = t3.col2)
>where (t1.date <= @datevar or completed = 0)
>group by t1.col1, t3.col2
>
>That check for the completed across the table takes about 3 seconds,
>compared to a couple ms without it. I have tried breaking this out
>into different queries and indexed the heck out of the table all to no
>avail.
>
>Because of the redundant way this has to be called the SP is running
>almost a minute and growing as the table grows. Any ideas?

Hi Paul,

In what table live the completed column?

Assuming it's in table1, see if this performs better:

SELECT t1.col1, t3.col2, COUNT(*), SUM(col3)
FROM (SELECT pkey, col1
           FROM table1
           WHERE date <= @datevar
           UNION
           SELECT pkey, col1
           FROM table1
           WHERE completed = 0) AS t1
INNER JOIN table2 AS t2
      ON t2.pkey = t1.pkey
INNER JOIN table3 AS t3
      ON t3.col1 = t2.col1
      AND t3.col2 = t2.col2
GROUP BY t1.col1, t3.col2

You could also replace the derived table in the above query with one of
the following alternatives:

          (SELECT pkey, col1
           FROM table1
           WHERE date <= @datevar
           AND completed <> 0 -- Assumes completed can't be NULL
           UNION ALL
           SELECT pkey, col1
           FROM table1
           WHERE completed = 0) AS t1
or
          (SELECT pkey, col1
           FROM table1
           WHERE date <= @datevar
           UNION ALL
           SELECT pkey, col1
           FROM table1
           WHERE date > @datevar -- Assumes date can't be NULL
           AND completed = 0) AS t1

Best, Hugo

-- 
(Remove _NO_ and _SPAM_ to get my e-mail address)


Relevant Pages

  • Re: Weekly crosstab query
    ... queries for the other information and then linking the queries together by ... ensure that the queries return data for the same set of employees. ... Query Two uses query one in place of the OvertimeTracking table ... INNER JOIN qPrior ON ...
    (microsoft.public.access.queries)
  • RE: Aggregates, Joins, I am totally stuck on this issue
    ... a left outer join it makes it an inner join? ... I have two aggregate queries which are grouped on ... >> query and then create an append query which puts the results of this master ... >> a LEFT OUTER JOIN and run it and what do it get? ...
    (microsoft.public.access.queries)
  • Re: Create query from list in a table
    ... add a comboboxfor the selection of the Team. ... Steve Clark, Access MVP ... There are 15 sub queries that individully produce the information that I ... I also have one query that uses INNER JOIN to put all that data into ...
    (microsoft.public.access.queries)
  • Re: Union query and exclusion of data
    ... -- Records in A or in B but not in both (Last two queries above) ... Access MVP 2002-2005, 2007-2008 ... I am usin Access 2003 Pro and trying to create a rather complex union query but don't seem to implement conditions. ... I have tried all INNER JOIN, LEFT JOIN and RIGHT JOIN but none of them have worked so far. ...
    (microsoft.public.access.queries)
  • Re: JOIN on multiple conditions
    ... You must use the OUTER JOIN syntax, of course, ... >but if you learn only the INNER JOIN syntax does it change the way you ... More complicated queries should not be written by ... condition is placed in the ON or in the WHERE clause. ...
    (microsoft.public.sqlserver.programming)