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 strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to update table nightly from massive view

Status
Not open for further replies.

hblabonte

Programmer
Oct 3, 2001
84
US
Hi all,

I've got a very invloved view joining quite a number of tables. It takes 30+ minutes to run because of all the data we're pulling.

To make things easier on the user, I'd like to populate a temp table each night with this data (and report from that). I'm not sure how to go about this as I haven't done much with stored procedures.

any tips?
 
Assuming your view's SELECT statement contains all the data that you want in your reporting table, the stored procedure to repopulate the reporting table might look something like this:
Code:
CREATE PROC uspMakeReportingTable
AS
  IF EXISTS(SELECT * FROM Information_Schema.Tables
            WHERE Table_Name = 'ReportingTable')
      DROP TABLE ReportingTable

  SELECT *
  INTO ReportingTable
  FROM view
But you really don't need the view at all, if you will be reporting from the table. So take the SELECT from the view, add the INTO clause, and use it in your stored procedure. Good luck!

--John [rainbow]
-----------------------------------
Behold! As a wild ass in the desert
go forth I to do my work.
--Gurnie Hallock (Dune)
 
Thanks John. Quick question to clarify. Do I need to name the columns in my view the same as in my temp table?

I'm wondering how that select into statement would work otherwise.

Thanks again,
Heather
 
Using the code above, the column names in the view will become the column names in the reporting table. If you want different column names in the table, you will have to alias each column in the SELECT clause. Can you eliminate the view altogether and put the appropriate SELECT right in the store procedure?

--John [rainbow]
-----------------------------------
Behold! As a wild ass in the desert
go forth I to do my work.
--Gurnie Hallock (Dune)
 
You might also consider looking at your indexing. Do you have indexes on all the join fields (Fks need to have indexesspecifically created, PKs get created when they are defines as PKs.)

BUt yes get rid of the view and act on the tables directly, it will be more efficient. SInce you have a lot of joins, I'd steal teh SQL for the joins and paste it into the SP to save time recreating it. But no real need to use the view at all if yo uare using an sp.

Questions about posting. See faq183-874
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top