Showing posts with label slow. Show all posts
Showing posts with label slow. Show all posts

Thursday, March 8, 2012

Complex SQL query

Hi,

I'm doing a report with a group of queries but right now is very slow, so I need to do it faster. These are not the real tables but will help:

The report needs to show the total products for every combination of ADDRESS and PRODUCT_TYPE. Assume these are the tables:

ADDRESS: ADDRESS_ID, ADDRESS_NAME
PRODUCT_TYPE: PRODUCT_TYPE_CODE, PRODUCT_TYPE_NAME
ORDER: ORDER_ID, DATE, ORDER_STATUS
ORDER_LINE: ORDER_ID, ORDER_PRODUCT_TYPE, PRODUCT_TOTAL
(This is an special table to handle the stock)
STOCK_INFO: STOCK_ACTUAL, DATE_UPDATED

This is what I'm doing in code (asp):

1. Retrieve all the address (and put it in array)
2. Retrieve all the product types (and put it in array)
3. Using double "FOR" I build the query for every combination of Address and ProductType

This is still slow (and is even better than before) and I would like to put everything in just 1 query and get this data ready to show in HTML

Address Product Type 1 Product Type2 Product Type3
Address1 TotProdType11 TotProdType21 TotProdType31
Address2 TotProdType12 TotProdType22 TotProdType32
.....

I'll really appreciate any help. And also any better idea to do these is welcome (is just I don't have to much knowledge in very complex queries)

Thanks in advance

Moving to Transact-SQL forum...|||I don't see any relationship between Address table and the Product_Type at all. How is it related ?|||

use a CROSS JOIN in SQL server if you want a combination of all products and addresses.

eg

select a.address, Address p.Product Type from address a cross join product_type p

Note that if you are wanting a cartesian product here, you should specify no join criteria, as you want every address and product combination. That should be much quicker than doing it in client side code. However, you will need some kind of join to get the totals for each product, as a cross joins blindly combines all rows from 1 table to all the rows from another. I need further clarification here.

You will then have to turn the results into a pivot table. In SQL 2005, use the PIVOT function, in SQL 2003 and earlier, you will need to use a case statement:

SELECT a.address,

CASE

WHEN p.Product_type = 'Product A' -- whatever first product type is

THEN ..... -- your code, I think from your example you want a sum() here

WHEN p.Product_type = 'Product A' --

THEN

etc

END

from.......

Hope that helps

from address a cross join product_type p

GROUP BY a.address

Saturday, February 25, 2012

Complex queries run slow after SP4 installed

