1

I have SQL Server 2016. I have two tables with the same structure, one contains historical data, and the other one contains current data.
I have a view that programs use, that returns the content of both tables.

CREATE TABLE A (int Id IDENTITY(1,1) NOT NULL, othercolumns, primary key, ect.)
CREATE TABLE B (int Id NOT NULL, othercolumns, primary key, ect.)
CREATE VIEW MyView AS SELECT * FROM A UNION ALL SELECT * FROM B

CREATE TRIGGER TR_INSTEADOFINSERT_ON_MyView on MyView
INSTEAD OF INSERT
AS
BEGIN
INSERT INTO A (columns)
   SELECT data
   FROM inserted
END 

I was wondering if the view gets re-created everytime an insert is done in the view. Because if I check in the sys.dm_exec_sessions and sys.db_exec_requests, I always see the "CREATE TRIGGER TR_INSTEADOFINSERT" in the query text beign executed.

enter image description here

Now I know that it's taking a long time because it's a big table and I have a lot of activity on the server. But I was just wondering why I keep seeing the "CREATE TRIGGER" in the sql_text of the query being executed? Is it re-created or not? What's the most efficient way to do this? Should I modify the programs to reference the table directly when inserting?

1 Answer 1

2

The trigger is not being recreated. sql_text in sys.db_exec_requests is at the batch level, not the query level. See eg

https://blogs.msdn.microsoft.com/dbrowne/2013/01/18/my-favorite-query-for-investigating-sql-server-performance/

For an example of how to parse out the currently-running query from the larger batch.

That wait is to fetch the page that the row is written to, either on the Clustered Index or some non-clustered index. So I would look at the index design of the table, and see if it should be better-optimized for inserts.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.