Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

how to speed this up?

Status
Not open for further replies.

karen4201

Programmer
Mar 9, 2006
37
US
I have 2 tables. One gets a nightly feed of data. The second table contains my own calculations based off of the data from the feed.

Here is the sql that I'm using to make the calculations and make my customized data. It's very slow. Any thoughts on how to speed it up would be GREATLY appreciated. I imagine there's probably a great way to perform this operation without a cursor, but I'm not sure how to do it.

Basically, the data in table_a has a date based on a seconds. I'm trying to convert the seconds into a date and time value, then insert into my customized table. Since I'm doing this nightly, I only want to do the inserts of data that I have not previously inserted into the table.


declare @create_date Datetime
declare @the_id varchar(200)
DECLARE my_cursor CURSOR FOR
select table_a.the_id as [the_id], CONVERT(datetime, DATEADD(s, dateseconds, '1970-01-01 00:00:00.000') , 121) as [create_datetime]
from table_a left outer join table_a_custom
on table_a.the_id = table_a_custom.the_id
where table_a_custom.the_id is null
OPEN my_cursor
FETCH FROM my_cursor INTO @the_id, @create_date
WHILE @@FETCH_STATUS = 0
BEGIN
insert into table_a_custom(the_id, the_create_date) values (@the_id, @create_date)
FETCH FROM my_cursor INTO @the_id, @create_date
END
CLOSE my_cursor
DEALLOCATE my_cursor
 
what about this?

insert into table_a_custom(the_id, the_create_date)
select table_a.the_id, CONVERT(datetime, DATEADD(s, dateseconds, '1970-01-01 00:00:00.000') , 121)
from table_a left outer join table_a_custom
on table_a.the_id = table_a_custom.the_id
where table_a_custom.the_id is null

Denis The SQL Menace
SQL blog:
Personal Blog:
 
Thank you! I knew there must be an easier way...just wasn't sure how to include the query into the insert statement. Thanks :)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top