Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can SQL CLR triggers do this? Or is there a better way?

I want to write a service (probably in c#) that monitors a database table. When a record is inserted into the table I want the service to grab the newly inserted data, and perform some complex business logic with it (too complex for TSQL).

One option is to have the service periodically check the table to see if new records have been inserted. The problem with doing it that way is that I want the service to know about the inserts as soon as they happen, and I don't want to kill the database performance.

Doing a little research, it seems like maybe writing a CLR trigger could do the job. I could write trigger in c# that fires when an insert occurs, and then send the newly inserted data to a Windows or WCF service.

What do you think, is that a good (or even possible) use of SQL CLR triggers?

Any other ideas on how to accomplish this?

like image 958
MrDustpan Avatar asked Jan 08 '09 20:01

MrDustpan


People also ask

What is CLR trigger in SQL?

A trigger is a special type of stored procedure that automatically runs when a language event executes. Because of the Microsoft SQL Server integration with the . NET Framework common language runtime (CLR), you can use any . NET Framework language to create CLR triggers.

Does triggers improve query performance?

It decreases performance on the query by definition: the query is then doing something it otherwise wasn't going to do. The other way to look at it is this: if you were going to manually be doing whatever the trigger is doing anyway then they increase performance by saving a round trip.

Do SQL triggers affect performance?

So the answer is No, not without a heavy cost to implement the logic in application level . As I know, triggers will affect performance in insert, update and delete operations. but doesn't affect read performance.

How does SQL CLR work?

CLR Integration Security NET Framework common language runtime (CLR) manages and secures access between different types of CLR and non-CLR objects running within SQL Server. These objects may be called by a Transact-SQL statement or another CLR object running in the server.


3 Answers

Probably you should de-couple postprocessing from inserting:

In the Insert trigger, add the record's PK into a queue table.

In a separate service, read from the queue table and do your complex operation. When finished, mark the record as processed (together with error/status info), or delete the record from the queue.

like image 166
devio Avatar answered Oct 13 '22 18:10

devio


What you are describing is sometimes called a Job Queue or a Message Queue. There are several threads about using a DBMS table (as well as other techniques) for doing this that you can find by searching.

I would consider doing anything iike this with a Trigger as being an inappropriate use of a database feature that's easy to get into trouble with anyway. Triggers are best used for low-overhead dbms structural functionality (e.g. fine-grained referential integrity checking) and need to be lightweight and synchronous. It could be done, but probably wouldn't be a good idea.

like image 23
dkretz Avatar answered Oct 13 '22 17:10

dkretz


I would suggest having a trigger on the table that calls the SQL Server Service Broker, that then (asynchronously) executes a CLR stored procedure that does all your work in a different thread.

like image 2
Chris KL Avatar answered Oct 13 '22 18:10

Chris KL