My company's production SQL Server 2000 becomes very slow after SP4 is
installed.
We have figured out that several complex queries which only took 2 seconds
to complete under SP3a now take more than 10 minutes to finish in SP4! The
sympton is similar to the following KB:
http://support.microsoft.com/kb/826906/#XSLTH3120121122120121120120
Has anyone experienced similar problems?
Where can I get the hotfix?
regards
ArthurArthur,
I had the same problem. Product support recommended I install build
2145, but that really didn't help in my case. I wound up re-writing my
queries to not use views -- if you can, I recommend you do the same. Queries
on the base tables are still (mostly) fast.
Regards,
Jonathan
"Arthur" wrote:
> My company's production SQL Server 2000 becomes very slow after SP4 is
> installed.
> We have figured out that several complex queries which only took 2 seconds
> to complete under SP3a now take more than 10 minutes to finish in SP4! The
> sympton is similar to the following KB:
> http://support.microsoft.com/kb/826906/#XSLTH3120121122120121120120
> Has anyone experienced similar problems?
> Where can I get the hotfix?
> regards
> Arthur
>
>|||Dear Jonathan
Thanks for your response.
The bad thing is that over 90% of our stored procedures or queries are based
on views and thus rewritting and testing all of them is just impossible.
Do you think I can fall back to SP3 safely (suppose I have the database
backup before the upgrade)?
regards
Arthur
"Jonathan Levine" <myfoo2@.nospam.nospam> wrote in message
news:C2490F12-DBF9-4854-9F78-06FE833FD131@.microsoft.com...
> Arthur,
> I had the same problem. Product support recommended I install build
> 2145, but that really didn't help in my case. I wound up re-writing my
> queries to not use views -- if you can, I recommend you do the same.
Queries
> on the base tables are still (mostly) fast.
> Regards,
> Jonathan
> "Arthur" wrote:
> > My company's production SQL Server 2000 becomes very slow after SP4 is
> > installed.
> >
> > We have figured out that several complex queries which only took 2
seconds
> > to complete under SP3a now take more than 10 minutes to finish in SP4!
The
> > sympton is similar to the following KB:
> >
> > http://support.microsoft.com/kb/826906/#XSLTH3120121122120121120120
> >
> > Has anyone experienced similar problems?
> >
> > Where can I get the hotfix?
> >
> > regards
> >
> > Arthur
> >
> >
> >|||Arthur,
"Arthur" wrote:
> Do you think I can fall back to SP3 safely (suppose I have the database
> backup before the upgrade)?
If you can roll back, I would do so ASAP. The problem just gets worse
and worse as the queries get longer (I had queries that took 3 or 4 minutes
in SP3a that took several hours in SP4).
-- J|||This may or may not apply to you but we experienced some problems recently
where queries that run fast on my computer were taking forever on the
client's site.
We discovered that SQL Server was reporting an error about parallel queries.
We weren't doing any but we configured the server to only use 1 CPU instead
of "all available processors" and the problem went away.
Good luck
Richard Speiss
"Arthur" <arthurw@.newgroup.nospam> wrote in message
news:u9pGzO9lFHA.2156@.TK2MSFTNGP14.phx.gbl...
> My company's production SQL Server 2000 becomes very slow after SP4 is
> installed.
> We have figured out that several complex queries which only took 2 seconds
> to complete under SP3a now take more than 10 minutes to finish in SP4! The
> sympton is similar to the following KB:
> http://support.microsoft.com/kb/826906/#XSLTH3120121122120121120120
> Has anyone experienced similar problems?
> Where can I get the hotfix?
> regards
> Arthur
>

Complex queries run slow after SP4 installed

My company's production SQL Server 2000 becomes very slow after SP4 is
installed.
We have figured out that several complex queries which only took 2 seconds
to complete under SP3a now take more than 10 minutes to finish in SP4! The
sympton is similar to the following KB:
http://support.microsoft.com/kb/8269...22120121120120
Has anyone experienced similar problems?
Where can I get the hotfix?
regards
Arthur
Arthur,
I had the same problem. Product support recommended I install build
2145, but that really didn't help in my case. I wound up re-writing my
queries to not use views -- if you can, I recommend you do the same. Queries
on the base tables are still (mostly) fast.
Regards,
Jonathan
"Arthur" wrote:

> My company's production SQL Server 2000 becomes very slow after SP4 is
> installed.
> We have figured out that several complex queries which only took 2 seconds
> to complete under SP3a now take more than 10 minutes to finish in SP4! The
> sympton is similar to the following KB:
> http://support.microsoft.com/kb/8269...22120121120120
> Has anyone experienced similar problems?
> Where can I get the hotfix?
> regards
> Arthur
>
>
|||Dear Jonathan
Thanks for your response.
The bad thing is that over 90% of our stored procedures or queries are based
on views and thus rewritting and testing all of them is just impossible.
Do you think I can fall back to SP3 safely (suppose I have the database
backup before the upgrade)?
regards
Arthur
"Jonathan Levine" <myfoo2@.nospam.nospam> wrote in message
news:C2490F12-DBF9-4854-9F78-06FE833FD131@.microsoft.com...
> Arthur,
> I had the same problem. Product support recommended I install build
> 2145, but that really didn't help in my case. I wound up re-writing my
> queries to not use views -- if you can, I recommend you do the same.
Queries[vbcol=seagreen]
> on the base tables are still (mostly) fast.
> Regards,
> Jonathan
> "Arthur" wrote:
seconds[vbcol=seagreen]
The[vbcol=seagreen]
|||Arthur,
"Arthur" wrote:
> Do you think I can fall back to SP3 safely (suppose I have the database
> backup before the upgrade)?
If you can roll back, I would do so ASAP. The problem just gets worse
and worse as the queries get longer (I had queries that took 3 or 4 minutes
in SP3a that took several hours in SP4).
-- J
|||This may or may not apply to you but we experienced some problems recently
where queries that run fast on my computer were taking forever on the
client's site.
We discovered that SQL Server was reporting an error about parallel queries.
We weren't doing any but we configured the server to only use 1 CPU instead
of "all available processors" and the problem went away.
Good luck
Richard Speiss
"Arthur" <arthurw@.newgroup.nospam> wrote in message
news:u9pGzO9lFHA.2156@.TK2MSFTNGP14.phx.gbl...
> My company's production SQL Server 2000 becomes very slow after SP4 is
> installed.
> We have figured out that several complex queries which only took 2 seconds
> to complete under SP3a now take more than 10 minutes to finish in SP4! The
> sympton is similar to the following KB:
> http://support.microsoft.com/kb/8269...22120121120120
> Has anyone experienced similar problems?
> Where can I get the hotfix?
> regards
> Arthur
>

