Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
223 views
in Technique[技术] by (71.8m points)

SQL Server concurrency issue while transaction amount real time limit validation

I have transaction table like below where transaction entry will insert and after that validation is happening for transaction such as no account number perform transaction more than 5000 ( 5K) in a day.

Now in below scenario both the transaction will process successfully as its comes from different session via API in same time.

tranid Amount AccountNumber TrnTime
1001 5000 12345 2021-01-31 20:20:57.713
1002 2000 12345 2021-01-31 20:20:57.713
question from:https://stackoverflow.com/questions/66058232/sql-server-concurrency-issue-while-transaction-amount-real-time-limit-validation

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Your query slowness is almost certainly down to :

  1. Lack of suitable indexes to quickly find the associated rows
  2. The function being applied to the TrnTime column - forcing a table scan

If you create an additional calculated column for MONTH(TrnTime) and include that in a non-clustered index with Amount and DebitAccount then your query would be quicker.

Also, calculating a balance (which I assume you are by calculating the sum of amounts on an account) while using NOLOCK is full of dangers through phantom reads and repeated reads so I would not trust those query results.

With the size of the table, also consider partitioning it - either by MONTH(TrnTime) or by DebitAccount columns


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...