Wpis z mikrobloga

Hej. Mam pytanie na temat #sql #mssql - chodzi o TABLOCK podczas insertów. Nakreślę całą sytuację:
Jest sobie baza danych (system rejestracji czasu pracy) do któej podłączone jest 200 osób. 30 osób dodaje rejestrację pracownikowi, reszta generuje raport rejestracji. Czy użycie TABLOCK podczas dodawania rejestracji zwiększy wydajność czy ją zmniejszy? Czy to się robi automatycznie jeśli jest opłacalne? #programowanie

Without the TABLOCK hint, SQL Server will use its normal row-level locking - so for each row it attempts to insert, it will lock that new row being inserted, and then move on. This can add up to a lot of locks that need to be held and managed.


Using TABLOCK just locks the entire table for the process, so while that INSERT ... SELECT ... statement is running, no other transactions can access the table - but there's only a single lock (on the table) in place, so the amount of management overhead is much smaller compared to having dozen, hundreds or even thousands of individual locks on newly inserted rows.
  • 2
  • Odpowiedz
@dj_mysz:

http://stackoverflow.com/questions/5102152/tablock-vs-tablockx

Both hints cause the db to bypass taking more granular locks (like row or page level locks). In principle, more granular locks allow you better concurrency. So for example, one transaction could be updating row 100 in your table and another row 1000, at the same time from two transactions (it gets tricky with page locks, but lets skip that).

In general granular locks is what you want,
  • Odpowiedz