April 22, 2010 at 10:17 pm
Hi Friends,
I have a query as below
INSERT INTO DATABASE..STAG_TEMP_TABLE
SELECT ABC,SUM(ZXC)ZXC,SUM(XYZ)XYZ
FROM
(Select * from DATABASENAME..WWW (NOLOCK)
Union all
Select * from DATABASENAME..WWW1 (NOLOCK)) a
WHERE a.A1 BETWEEN '2009-10-09' AND '2010-04-08' AND a.A2 = 130 AND a.A3 > 10000
GROUP BY A2, ABC
This query is working as per the business, but is extremely slow(taking around 7-10Min). There are millions of records in the UNION All Tables.
Please let me know if there is any alternative approach for the above query.
Thanks
April 23, 2010 at 3:03 am
INSERT INTO DATABASE..STAG_TEMP_TABLE
SELECT ABC,SUM(ZXC)ZXC,SUM(XYZ)XYZ
FROM
(
-- Filter data here as the result will be passed up.
-- These two selects may also run in parallel.
SELECT *
FROM DATABASENAME..WWW
WHERE A1 BETWEEN '20091009' AND '20100408'
AND A2 = 130
AND A3 > 10000
UNION ALL
SELECT *
FROM DATABASENAME..WWW1
WHERE A1 BETWEEN '20091009' AND '20100408'
AND A2 = 130
AND A3 > 10000
) a
GROUP BY A2, ABC
April 23, 2010 at 7:12 am
What does the execution plan look like?
"The credit belongs to the man who is actually in the arena, whose face is marred by dust and sweat and blood"
- Theodore Roosevelt
Author of:
SQL Server Execution Plans
SQL Server Query Performance Tuning
April 23, 2010 at 7:42 am
Also post table and index definition
-------Bhuvnesh----------
I work only to learn Sql Server...though my company pays me for getting their stuff done;-)
April 23, 2010 at 11:09 am
Do both WWW and WWW1 have indexes on ABC, A1, A2 and A3? And updated statistics?
Depending on how the tables are being used, it may or may not be wise to have those indexes, but they would (probably) speed up the select queries (and slow down inserts). I say 'probably' because it would depend on cardinality and distribution of the data.
As Mr. Fritchey said, what do your execution plans look like? That would tell a lot.
Rob Schripsema
Propack, Inc.
April 23, 2010 at 6:47 pm
Also, is there a chance that WWW and WWW1 are actually views. Depending on the content of those views, that could really gum up the works, as well.
Last but not least, the use of SELECT * will pretty much guarantee a table scan or its near equivalent INDEX SCAN. Almost always limit your SELECTs to only those columns actually needed. Yes, there are some exceptions to that rule... but this isn't one of them. 😉
--Jeff Moden
Change is inevitable... Change for the better is not.
April 24, 2010 at 10:01 am
Jeff Moden (4/23/2010)
Also, is there a chance that WWW and WWW1 are actually views.
Why do you think that it could be view ? because of "Select * " ?
-------Bhuvnesh----------
I work only to learn Sql Server...though my company pays me for getting their stuff done;-)
April 24, 2010 at 12:14 pm
Try this one? In certain circumtances this can improve the performance..
INSERT INTO DATABASE..STAG_TEMP_TABLE
SELECT ABC,SUM(ZXC)ZXC,SUM(XYZ)XYZ
FROM
(
SELECT ABC,SUM(ZXC)ZXC,SUM(XYZ)XYZ
FROM DATABASENAME..WWW
WHERE A1 BETWEEN '20091009' AND '20100408'
AND A2 = 130
AND A3 > 10000
GROUP BY A2, ABC
UNION ALL
SELECT ABC,SUM(ZXC)ZXC,SUM(XYZ)XYZ
FROM DATABASENAME..WWW1
WHERE A1 BETWEEN '20091009' AND '20100408'
AND A2 = 130
AND A3 > 10000
GROUP BY A2, ABC
) a
GROUP BY A2, ABC
April 24, 2010 at 12:46 pm
Bhuvnesh (4/24/2010)
Jeff Moden (4/23/2010)
Also, is there a chance that WWW and WWW1 are actually views.Why do you think that it could be view ? because of "Select * " ?
No... I asked the question because many performance faults are caused by views, especially non-indexed aggregated views and most people simply forget about them. It's just one of those questions that needs to be asked in light of so little information by the OP.
--Jeff Moden
Change is inevitable... Change for the better is not.
April 24, 2010 at 11:17 pm
Jeff Moden (4/24/2010)
Bhuvnesh (4/24/2010)
Jeff Moden (4/23/2010)
Also, is there a chance that WWW and WWW1 are actually views.Why do you think that it could be view ? because of "Select * " ?
No... I asked the question because many performance faults are caused by views, especially non-indexed aggregated views and most people simply forget about them. It's just one of those questions that needs to be asked in light of so little information by the OP.
hmm got your point , can u suggest me any link where i can study about "view optimization stuff" , i know that behind the scene view are basically queries but still 😉
-------Bhuvnesh----------
I work only to learn Sql Server...though my company pays me for getting their stuff done;-)
Viewing 10 posts - 1 through 9 (of 9 total)
You must be logged in to reply to this topic. Login to reply