September 5, 2011 at 3:50 pm
I have a complex query that takes abouts 3-4 minuites running to get 80,xxx rows. That is not a big deal. The problem is that it take so long time to insert the result set to a destination table. There is no index on the destination table. Would the insert be faster if creating index on it?
The query is like
Insert into TABLE_NAME
select (complex query )
Please suggest me a solution. Thank you.
September 5, 2011 at 9:48 pm
I would suggest to get a good answer to your question you post the table definition, and the actual query text AND a copy of the execution plan ... For each of these click on both the links in my signature box to learn how to post the requested information quick and easily.
September 5, 2011 at 10:04 pm
Please identity the area where it can slow down
1) How long does the complex select takes to produce the result.
2) check the tables involved in the complex select.
3) Insert record to the target table 1 record to identity the time taken to insert.
These analysis will help to understand which part is taking time.
September 6, 2011 at 10:26 am
Hi everyone,
I have attached actual execution plans of main select query and insert query.
The Select query itself retrieves 80826 rows within 3 minutes.
For the Insert query execution plan, I try to insert data only 10 records. It took 24 minutes.
I'm not sure if that is enough info, pls let me know if you want any info.
Thank you
September 6, 2011 at 1:15 pm
Please attach the actual query since the execution plan does not show the full query.
My pot shot would be this task would benefit from a divide'n'conquer approach (based on the complexity of the execution plan and the re-usage of several large tables).
September 6, 2011 at 2:15 pm
i see lots of issues; many , many of your tables are HEAPs, meaning without a primary key and a nice clustered index to access them, they will slow things down.
your destination table, [Repl_AxWalton].[ODS].[UDI_ Production_Pam] is a heap, as well as
[WFWaitlist],
[Phase Region]
[WFCLIENTFILESALESORDER]
[WFCLIENTFILE]
[All Land Cos]
[_PROC]
[_ActInst]
[_Act]
[ADDRESS]
and a few more. adding primary keys and clustered indexes will go a long way to fixing performance issues, as well as space recovery considerations.
one of the expensive items was the RID lookup on [Repl_AXWalton].[WF].[WFCategory], .
looks like the LandUnit's expression for getting the value was very expensive as well:
CASE
WHEN LEFT(wfia.LandUnit,6) = 'KC1160'
THEN 'KC1160' + '-' + '0' + SUBSTRING(wfia.LandUnit, 8, 3)
ELSE wfia.LandUnit END LandUnit,
i'd start first with eliminating heaps with tables with PKs, clustered indexes,a dn then start working on adding indexes, i think.
hope this helps!
Lowell
September 6, 2011 at 2:19 pm
also the index on [Repl_AxWalton].[WF].[WFInventoryAllocation]: the statsitics on it made it scan a lot more rows: it was estimated @ 215,560 but it actually scanned 431,012,220 rows.
i'd rebuild the index or update statsutics with fullscan on that table, too.
Lowell
September 7, 2011 at 11:34 am
did you make any more progress on this?
I'd love to hear if you added clustered indexes and if they made a difference.
Lowell
September 7, 2011 at 11:38 am
Hi Lovell,
I'm doing what you have recommended. Anyway, I'm not sure what is wrong today with the server. The main query is even worse. Anything going on, I'll let you know.
September 7, 2011 at 6:11 pm
As Lowell pointed out, you have an accidental cross join (think "many-to-many" join and look for the arrows with counts larger than the table or index they're coming from) in the stand alone SELECT and, because of the INSERT, an ever worse one shows up on the SELECT for the insert.
A lot of people make the mistake of joining too many tables. Split this query up by using a Temp Table or two... isolate the minimum rows that you can in the Temp Tables. You can very likely get this query down to something that runs in less than 3 seconds. It's just going to take a little time, effort, and research on your part to fix it.
--Jeff Moden
Change is inevitable... Change for the better is not.
September 8, 2011 at 8:32 am
Hello Jeff,
I am changing the query as you mention. The performance is getting better. Now I got the Arithmatic overflow error but I think I could handle it.
Thank you for you guys sharing knowledge with me. 🙂
Pam
Viewing 11 posts - 1 through 10 (of 10 total)
You must be logged in to reply to this topic. Login to reply