views:

282

answers:

3

How can I query the read/write ratio in Sql Server 2005? Are there any caveats I should be aware of?

Perhaps it can be found in a DMV query, a standard report, a custom report (i.e the Performance Dashboard), or examining a Sql Profiler trace. I'm not sure exactly.

Why do I care?

I'm taking time to improve the performance of my web app's data layer. It deals with millions of records and thousands of users.

One of the points I'm examining is database concurrency. Sql Server uses pessimistic concurrency by default--good for a write-heavy app. If my app is read-heavy, I might switch it to optimistic concurrency (isolation level: read committed snapshot) like Jeff Atwood did with StackOverflow.

A: 

I did it using SQL Server Profiler. I just opened it before running application and tested what kind of queries are executed while I'm doing something in application. But I think it's better just for making sure that queries work, don't know if it is convenient for measuring server workload like this. Profiler can also save traced which you can analyse later, so it might work.

Jull
Your answer is vague. Did you sum(reads)/sum(writes) in your trace to get the ratio? It is important info to know. If it's heavy one way or the other, you can optimize SQL Server for that.
Bill Paetzke
+1  A: 

Check out sys.dm_db_index_usage_stats:

  • seeks, scans, lookups are all reads
  • updates are writes

Keep in mind that the counters are reset with each server restart, you need to look at them only after a representative load was run.

There are also some performance counters that can help you:

From these rates you can get a pretty good estimate of read:write ratio of your requests.

after your update

Turning on the version store is probably the best avenue for dealing with concurrency. Rather than using the snapshot isolation explicitly, I'd recommend turning on read committed snapshot:

alter database <dbname> set allow_snapshot_isolation on;
alter database <dbname> set read_committed_snapshot on;

this will make read committed reads (ie. the default ones) to use snapshot instead, so it literally doesn't require any change in the app and can be quickly tested.

You should also investigate if your reads don't get executed under serialization reads isolation level, which is what happens when a TransactionScope is used w/o explicitly specifying the isolation level.

One word of caution that the version store is not exactly free. See Row Versioning Resource Usage. And you should give a read to SQL Server 2005 Row Versioning-Based Transaction Isolation.

Remus Rusanu
Would the read-write ratio = sum(seeks, scans, lookups) / sum(updates)?
Bill Paetzke
I'm not aware of a formal definition of 'read/write ratio' when talking about SQL loads. Many use the term, but is not really well defined. Personally, I would rather use something like (write transactions/batch requests). Using the index stats would give a ratio much more biased to exaggerate the reads, since a single read-only operation (eg. one SELECT statement) will usually result in many seeks/scans/lookups, while a write operation will generally result in few updates, often only one.
Remus Rusanu
Ok, I'll take a look at those stats. Here's a [good set of queries](http://sqlblog.com/blogs/louis_davidson/archive/2009/06/20/read-write-ratio-versus-read-write-ratio.aspx) I found, from the author of [Pro Sql Server 2005 Database Design and Optimization](http://drsql.org/).
Bill Paetzke
And another good candidate for read-write ratio: sys.dm_exec_query_stats http://msdn.microsoft.com/en-us/library/ms189741.aspx: `total_logical_reads/total_logical_writes`.
Remus Rusanu
Why is `allow_snapshot_isolation` necessary? I was only considering `read_committed_snapshot` until you posted that.
Bill Paetzke
You're right. Allow is only needed if the explicit snapshot isolation level is used. To change the behavior of read committed isolation level, is enough to run only set read_committed_snapshot on.
Remus Rusanu
+2  A: 

All apps are heavy read only.

  • An UPDATE is a read for the WHERE clause followed by a write
  • An INSERT must check unique indexes and FKs, which are reads and why you index FK columns

At most you have 15% writes. I saw an article once discussing it, but can't find it again. More likely 1%.

I know that in our 6 million new rows per day DB, we still have a minimum of 95%+ reads (an estimate of course).

Why do you need to know?

Also: How to find out SQL Server table’s read/write statistics?

Edit, based on the question update...

I would leave DB concurrency until you need to change it. We've not change anything out of the box for our 6 million rows + heavy reads too

For tuning our web app, we designed it to reduce round trips (one call = one action, mutliple record sets per call etc)

gbn
Thanks for the info. Good stuff. Concise. I updated my question to explain why I care about the ratio.
Bill Paetzke