mysql - how to simulate slow update sql? -
dba told me there slow sql should optimize,
and there 10 same update sql , longest time 56 seconds.
the business logic of sql that, if user wins award , should decrease 1 stock of award.
i think caused high concurrent, in case exist lock competion. decided repropduce in local environment, let many thread execute update operation simultaneously, cannot reproduce above slow sql, below experiment result:
thred count|100|500|1000|2000 -----|-----|-----|-----|----- took time(ms)|149|490|1064|1996
even 2000 update sql execute concurrently, need 1996
ms execute all. how reprduce these slow sql?
my code
@test public void test_concurrent_update() throws interruptedexception { int nthreads = 50; //thread count executorservice pool = executors.newfixedthreadpool(nthreads); final countdownlatch startlatch = new countdownlatch(nthreads); final countdownlatch endlatch = new countdownlatch(nthreads); long starttime = system.currenttimemillis(); (int = 0; < nthreads; i++) { pool.submit(new runnable() { @override public void run() { startlatch.countdown(); // let threads concurrently execute update operation try { startlatch.await(); } catch (interruptedexception e) { e.printstacktrace(); } mapper.updateawardstock(); // reduce stock mybatis endlatch.countdown(); system.out.printf("%s down%n",thread.currentthread().getname()); } }); } endlatch.await(); // wait threads finished executing long endtime = system.currenttimemillis(); system.out.printf("%d threads concurrent update stock took time: %d(ms)%n",nthreads,(endtime-starttime)); } @update("update award_stock set stock = stock-1 award_id = 1 , stock>0 limit 1") void updateawardstock();
Comments
Post a Comment