My goal is to use selects on production without affecting anything else.
When I was new to SQL, I just wrote simple selects, but later I learned this can cause locks.
If I don't care about dirty reads, I can use nolocks on every select or set transaction isolation level to read uncommited.
But I do care about dirty reads, so I went with isolation level snapshot.
Isolation level snapshot writes into tempdb and I don't know if that's a problem or not (extra writing to database means slower DB and less space?)
So what is the least intrusive way to write a select on DB, where I cannot afford slowing it down or creating shared locks? Should I go back to isolation level read uncommited on every query?
This is asked from developer point of view. I don't know details about DB settings, but want to use selects in a way that will cause the least trouble.
Best Answer
The correct answer will depend on a number of things:
You should always get an estimated query plan and look for intensive operations, such as scans of large tables/indexes, or sorts of a large number of rows. One of the most important considerations when working with a live, heavily used database is to avoid driving pages out of the buffer pool by running a query that needs to bring a lot of data into cache.
If you are concerned about your select queries going parallel and consuming excessive CPU, you can limit your query to a single core by adding
OPTION (MAXDOP 1)
to the end of your query.If you don't have Read Committed Snapshot enabled on your database, see if you can get that turned on. That will give you snapshot-like behaviour on your selects, so that you will not block writers. However if you are running long ad-hoc queries and there is a lot of write activity, you'll get a lot of data written to tempdb.
You could also investigate using Resource Governor to limit yourself.