Hi
i have one database table Table1.which contains almost 20000000 recordes.
record to this table are inserted through storedprocedure. storedprocedure takes parameter as
"value",
Beginningdate,
Endate .
which will insert one record for each day between Beginning date and EndDate.
Before inserting record i check is record exsist for date,if exsist i will update value otherwise insert new record.
so in there is 3000 days diff between Beginning date And EndDate its taking so much time so plz help me.
Here is code of sp
BEGIN
WHILE @BeginningDate <= @EndDate
BEGIN --- Start While LOOP
IF NOT EXISTS
(
SELECT * FROM Table1 WITH (NOLOCK)
WHERE BeginningDate = @BeginningDate
)
BEGIN --- Start Insert Sql Querry
INSERT INTO Table1
(
Val,
BeginningDate,
EndDate
)
Values
(
@Val,
@BeginningDate,
@BeginningDate
)
END --- Endof Insert Sql Querry
ELSE
BEGIN --- Start Update Sql Querry
UPDATE Table1 WITH (ROWLOCK)
SET
Val=@Val
WHERE
BeginningDate = @BeginningDate
END --- Endof Update Sql Querry
SET @BeginningDate = @BeginningDate + 1;
END --- Endof While LOOP
END
2 1784
Hi,
what i think is instead of looping,
1. take all the dates between beginning date and end date into temporary table.
2. Update the Table1 with value where the dates in Table1 and Temporary table
matches.
3. Delete the common dates from Temporary table.( Now you have new dates in
the temporary table)
4. Insert all the new records into Table1.
and the code is like: -
-
-
DECLARE @Table TABLE
-
( ID INT IDENTITY(1,1),
-
Date datetime
-
)
-
-
Declare @startdate datetime
-
Declare @enddate datetime
-
-
-
Set @startdate = '2008-06-01'
-
Set @enddate = '2008-06-16'
-
-
INSERT INTO @Table(Date)
-
Select dateadd(dd,number,@startdate)
-
from master.dbo.spt_values
-
where master.dbo.spt_values.type='p'
-
AND dateadd(dd,number,@startdate)<=(@enddate)
-
-
-- Update all existing records
-
UPDATE Table1 WITH (ROWLOCK)
-
SET Val=@Val
-
FROM Table1 INNER JOIN
-
@Table as T on Table1.BeginningDate = T.Date
-
-
-- Remove all existing records
-
DELETE FROM @Table
-
FROM @Table as T INNER JOIN
-
Table1 ON Table1.BeginningDate = T.Date
-
-
-- Insert new records
-
INSERT INTO Table1(Val,BeginningDate,EndDate)
-
SELECT @Val,Date,Date
-
FROM @Table as T
-
-
Hope this will help you.
Thanks
Edit:^^^^ I like deepuv04's solution.
hmmm. sounds like a very interesting problem. A trigger might help displace some off load some of the comparisons you perform in your stored procedure. However, i cannot be sure what kind of performance benefits you will get considering by design you wish to compare all date values in a given BeginDate EndDate range.
if storedprocedure is the only way you use to add data to Table1. you could write an INSTEAD OF trigger for INSERT on Table1 that handles the decision making of whether to insert or update a row. that way your original stored procedure will only be responsible for INSERTS to TABLE1. - CREATE TRIGGER InsteadTrigger on TABLE1
-
INSTEAD OF INSERT
-
AS
-
BEGIN
-
DECLARE @BD Varchar
-
DECLARE @Val Varchar
-
SET @BD = inserted.BeginningDate
-
SET @Val = inserted.Val
-
if((SELECT Count(ID) FROM TABLE1 WHERE TABLE1.BeginningDate = @BD) > 0)
-
BEGIN
-
UPDATE Table1 WITH (ROWLOCK)
-
SET
-
Val=@Val
-
WHERE
-
BeginningDate = @BD
-
END
-
ELSE
-
BEGIN
-
INSERT INTO TABLE1
-
(
-
Val,
-
BeginningDate,
-
EndDate
-
)
-
VALUES
-
(
-
@Val,
-
@BD,
-
@BD
-
)
-
END
-
END
-
GO
Sign in to post your reply or Sign up for a free account.
Similar topics
by: Darius |
last post by:
I was having an issue inserting data into a table in an I-Series DB2
database. The Insert statement itself is very simple:
Insert into Table1
select * from Table2
These two tables have...
|
by: peter.morin |
last post by:
Issue: I am inserting an Oracle record containing insert trigger via
Access 2002 using the code below. The issue is that the sequence from
the acSaveRecord is not reflected after the insert so...
|
by: hardieca |
last post by:
Hi,
My formview or gridview control stops updating or deleting a record
once the record has a null value.
I have table tblTest with the following
pkID int NOT NULL **IDENTITY COLUMN**...
|
by: =?Utf-8?B?SmltSGVhdmV5?= |
last post by:
I have a program which is looping through a data reader, taking the record
which is read, creating an instance of a class which respresents that record,
and then inserting that record into a...
|
by: bob44 |
last post by:
Hi, I recently created a mysql database using phpmyadmin. I then proceeded to make a form to insert data into the database, but the problem is that the form is only able to insert one record, and...
|
by: dos360 |
last post by:
Hello,
I have two tables, one is a list of activities, the other a list of
participants. I want to insert one record in the activities table and
then using its identity column as foreign key, I...
|
by: AlexanderDeLarge |
last post by:
Hi!
I got a problem that's driving me crazy and I'm desperately in need of help.
I'll explain my scenario:
I'm doing a database driven site for a band, I got these tables for their discography...
|
by: =?Utf-8?B?UGF1bCBQcmV3ZXR0?= |
last post by:
I'm attempting to use LINQ to insert a record into a child table and I'm
receiving a "Specified cast is not valid" error that has something to do w/
the keys involved. The stack trace is:
...
|
by: rando1000 |
last post by:
Okay, here's my situation. I need to loop through a file, inserting records based on a number field (in order) and if the character in a certain field = "##", I need to insert a blank record.
...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
by: ryjfgjl |
last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
|
by: ryjfgjl |
last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
|
by: emmanuelkatto |
last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud.
Please let me know.
Thanks!
Emmanuel
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
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...
|
by: Hystou |
last post by:
There are some requirements for setting up RAID:
1. The motherboard and BIOS support RAID configuration.
2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
|
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,...
|
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...
| |