Complex queries run slow after SP4 installed

My company's production SQL Server 2000 becomes very slow after SP4 is
installed.
We have figured out that several complex queries which only took 2 seconds
to complete under SP3a now take more than 10 minutes to finish in SP4! The
sympton is similar to the following KB:
http://support.microsoft.com/kb/826...122120121120120
Has anyone experienced similar problems?
Where can I get the hotfix?
regards
ArthurArthur,
I had the same problem. Product support recommended I install build
2145, but that really didn't help in my case. I wound up re-writing my
queries to not use views -- if you can, I recommend you do the same. Querie
s
on the base tables are still (mostly) fast.
Regards,
Jonathan
"Arthur" wrote:

> My company's production SQL Server 2000 becomes very slow after SP4 is
> installed.
> We have figured out that several complex queries which only took 2 seconds
> to complete under SP3a now take more than 10 minutes to finish in SP4! The
> sympton is similar to the following KB:
> http://support.microsoft.com/kb/826...122120121120120
> Has anyone experienced similar problems?
> Where can I get the hotfix?
> regards
> Arthur
>
>|||Dear Jonathan
Thanks for your response.
The bad thing is that over 90% of our stored procedures or queries are based
on views and thus rewritting and testing all of them is just impossible.
Do you think I can fall back to SP3 safely (suppose I have the database
backup before the upgrade)?
regards
Arthur
"Jonathan Levine" <myfoo2@.nospam.nospam> wrote in message
news:C2490F12-DBF9-4854-9F78-06FE833FD131@.microsoft.com...
> Arthur,
> I had the same problem. Product support recommended I install build
> 2145, but that really didn't help in my case. I wound up re-writing my
> queries to not use views -- if you can, I recommend you do the same.
Queries[vbcol=seagreen]
> on the base tables are still (mostly) fast.
> Regards,
> Jonathan
> "Arthur" wrote:
>
seconds[vbcol=seagreen]
The[vbcol=seagreen]|||Arthur,
"Arthur" wrote:
> Do you think I can fall back to SP3 safely (suppose I have the database
> backup before the upgrade)?
If you can roll back, I would do so ASAP. The problem just gets worse
and worse as the queries get longer (I had queries that took 3 or 4 minutes
in SP3a that took several hours in SP4).
-- J|||This may or may not apply to you but we experienced some problems recently
where queries that run fast on my computer were taking forever on the
client's site.
We discovered that SQL Server was reporting an error about parallel queries.
We weren't doing any but we configured the server to only use 1 CPU instead
of "all available processors" and the problem went away.
Good luck
Richard Speiss
"Arthur" <arthurw@.newgroup.nospam> wrote in message
news:u9pGzO9lFHA.2156@.TK2MSFTNGP14.phx.gbl...
> My company's production SQL Server 2000 becomes very slow after SP4 is
> installed.
> We have figured out that several complex queries which only took 2 seconds
> to complete under SP3a now take more than 10 minutes to finish in SP4! The
> sympton is similar to the following KB:
> http://support.microsoft.com/kb/826...122120121120120
> Has anyone experienced similar problems?
> Where can I get the hotfix?
> regards
> Arthur
>