I am using SQL Server 2012.
I have table where I store the users. The max count of users allowed is 1 million. I have a sproc where I am registering users. When I insert I want to make sure that the total number of rows in the users table will not exceed 1 million. I would prefer to use a method which allows maximum concurrency.
I believe I can use a transaction with an ISOLATION LEVEL of SERIALIZABLE and then first count the number of rows and insert if the total count is less than 1 million. My understanding is that SERIALIZABLE Is very restrictive and will cause performance degradation as concurrency increases.
IF(SELECT COUNT(*) FROM Users) < 100000
BEGIN
INSERT INTO Users VALUES (@Name, @Email, @Password)
END
How can I do this atomically so that the total row count is guaranteed to be less than 1 million, but at the same time I do minimal locking to prevent blocking other transactions?
Any solution/best practices for this situation?