tags:

views:

56

answers:

2

Question is a follow up to this.

The SQL in question was

UPDATE stats SET visits = (visits+1)

And the question is, for the purpose of performance, does it matter if you lock all rows in stats in comparison to locking the table stats? Or, if the database uses a page-lock rather than a table/row lock?

+2  A: 

In theory you should lock the table, because 1 lock is cheaper than 1M locks.

Many DBs, though, will promote locks for operations like this. As they see the locks expanding, they'll automatically promote to page and table locks.

But, as with anything, "it depends", and it's better to be specific and lock the table yourself.

Edit:

sigh

Postgres example:

LOCK TABLE mytable IN EXCLUSIVE MODE;
UPDATE mytable SET field = field + 1;
COMMIT;

Here's the deal. This is going to happen ANYWAY, the LOCK TABLE command makes it more explicit, and ensures that your intent, locking the table, is clear and capable before the process takes place.

Would I do this on a 10 row table? No.

Would I do this on a database that I KNEW I had exclusive access to? No, there's no need.

Would I do this on a operational database with a table with a large amount a rows? You bet.

Will Hartung
I've never set a custom lock for something as simple as the above. Are you honestly advocating setting the lock level for a single UPDATE statement?
Evan Carroll
Postgres isn't necessarily a good example. It doesn't normally put row locks into a central lock manager (and doesn't have page locks in that sense at all), and locking the table ahead of time doesn't actually change the operation of the update statement at all- much the same for Oracle. Arguably, this observation just supports your "it depends" answer though :)
araqnid
I've heard on good word from a core dev, that this is totally incorrect as applicable to Pg: http://gist.github.com/428839
Evan Carroll
@EvanCarroll: that which is totally incorrect? btw, good suggestion in that log about using ALTER TABLE to rewrite the table.
araqnid
@araqnid, *"In theory you should lock the table, because 1 lock is cheaper than 1M locks."* -- In the MVCC even if you lock the table, you have to effectively lock all of the rows inside of it, because locking the rows is done by assigning the xmax of the new row anyway, which is required for any update operation. Locking the table just goes one step further and prevents inserts, and other things that don't have to be blocked by an operation that "locks" all of the rows.
Evan Carroll
+3  A: 

There is no predicate on this. Any self respecting DB engine should work this out and realise all rows need updated.

Generally, don't second guess the DB engine: performance is subjectively the same.

Personally,

  • I'd not use table or locking hints unless I have to and know why I'm doing it.
  • I'd not issue a query like this anyway from an application without a WHERE clause
gbn