473,396 Members | 1,815 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Tempdb gets out of control when inserting records

AG
I have a very big table with 20 million records DistinctProjectionKey
which i join several times to different tables in this query.
select distinct distinctprojectionkeyid,dpk.MarketID,
dpk.Classificationid,
dpk.DistributorID,
dpk.ManufacturerID,
dpk.LocationID,
dpk.TimeID,P4.FACTOR as factor1,P3.FACTOR as factor2 ,P2.FACTOR as
factor3,P1.FACTOR as factor4
into Projectionfactors1
FROM DistinctProjectionKey dpk INNER JOIN D_Time t
ON t.TimeID = dpk.TimeID

INNER JOIN (select * from (select distinct
ClassificationID_Major,'fam' as lab from
StagingOLTP..ClassificationFlat) cf1)cf
ON cf.ClassificationID_Major = dpk.ClassificationID
LEFT OUTER JOIN StagingOLTP..ProjectionDefaultFlat p4
ON t.TheDate = p4.TheDate
AND p4.Name = 'FAM'
AND cast(cf.Lab as varchar(20)) = cast(p4.Lab as varchar(20))
AND dpk.MarketID = p4.MarketID
AND p4.ManufacturerID IS NULL
AND p4.ClassificationID IS NULL

LEFT OUTER JOIN StagingOLTP..ProjectionDefaultFlat p3
ON t.TheDate = p3.TheDate
AND p3.Name = 'fam'
AND cast(cf.Lab as varchar(20)) = cast(p3.Lab as varchar(20))
AND dpk.MarketID = p3.MarketID
AND p3.ClassificationID = dpk.ClassificationID
AND p3.ManufacturerID IS NULL

LEFT OUTER JOIN StagingOLTP..ProjectionDefaultFlat p2
ON t.TheDate = p2.TheDate
AND p2.Name = 'fam'
AND cast(cf.Lab as varchar(20)) = cast(p2.Lab as varchar(20))
AND dpk.MarketID = p2.MarketID
AND p2.ManufacturerID = dpk.ManufacturerID
AND p2.ClassificationID IS NULL

LEFT OUTER JOIN StagingOLTP..ProjectionDefaultFlat p1
ON t.TheDate = p1.TheDate
AND p1.Name = 'fam'
AND cast(cf.Lab as varchar(20)) = cast(p1.Lab as varchar(20))
AND dpk.MarketID = p1.MarketID
AND p1.ManufacturerID = dpk.ManufacturerID
AND p1.ClassificationID = dpk.ClassificationID
the other table have fewer number of records .

I find that when I try to do the insert tempdb goes out of control ,it
grows above 100 GB?
Would anyone know the reason why and the solution to apply to avoid
this problem?
The other tables have fewer recods
Classification flat has 5000 records and projection default flat has
32652 records.
Ajay
Ajay

Jul 23 '05 #1
2 1965
Hi

To execute the query, the SQL Engine needs to take data out of the original
tables and store it in a work table. If the work table exceeds the amount of
RAM you have, it has to spool it to disk. TempDB is the spool location. (If
you switch on Display Execution Plan in QA, you will notice many spool
operations).

The use of code like "INNER JOIN (select * from (select distinct" results in
having to do this as it first has to find distinct rows, store it, then do
the join.

Re-writing the query will help and monitor the operations in the execution
plans until you can get rid of the spool operations.

Regards
--------------------------------
Mike Epprecht, Microsoft SQL Server MVP
Zurich, Switzerland

IM: mi**@epprecht.net

MVP Program: http://www.microsoft.com/mvp

Blog: http://www.msmvps.com/epprecht/

"AG" <aj*****@hotmail.com> wrote in message
news:11**********************@l41g2000cwc.googlegr oups.com...
I have a very big table with 20 million records DistinctProjectionKey
which i join several times to different tables in this query.
select distinct distinctprojectionkeyid,dpk.MarketID,
dpk.Classificationid,
dpk.DistributorID,
dpk.ManufacturerID,
dpk.LocationID,
dpk.TimeID,P4.FACTOR as factor1,P3.FACTOR as factor2 ,P2.FACTOR as
factor3,P1.FACTOR as factor4
into Projectionfactors1
FROM DistinctProjectionKey dpk INNER JOIN D_Time t
ON t.TimeID = dpk.TimeID

INNER JOIN (select * from (select distinct
ClassificationID_Major,'fam' as lab from
StagingOLTP..ClassificationFlat) cf1)cf
ON cf.ClassificationID_Major = dpk.ClassificationID
LEFT OUTER JOIN StagingOLTP..ProjectionDefaultFlat p4
ON t.TheDate = p4.TheDate
AND p4.Name = 'FAM'
AND cast(cf.Lab as varchar(20)) = cast(p4.Lab as varchar(20))
AND dpk.MarketID = p4.MarketID
AND p4.ManufacturerID IS NULL
AND p4.ClassificationID IS NULL

LEFT OUTER JOIN StagingOLTP..ProjectionDefaultFlat p3
ON t.TheDate = p3.TheDate
AND p3.Name = 'fam'
AND cast(cf.Lab as varchar(20)) = cast(p3.Lab as varchar(20))
AND dpk.MarketID = p3.MarketID
AND p3.ClassificationID = dpk.ClassificationID
AND p3.ManufacturerID IS NULL

