问题
I need to notify users (around 100 clients) of my software (C#, .NET 3.5) when there are new records added in one of the tables of my database (SQL Server 2008). If I understand it right, such operation should qualify as cache invalidation which is exactly what is SqlDependency
for, please correct me on this one if I'm wrong.
For that I can see 2 options:
SqlDependency
which will allow me to receive practically realtime notifications.- Checking table by some timer.
But I never used SqlDependency
before and have no idea how reliable is it really? I mean, can I allow my application just run SqlDependency.Start()
at the start of a day and be sure it will listen for notifications no matter what for 10 hours? Of course I understand I must correctly react on service query notifications. And is it really better to do so in comparison to query by timer in terms of performance of database? Is there any considerable difference actually for database?
I’m considering using SqlDependency
mostly because I’m not sure it’s a best idea to have all of my users to open connection and query the same table in the same database every 5 minutes.
回答1:
I have also been looking for performance metrics of the SqlDependency class, but unfortunately haven't found any. But if I had to guess, I'd say a SqlDependency is more efficient than an external polling mechanism, given everything else stays the same (including polling interval = internal polling interval of the SqlDependency). I say this because when polling externally, you have to consider the overhead that each polling request makes. With a SqlDependency, the service is optimized to reduce the overhead (at the very least, the overhead would be less than external polling). Just my two cents, but I've read at Using SQLDependency vs. periodic polling of a table (performance impact) that the effect is quite the opposite. I will be doing my own testing and post back what works for my scenario.
来源:https://stackoverflow.com/questions/14535356/how-efficient-sqldependency-service-broker-is-compared-to-query-by-timer