Wednesday, March 21, 2012

Indexing table-valued function?

I am using a multi-statement table-valued function to assemble data from several tables and views for a report. To do this, I INSERT data into the first few columns and then use UPDATEs to put data additional data into each row. Each UPDATE uses a WHERE criteria that identifies a unique row, based on the value of the first few columns.

The problem I'm having is that the UPDATEs are taking forever to execute. I believe the reason is that the temporary table that's created for the function is not indexed, so each row update requires a complete search of several columns.

In other situations I've been able to define one column as a primary key for the temporary table, but in this situation the primary key would have to consist of four columns, which doesn't seem to be allowed in the table definition for the function.

Is there any way to create indexes for the temporary tables that are created for multistatement table-valued functions? I think that would improve the UPDATE performance dramatically.

Thanks,

Lee Silverman
JackRabbit Sports

-Is it not possible to insert the data with one sql statement that would join all the needed tables together?

-Yes, temporary tables can be indexed just like regular tables.

Code Snippet

create unique clustered idex [IX_TestIndex] on #TmpTable (colA, colB, colC, colD)

No comments:

Post a Comment