LEFT OUTER JOIN StagingOLTP..ProjectionDefaultFlat p2
ON t.TheDate = p2.TheDate
AND p2.Name = 'fam'
AND cast(cf.Lab as varchar(20)) = cast(p2.Lab as varchar(20))
AND dpk.MarketID = p2.MarketID
AND p2.ManufacturerID = dpk.ManufacturerID
AND p2.ClassificationID IS NULL

LEFT OUTER JOIN StagingOLTP..ProjectionDefaultFlat p1
ON t.TheDate = p1.TheDate
AND p1.Name = 'fam'
AND cast(cf.Lab as varchar(20)) = cast(p1.Lab as varchar(20))
AND dpk.MarketID = p1.MarketID
AND p1.ManufacturerID = dpk.ManufacturerID
AND p1.ClassificationID = dpk.ClassificationID
the other table have fewer number of records .

I find that when I try to do the insert tempdb goes out of control ,it
grows above 100 GB?
Would anyone know the reason why and the solution to apply to avoid
this problem?
The other tables have fewer recods
Classification flat has 5000 records and projection default flat has
32652 records.
Ajay
Ajay

Jul 23 '05 #2
AG (aj*****@hotmail.com) writes:
I have a very big table with 20 million records DistinctProjectionKey
which i join several times to different tables in this query.
...
I find that when I try to do the insert tempdb goes out of control ,it
grows above 100 GB?
Would anyone know the reason why and the solution to apply to avoid
this problem?
The other tables have fewer recods
Classification flat has 5000 records and projection default flat has
32652 records.


I have a suspicion that this query does not do what you intend. How many
rows in the ProjectionDefaultFlat can each LEFT JOIN hit? If it's more
than one row, you also have the answer for the tempdb explosion. Since
you join with the table four times, you get a four-way cross join (i.e
a cartestian product) between the various projections of the staging table.
That can be many rows! And it may be more rows than you want. You will
get a combination of factor1-4 that make little sense.

Probably you should write the query something like this:

SELECT ...
FROM DistinctProjectionKey
JOIN D_Time t
JOIN (SELECT ...) cf
LEFT JOIN Staging...
AND p4.ManufacturerID IS NULL
AND p4.ClassificationID IS NULL
UNION
SELECT ...
FROM DistinctProjectionKey
JOIN D_Time t
JOIN (SELECT ...) cf
LEFT JOIN Staging...
AND p3.ClassificationID = dpk.ClassificationID
AND p3.ManufacturerID IS NULL

Or you could just have:

AND dpk.MarketID = p4.MarketID
AND (p4.ManufacturerID IS NULL OR p4.ManufacturerID = dpk.ManufacturerID)
AND (p4.ClassificationID IS NULL ...)

There is also this part:

INNER JOIN (select *
from (select distinct ClassificationID_Major,'fam' as lab
from StagingOLTP..ClassificationFlat) cf1) cf

This looks like a bit of an overkill. It may be better to replace this
with an EXISTS:

WHERE EXISTS (SELECT *
FROM StagingOLTP..ClassificationFlat cf1
WHERE cf1.ClassificationID = dpk.ClassificationID)

In the LEFT JOIN there is a reference to cf:

AND cast(cf.Lab as varchar(20)) = cast(p4.Lab as varchar(20))

But since cf.lab is a constant, this can be rewritten.

Disclaimer: I no nothing of your business process, nothing about your
tables and nothing about your indexes. See the above as general guesses
that may or may not help to improve the processing. If there is something
you think does not make sense, feel free to discard. For more precise
help, it can be a good idea to post CREATE TABLE and CREATE INDEX statements
of your tables, and don't forget PRIMARY and UNIQUE constraints.

--
Erland Sommarskog, SQL Server MVP, es****@sommarskog.se

Books Online for SQL Server SP3 at
http://www.microsoft.com/sql/techinf...2000/books.asp
Jul 23 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

10
by: Jay Chan | last post by:
I keep getting the following error message when I run a serie of SQL commands: Server: Msg 9002, Level 17, State 6, Line 15 The log file for database 'tempdb' is full. Back up the transaction...
3
by: Sean Lambert | last post by:
We had someone create an extra data file and log file for tempdb. So we currently have two data files and two log files. Is it possible to delete the newly created data and log files? If I just...
2
by: Warren Wright | last post by:
Hi All, First, where can I get some questions of this sort answered? Preferably, are there good books or online guides that I can consult for these types of answers when necessary? 1. How do...
2
by: John R. Dougherty | last post by:
Using VB.NET: I am populating a DataTable from a SQL Server table, then associating that DataTable to a DataGrid control. I want to disallow the user from inserting new records on that DataGrid,...
8
by: arijitchatterjee123 | last post by:
Hi Group, I am facing a problem regarding locking. I have created a Stored Procedure in my Database. In this Stored Procedure Temprary Tables get created and after that values are inserted in...
1
by: Elham Ghoddousi | last post by:
I have a problem with my TEMPDB Database.It gets extra size through importing data into one of my databases in same server. How can I shrink my TempDB Databse? Thanks
2
by: Dee | last post by:
I have a form which contains basics of a customer's order from table . I placed a tab control on this form which is still empty. I'm trying to design a project management database and would like...
3
by: Karthik | last post by:
I have an application that creates and makes use of a table in tempdb. My problem is that I have a couple of triggers on this table that get dropped everytime SQL Server restarts (due to the fact...
3
by: Kurt | last post by:
Hello I have questions about how works transaction log et the database tempdb in SQL Server and I hop you could help me - Is it possible to reduce the size of the transaction log fil during an...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.