ããã»ã¹1ã¯ãªãœãŒã¹Aããããã¯ããŸãã
ããã»ã¹2ã¯ãªãœãŒã¹Bããããã¯ããŸãã
ããã»ã¹1ã¯ãªãœãŒã¹Bã«ã¢ã¯ã»ã¹ããããšããŠããŸãã
ããã»ã¹2ã¯ãªãœãŒã¹Aã«ã¢ã¯ã»ã¹ããããšããŠããŸãã
ãã®çµæãäžæ¹ã®ããã»ã¹ãäžæããŠãä»æ¹ã®ããã»ã¹ãå®è¡ãç¶ç¶ã§ããããã«ããå¿ èŠããããŸãã
ããããããã¯çžäºãããã¯ã®æãåçŽãªããŒãžã§ã³ã§ãããå®éã«ã¯ããè€éãªã±ãŒã¹ã«å¯ŸåŠããå¿ èŠããããŸãã ãã®èšäºã§ã¯ãMS SQLã®ã©ã®çžäºããã¯ã«å¯Ÿå¿ããå¿ èŠããããã©ã®ããã«ããããšæŠããã説æããŸãã
çè«ã®ããã
MS SQLãªã©ã®è€éãªDBMSã䜿çšããå Žåãäžè¬çãªã¯ãšãªããããã¯ããæ¹æ³ãšãã®ãªãœãŒã¹ãããã³ãã©ã³ã¶ã¯ã·ã§ã³åé¢ã¬ãã«ãã¯ãšãªã«äžãã圱é¿ãç解ããå¿ èŠããããŸãã
ããã«ããŸã詳ãããªã人ã«ã¯ã次ã®èšäºãèªãããšããå§ãããŸãã
- ããã¯ã®ç²åºŠãšããã¯éå±€
- ããã¯ã¢ãŒã
- ããŒã¬ã³ãžããã¯
- ãã©ã³ã¶ã¯ã·ã§ã³åé¢ã¬ãã«
ãã©ã³ã¶ã¯ã·ã§ã³åé¢ã¬ãã«ãéžæ
ã·ãªã¢ã©ã€ãºå¯èœãªåé¢ã¬ãã«ã§ãã©ã³ã¶ã¯ã·ã§ã³ã䜿çšãããšããããããã¯ãçºçããå¯èœæ§ããããŸãã å埩å¯èœãªèªã¿åãåé¢ã¬ãã«ã䜿çšããå Žåã以äžã§èª¬æãããããããã¯ã®äžéšã¯çºçããŸããã ã³ãããã¬ãã«ã®èªã¿åããã³ãããããããã©ã³ã¶ã¯ã·ã§ã³ã§ã¯ãæãåçŽãªãããããã¯ã®ã¿ãçºçããŸãã ã³ããããããŠããªãèªã¿åãåé¢ã¬ãã«ã®ãã©ã³ã¶ã¯ã·ã§ã³ã¯ãä»ã®ãã©ã³ã¶ã¯ã·ã§ã³ã®é床ã«å®è³ªçã«åœ±é¿ãäžãããå ±æããã¯ã課ããªããããèªã¿åãã«ãããããããã¯ãåŒãèµ·ããããšã¯ã§ããŸããïŒãã ããããŒã¿ããŒã¹ã¹ããŒããå€æŽãããã©ã³ã¶ã¯ã·ã§ã³ã§ã¯ãããããã¯ãçºçããå¯èœæ§ããããŸãïŒã
ãã©ã³ã¶ã¯ã·ã§ã³åé¢ã¬ãã«ã¯ããããããã¯ãèæ ®ããã«ã·ã¹ãã ã®é床ã«åŒ·ã圱é¿ããããããã©ã³ã¶ã¯ã·ã§ã³ãå®è¡ããã¿ã¹ã¯ã«å¿ããŠåé¢ã¬ãã«ãæ£ããéžæããããšãéåžžã«éèŠã§ãã ãµã³ãã«ã®ãã³ãã次ã«ç€ºããŸãã
- ãã©ã³ã¶ã¯ã·ã§ã³ãããŒã¿ããŒã¹å ã®ããŒã¿ãå€æŽããåæã«ãã®ããŒã¿ãããŒã¿ããŒã¹å ã®æ¢åã®ã¬ã³ãŒããšççŸããªãããšã確èªããå Žåãã»ãšãã©ã®å Žåãã·ãªã¢ã©ã€ãºå¯èœãªåé¢ã¬ãã«ãå¿ èŠã§ãã ãã ãã䞊åãã©ã³ã¶ã¯ã·ã§ã³ãžã®æ°ããã¬ã³ãŒãã®æ¿å ¥ãçŸåšã®ãã©ã³ã¶ã¯ã·ã§ã³ã®çµæã«ãŸã£ãã圱é¿ããªãå Žåã¯ãåé¢ã¬ãã«ã®å埩å¯èœèªã¿åãã䜿çšã§ããŸãã
- ããŒã¿ãèªã¿åãã«ã¯ãéåžžããã©ã³ã¶ã¯ã·ã§ã³ãªãã§ããã©ã«ãã®åé¢ã¬ãã«ïŒã³ãããèªã¿åãïŒã䜿çšããã ãã§ååã§ãã ãã ããèªã¿åãäžã«äžéšãå€æŽã§ããéèšãèªã¿åãå Žåãå埩å¯èœãªèªã¿åãã¬ãã«ãŸãã¯ã·ãªã¢ã«åå¯èœãªåé¢ã¬ãã«ã®ãã©ã³ã¶ã¯ã·ã§ã³ã䜿çšããå¿ èŠãããå ŽåããããŸãã
- 絶ããå€åããããŒã¿ã®ãªã¢ã«ã¿ã€ã çµ±èšã衚瀺ããå¿ èŠãããå Žåã¯ãå€ãã®å Žåãèªã¿åãéã³ãããåé¢ã¬ãã«ã䜿çšããããšããå§ãããŸãã ãã®å Žåãçµ±èšã«ã¯äžå®éã®ããŒãã£ããŒã¿ãå«ãŸããŸããïŒãããç®ç«ã€ããšã¯ã»ãšãã©ãããŸãããïŒãã¬ããŒãã®æ§ç¯ã¯ã·ã¹ãã ã®é床ã«å®è³ªçã«åœ±é¿ããŸããã
ãããããã¯ã§åè©Šè¡
æ°åçš®é¡ã®ããžãã¹ãã©ã³ã¶ã¯ã·ã§ã³ãããããªãè€éãªã·ã¹ãã ã§ã¯ãã©ã®ãããªç¶æ³ã§ããããããã¯ãçºçããªãããã«ãã¹ãŠã®ãã©ã³ã¶ã¯ã·ã§ã³ãèšèšã§ãããšã¯èããããŸããã ãããããã¯ã®é²æ¢ã«æéãè²»ããã¹ãã§ã¯ãããŸããããããããã¯ã®å¯èœæ§ã¯éåžžã«å°ããã§ãã ãã ãããŠãŒã¶ãŒãšã¯ã¹ããªãšã³ã¹ãæãªããªãããã«ãçžäºããã¯ãåå ã§æäœãäžæãããå Žåã¯ãæäœãç¹°ãè¿ãå¿ èŠããããŸãã æäœãå®å šã«ç¹°ãè¿ãã«ã¯ãå ¥åããŒã¿ãå€æŽããã«1ã€ã®ãã©ã³ã¶ã¯ã·ã§ã³ã«ã©ããããå¿ èŠããããŸãïŒãŸãã¯æäœå šäœã§ã¯ãªããåSQLãã©ã³ã¶ã¯ã·ã§ã³ãRetryOnDeadlockã®æäœã«ã©ããããå¿ èŠããããŸãïŒã
CïŒã®RetryOnDeadlocké¢æ°ã®äŸã次ã«ç€ºããŸãã
private const int DefaultRetryCount = 6; private const int DeadlockErrorNumber = 1205; private const int LockingErrorNumber = 1222; private const int UpdateConflictErrorNumber = 3960; private void RetryOnDeadlock( Action<DataContext> action, int retryCount = DefaultRetryCount) { if (action == null) throw new ArgumentNullException("action"); var attemptNumber = 1; while (true) { var dataContext = CreateDataContext(); try { action(dataContext); break; } catch (SqlException exception) { if(!exception.Errors.Cast<SqlError>().Any(error => (error.Number == DeadlockErrorNumber) || (error.Number == LockingErrorNumber) || (error.Number == UpdateConflictErrorNumber))) { throw; } else if (attemptNumber == retryCount + 1) { throw; } } finally { dataContext.Dispose(); } attemptNumber++; } }
RetryOnDeadlocké¢æ°ã¯ãææçºçãããããããã¯ã§ãŠãŒã¶ãŒãšã¯ã¹ããªãšã³ã¹ãåäžãããã ãã§ããããšãç解ããããšãéèŠã§ãã éåžžã«é »ç¹ã«çºçããå Žåãç¶æ³ãæªåãããã ãã§ãã·ã¹ãã ã®è² è·ãäœåºŠãå¢å ããŸãã
æãåçŽãªãããããã¯ãšã®æŠã
2ã€ã®ããã»ã¹ãåããªãœãŒã¹ã«ã¢ã¯ã»ã¹ããŠãããé åºãç°ãªãããã«ãããããã¯ãçºçããå ŽåïŒèšäºã®åé ã§èª¬æïŒããªãœãŒã¹ããããã¯ããé åºãå€æŽããã ãã§ååã§ãã ååãšããŠãç¹å®ã®ãªãœãŒã¹ã»ãããç°ãªãæäœã§ãããã¯ãããå Žå ã å¯èœã§ããã°ãåããªãœãŒã¹ãåžžã«æåã«ãããã¯ãããå¿ èŠããããŸã ã ãã®ã¢ããã€ã¹ã¯ããªã¬ãŒã·ã§ãã«ããŒã¿ããŒã¹ã ãã§ãªããäžè¬çã«çžäºããã¯ãçºçãããã¹ãŠã®ã·ã¹ãã ã«é©çšãããŸãã
MS SQLã«é©çšããå Žåããã®ã¢ããã€ã¹ã¯å°ãç°¡ç¥åããªããã次ã®ããã«è¡šçŸã§ããŸããè€æ°ã®ããŒãã«ãå€æŽããããŸããŸãªãã©ã³ã¶ã¯ã·ã§ã³ã§ã¯ãæåã«åãããŒãã«ãå€æŽããå¿ èŠããããŸãã
Shared-> Exclusive lock escalation
ç§ãã¡ã®ãã©ã¯ãã£ã¹ã§æãäžè¬çãªãããããã¯ã¯ã次ã®ããã«ãåé¢ã¬ãã«ãå埩å¯èœèªã¿åããŸãã¯ã·ãªã¢ã«åå¯èœã®ãã©ã³ã¶ã¯ã·ã§ã³ã§çºçããŸãã
- ãã©ã³ã¶ã¯ã·ã§ã³1ã¯ã¬ã³ãŒããèªã¿åããŸãïŒéç³Sããã¯ïŒã
- ãã©ã³ã¶ã¯ã·ã§ã³2ã¯åãã¬ã³ãŒããèªã¿åããŸãïŒ2çªç®ã®Sããã¯ã課ãããŸãïŒã
- ãã©ã³ã¶ã¯ã·ã§ã³1ã¯ã¬ã³ãŒãã®å€æŽãè©Šã¿ããã©ã³ã¶ã¯ã·ã§ã³2ãçµäºããŠãã®Sããã¯ã解æŸããã®ãåŸ ã¡ãŸãã
- ãã©ã³ã¶ã¯ã·ã§ã³2ã¯åãã¬ã³ãŒããå€æŽããããšãããã©ã³ã¶ã¯ã·ã§ã³1ãçµäºããŠSããã¯ã解æŸããã®ãåŸ ã¡ãŸã
ãã®ãããªçžäºããããã³ã°ã®çºçã«ã¯ã1ã€ã®ãªãœãŒã¹ã§ååã§ãããåãã¿ã€ãã®2ã€ã®ãã©ã³ã¶ã¯ã·ã§ã³ãæŠã£ãŠããŸãã æŠãã¯ã1ã€ã®ã¬ã³ãŒãã ãã§ãªããä»ã®ãªãœãŒã¹ã«å¯ŸããŠãå®è¡ã§ããŸãã ããšãã°ã2ã€ã®Serializableãã©ã³ã¶ã¯ã·ã§ã³ãç¹å®ã®å€ãæã€ã¬ã³ãŒãã®æ°ãã«ãŠã³ãããåãå€ãæã€ã¬ã³ãŒããæ¿å ¥ãããšãããããåŸ æ©ããŠãããªãœãŒã¹ãã€ã³ããã¯ã¹ã®ããŒã«ãªããŸãã
ãã®ãããªãããããã¯ãåé¿ããã«ã¯ãã¬ã³ãŒããå€æŽããããšããŠãã2ã€ã®ãã©ã³ã¶ã¯ã·ã§ã³ã®ãã¡2ã€ã ãããããèªã¿åãããšãã§ããå¿ èŠããããŸãã ç¹ã«ãã®ããã«ãæŽæ°ããã¯ãå°å ¥ãããŸããã 次ã®ããã«é©çšã§ããŸãã
SELECT * FROM MyTable WITH (UPDLOCK) WHERE Id = @Id
ORMã䜿çšããŠããŠãããŒã¿ããŒã¹ããã®ãšã³ãã£ãã£ã®èŠæ±æ¹æ³ãå¶åŸ¡ã§ããªãå ŽåãããŒã¿ããŒã¹ããèŠæ±ããåã«ã¬ã³ãŒãããããã¯ããããã«ãçŽç²ãªSQLã§å¥ã®ã¯ãšãªãå®è¡ããå¿ èŠããããŸãã æŽæ°ããã¯ã課ããªã¯ãšã¹ãã¯ããã®ãã©ã³ã¶ã¯ã·ã§ã³ã§ãã®ã¬ã³ãŒãã«ã¢ã¯ã»ã¹ããæåã®ãªã¯ãšã¹ãã§ããããšãéèŠã§ããããããªããšãåããããããã¯ãçºçããŸãããã¬ã³ãŒããå€æŽããããšãã§ã¯ãªããæŽæ°ããã¯ãé©çšããããšãããšãã§ãã
æŽæ°ããã¯ãé©çšããããšã«ãããåããªãœãŒã¹ã«ã¢ã¯ã»ã¹ãããã¹ãŠã®ãã©ã³ã¶ã¯ã·ã§ã³ãé çªã«å®è¡ããŸãããéåžžãåããªãœãŒã¹ãå€æŽãããã©ã³ã¶ã¯ã·ã§ã³ã¯ååãšããŠäžŠè¡ããŠå®è¡ã§ããªããããããã¯æ£åžžã§ãã
ãã®ãããªãããããã¯ã¯ãããŒã¿ãå€æŽããåã«ãã§ãã¯ãããã©ã³ã¶ã¯ã·ã§ã³ã§çºçããå¯èœæ§ããããŸããããã£ãã«å€æŽãããªããšã³ãã£ãã£ã«ã€ããŠã¯ãRetryOnDeadlockã䜿çšã§ããŸãã äºåã®æŽæ°ããã¯ã䜿çšããã¢ãããŒãã¯ãç°ãªãããã»ã¹ã«ãã£ãŠäžŠè¡ããŠé »ç¹ã«å€æŽããããšã³ãã£ãã£ã«ã®ã¿äœ¿çšããã«ã¯ååã§ãã
äŸ
ãŠãŒã¶ãŒã¯ãã€ã³ãã®è³åã泚æããŸãã åã¿ã€ãã®è³åã®æ°ã¯éãããŠããŸãã ã·ã¹ãã ã¯ãå©çšå¯èœãªè³åãããå€ãã®è³åã泚æã§ããªãããã«ããå¿ èŠããããŸãã ããã¢ãŒã·ã§ã³ã®æ§è³ªäžãåãè³åã泚æãããŠãŒã¶ãŒã«å¯ŸããŠå®æçã«ã¬ã€ããçºçããŸãã ãã®ãããªç¶æ³ã§RetryOnDeadlockã䜿çšãããšããŠãŒã¶ãŒã®è¥²æäžã«ãã»ãšãã©ã®å Žåãè³åã®æ³šæã¯Webã¿ã€ã ã¢ãŠãã«ãã£ãŠäœäžããŸãã
æ®ãã®è³ã®æ°ãè³ã®çš®é¡ã®ã¬ã³ãŒãã«ä¿åãããšãè³ã®æ³šæãã©ã³ã¶ã¯ã·ã§ã³ã¯æ¬¡ã®ããã«ãªããŸãã
- æŽæ°ããã¯ãé©çšããããšã«ãããè³ã®çš®é¡ã®èšé²ãååŸããŸãã
- æ®ãã®è³åã®æ°ã確èªããŠãã ããã 0ã®å Žåããã©ã³ã¶ã¯ã·ã§ã³ãå®äºããé©åãªå¿çããŠãŒã¶ãŒã«è¿ããŸãã
- ãŸã è³åãããå Žåã¯ãæ®ãã®è³åã®æ°ã1ã€æžãããŸãã
- 泚æããè³åã®èšé²ãè¿œå ããŸãã
ãããã£ãŠã1人ã®ãŠãŒã¶ãŒã®ã¿ãåãçš®é¡ã®è³åãäžåºŠã«æ³šæã§ããŸãã åãçš®é¡ã®è³åã泚æãããã¹ãŠã®ãŠãŒã¶ãŒãªã¯ãšã¹ãã䞊ãã§ããŸããããã®ã¢ãããŒãã¯ãRetryOnDeadlockãŸãã¯ãããããã¯çºçæã®æ¶è²»è ãžã®ãšã©ãŒåºåãããåªãããŠãŒã¶ãŒãšã¯ã¹ããªãšã³ã¹ãæäŸããŸãã
æ®ãã®è³åã®æ°ãä¿åããã«ã泚æããè³åã®æ°ã«åºã¥ããŠèšç®ããå Žåã泚æããè³åã®æ°ãèšç®ãããšãã«æŽæ°ãããã¯ãé©çšã§ããŸãã 次ã®ããã«ãªããŸãã
SELECT count(*) FROM OrderedPrizes WITH (UPDLOCK) WHERE PrizeId = @PrizeId
以äžã§èª¬æããã»ãšãã©ã®ãããããã¯ã¯åæ§ã®æ¹æ³ã§çºçããŸããå ±æããã¯ãèšå®ããåŸãããŒã¿ãå€æŽããããšããŸãã ãããããããã®åã±ãŒã¹ã«ã¯ç¬èªã®ãã¥ã¢ã³ã¹ããããŸãã
ã€ã³ããã¯ã¹äžå¯èœãªãã£ãŒã«ãã®éžæ
ã€ã³ããã¯ã¹ã«å±ããªããã£ãŒã«ãã«ãã£ãŠã·ãªã¢ã«åå¯èœãªãã©ã³ã¶ã¯ã·ã§ã³ã§ã¬ã³ãŒããæ¢ããŠããå Žåãå ±æããã¯ã¯ããŒãã«å šäœã«é©çšãããŸãã ããããªããšãçŸåšã®ãã©ã³ã¶ã¯ã·ã§ã³ãå®äºãããŸã§ãä»ã®ãã©ã³ã¶ã¯ã·ã§ã³ãåãå€ã®ã¬ã³ãŒããæ¿å ¥ã§ããªãããšã確èªã§ããŸããã ãã®çµæããã®ãã£ãŒã«ãã§éžæãè¡ã£ãŠãããã®ããŒãã«ãå€æŽãããã©ã³ã¶ã¯ã·ã§ã³ã¯ãåæ§ã®ãã©ã³ã¶ã¯ã·ã§ã³ã§çžäºã«ãããã¯ãããŸãã
ãã®ãã£ãŒã«ãã®ã€ã³ããã¯ã¹ïŒãŸãã¯æåã®ãã£ãŒã«ããæ¢ããŠãããã£ãŒã«ãã§ããè€æ°ã®ãã£ãŒã«ãã®ã€ã³ããã¯ã¹ïŒãè¿œå ãããšããã®ã€ã³ããã¯ã¹ã®ããŒã¯ãããã¯ãããŸãã ãã®ãããã·ãªã¢ã«åå¯èœãªãã©ã³ã¶ã¯ã·ã§ã³ã§ã¯ãã¬ã³ãŒããæ¢ããŠããåã«ã€ã³ããã¯ã¹ããããã©ãããèããããšãããã«éèŠã§ãã
èŠããŠããã¹ãéèŠãªç¹ããã1ã€ãããŸããã€ã³ããã¯ã¹ãäžæã§ããå ŽåãèŠæ±ãããããŒã«ã®ã¿ããã¯ãé©çšãããäžæã§ãªãå Žåããã®ããŒã«ç¶ãå€ããããã¯ãããŸãã äžæã§ãªãã€ã³ããã¯ã¹ã«ãã£ãŠç°ãªãã¬ã³ãŒããèŠæ±ããããããå€æŽãã2ã€ã®ãã©ã³ã¶ã¯ã·ã§ã³ã¯ãé£æ¥ããããŒå€ãèŠæ±ãããå Žåãçžäºã«ãããã¯ã§ããŸãã ããã¯éåžžããŸããªç¶æ³ã§ãããRetryOnDeadlockã䜿çšããŠåé¡ãåé¿ããã®ã«ååã§ãããå Žåã«ãã£ãŠã¯ãäžæã§ãªãããŒã§ã¬ã³ãŒãããã«ãããšãã«æŽæ°ããã¯ããããå¿ èŠããããŸãã
æ¿å ¥åã«å©çšå¯èœæ§ã確èªãã
äŸ
è¿œå ããåã«ãããŒã¿ããŒã¹ã«Facebookã«ãã®ãããªIDãæã€ãŠãŒã¶ãŒãååšãããã©ããã確èªããå¿ èŠããããŸãã ããŒã¿ããŒã¹å ã®1è¡ã§äœæ¥ããŠãããããããã ãããããã¯ãããçžäºãããã¯ã®å¯èœæ§ã¯å°ãããšããæããããŸãã ãã ããSerializableïŒããã³ãã®åãã€ã³ããã¯ã¹ã«å«ãŸããïŒã®åé¢ã¬ãã«ãæã€ãã©ã³ã¶ã¯ã·ã§ã³ã§ååšããªãå€ãéžæããããšãããšãããŒãã«ã«ãã2ã€ã®æãè¿ãå€ã®éã®ãã¹ãŠã®ããŒã«å ±æããã¯ãé©çšãããŸãã ããšãã°ãããŒã¿ããŒã¹ã«Id 15ãšId 1025ãããããããã®éã«åäžã®å€ããªãå ŽåãSELECT * FROM Users WHERE FacebookId = 500ãå®è¡ããããšãå ±æããã¯ã15ãã1025ã®ããŒã«é©çšãããŸãã FacebookId = 600ã®ãŠãŒã¶ãŒãæ¿å ¥ããããšãããšãçžäºããã¯ãçºçããŸãã ããŒã¿ããŒã¹å ã«FacebookIdãæºæ¯ã®æ¶è²»è ãæ¢ã«å€æ°ããå Žåãçžäºããããã³ã°ã®å¯èœæ§ã¯äœããRetryOnDeadlockã䜿çšããã®ã«ååã§ãã ãã ããã»ãšãã©ç©ºã®ããŒã¿ããŒã¹ã§ãã®ãããªãã©ã³ã¶ã¯ã·ã§ã³ãå€æ°å®è¡ãããšãããã©ãŒãã³ã¹ã«å€§ãã圱é¿ããã»ã©çžäºããã¯ãé »ç¹ã«çºçããŸãã
æ°ãã顧客ããã®æ¶è²»è ã®äžŠè¡ã€ã³ããŒãã§ãã®åé¡ãçºçããŸããïŒã¯ã©ã€ã¢ã³ãããšã«æ°ãã空ã®ããŒã¿ããŒã¹ãäœæããŸãïŒã çŸåšãã·ã³ã°ã«ã¹ã¬ããã®ã€ã³ããŒãé床ã«æºè¶³ããŠãããããåæå®è¡ããªãã«ããŸããã ããããååãšããŠãäžèšã®äŸãšåæ§ã«åé¡ã¯è§£æ±ºãããæŽæ°ããã¯ã䜿çšããå¿ èŠããããŸãã
SELECT * FROM Users WITH(UPDLOCK) WHERE FacebookId = 500
ãã®å Žåã空ã®ããŒã¿ããŒã¹ãžã®ãã«ãã¹ã¬ããã€ã³ããŒãã§ã¯ãæåã¯ã¹ã¬ãããã¢ã€ãã«ç¶æ ã«ãªããããã¯ã解é€ããããŸã§åŸ æ©ããŸãããããŒã¿ããŒã¹ããã£ã±ãã«ãªããšäžŠå床ãåäžããŸãã ã€ã³ããŒããããããŒã¿ãFacebookIdã«ãã£ãŠé åºä»ããããŠããå Žåããããã䞊è¡ããŠã€ã³ããŒãããããšã¯ã§ããŸãããã 空ã®ããŒã¿ããŒã¹ã«ã€ã³ããŒãããå Žåããã®ãããªé åºä»ãã¯é¿ããŠãã ããïŒãŸãã¯ãæåã®ã€ã³ããŒãäžã«FacebookIdã§ããŒã¿ããŒã¹å ã®ãŠãŒã¶ãŒã®ååšã確èªããªãã§ãã ããïŒã
è€éãªãŠãããã®çžäºé£å
ã·ã¹ãã ã«è€éãªéåäœãããããã®ããŒã¿ãè€æ°ã®ããŒãã«ã«æ ŒçŽãããŠããããã®éåäœã®ç°ãªãéšåã䞊è¡ããŠå€æŽããå€ãã®ãã©ã³ã¶ã¯ã·ã§ã³ãããå Žåããããã®ãã¹ãŠã®ãã©ã³ã¶ã¯ã·ã§ã³ãçžäºããã¯ãåŒãèµ·ãããªãããã«é 眮ããå¿ èŠããããŸãã
äŸ
ããŒã¿ããŒã¹ã«ã¯ãæ¶è²»è ã®å人ããŒã¿ããœãŒã·ã£ã«ãããã¯ãŒã¯ã§ã®åœŒã®èå¥åããªã³ã©ã€ã³ã¹ãã¢ã§ã®æ³šæã圌ã«éãããæçŽã®èšé²ãä¿åãããŸãã
ãœãŒã·ã£ã«ãããã¯ãŒã¯ã«èå¥åãè¿œå ããæçŽãéããè³Œå ¥ãç»é²ãããã©ã³ã¶ã¯ã·ã§ã³ã¯ãæ¶è²»è ãã¹ã¿ãŒã¬ã³ãŒãã®æè¡ãã£ãŒã«ããå€æŽããããšãã§ããŸãã ãããã®ãã©ã³ã¶ã¯ã·ã§ã³ã®ãããã«ããã³ã³ã·ã¥ãŒãIDãååšããŸãã
ãããããã¯ãåé¿ããã«ã¯ã次ã®ãªã¯ãšã¹ãã§ãã©ã³ã¶ã¯ã·ã§ã³ãéå§ããå¿ èŠããããŸãã
SELECT * FROM Customers WITH (UPDLOCK) WHERE Id = @Id
ãã®å Žåãç¹å®ã®æ¶è²»è ã«é¢é£ããããŒã¿ãå€æŽã§ãããã©ã³ã¶ã¯ã·ã§ã³ã¯äžåºŠã«1ã€ã ãã§ãããæ¶è²»è ã®éèšãã©ãã»ã©è€éã§ãã£ãŠãçžäºããã¯ã¯çºçããŸããã
ããŒã¿ã¹ãã¬ãŒãžã¹ããŒã ãå€æŽããŠãé»åã¡ãŒã«ã®éä¿¡ãšè³Œå ¥ã®ç»é²ãæ¶è²»è ã®ãã¯ãã«ã«ããŒããå€æŽããªãããã«ããããšãã§ããŸãã ãã®åŸã泚æãšéä¿¡ãããæçŽã«é¢ããæ å ±ã¯ãæ¶è²»è ã®å€åãšäžŠè¡ããŠå€æŽã§ããŸãã ãã®å Žåãå®éã«ã¯ãã®ããŒã¿ããæ¶è²»è ãéèšã®ç¯å²ããåãåºããŸãã
èŠçŽãããšããŠããããæäœããããã®ãã³ãã¯æ¬¡ã®ããã«èª¬æã§ããŸãã
- ã·ã¹ãã å ã®ãã©ã³ã¶ã¯ã·ã§ã³ãè€æ°ã®éçŽãå€æŽããªãããã«ããå¿ èŠããããŸã
- éçŽãå€æŽãããã©ã³ã¶ã¯ã·ã§ã³ã§ã¯ãéçŽããŒã¿ã«ã¢ã¯ã»ã¹ããæåã®ãªã¯ãšã¹ãã¯ãéçŽã«ãŒãã«æä»ããã¯ãŸãã¯æŽæ°ããã¯ã課ãå¿ èŠããããŸã
- ã·ã¹ãã ã®äžŠå床ãäžããã«ã¯ãéèšãå¯èœãªéãå°ããããå¿ èŠããããŸã
ãã¡ããããããã®ãã³ãã¯ãããããã¯ã®äžèœè¬ã§ã¯ãããŸãããã人çã倧ãã«ä¿é²ã§ããŸãã
é£ç¶ããã¬ã³ãŒãã®çžäºããã¯
ãã®ãããªçžäºãããã¯ã¯ãéåžžã«ç¹å®ã®æ¡ä»¶äžã§çºçããŸãããæ°åã¯ãŸã ãããã«ééããŠããã®ã§ããããã«ã€ããŠè©±ã䟡å€ããããŸãã
äŸ
DirectCRMããã¡ãŒã«ã²ãŒããŠã§ã€ã«ã¬ã¿ãŒãéä¿¡ããããšãéä¿¡ã®äºå®ïŒããŒã¿ããŒã¹å ã®1ã€ã®ãšã³ããªïŒã«ã€ããŠæ¶è²»è ã«ã¢ã¯ã·ã§ã³ãçºè¡ãããŸãã ã¢ã¯ã·ã§ã³IDã¯éåžžã®IDã§ãããåŸç¶ã®ã¬ã³ãŒãããšã«1ãã€å¢å ããŸãã ã¡ãŒã«ãµãŒããŒãžã®ã¬ã¿ãŒã®éä¿¡ãæåãããšãã¡ãŒã«ã²ãŒããŠã§ã€ã¯ããã«ã€ããŠDirectCRMãå ±åããCRMã¯ã¬ã¿ãŒã®éä¿¡ã®æåã«é¢ããã¢ã¯ã·ã§ã³ãçºè¡ããŸããããã¯ãéä¿¡ã®äºå®ã«é¢ããã¢ã¯ã·ã§ã³ãåç §ããŸãïŒãªã³ã¯ã¯HierarchicalCustomerActionsããŒãã«ã«æ ŒçŽãããŸãïŒã é»åã¡ãŒã«ã²ãŒããŠã§ã€ããã®ã¡ãã»ãŒãžãåŠçããæäœãã¹ãçã§ããããïŒ åã®èšäºã§ãããèªãå¿ èŠãããçç±ïŒãïŒã·ãªã¢ã«åå¯èœãªãã©ã³ã¶ã¯ã·ã§ã³ã§ïŒæ£åžžã«éä¿¡ããã¢ã¯ã·ã§ã³ãçºè¡ããããã©ããã確èªããŸãã ãã®ãã§ãã¯ã§ã¯ãã¬ã¿ãŒã®éä¿¡ã®äºå®ã«å¯Ÿå¿ããŠãRootCustomerActionIdã«ãã£ãŠã€ã³ããã¯ã¹å ã®ããŒã«å ±æããã¯ãé©çšãããŸãã ãã ããéä¿¡ã«é¢ããã¢ã¯ã·ã§ã³ã¯æåã«éä¿¡ã®äºå®ãåç §ãããã®ã§ããããã®çºè¡æã«ã¯ãHierarchicalCustomerActionsããŒãã«ã«ãã®ãããªRootCustomerActionIdãæã€åäžã®ã¬ã³ãŒãã¯ãããŸããã ãããã£ãŠã2ã€ã®æ¢åã®ããŒã®éã®ãã¹ãŠã®ããŒã«å ±æããã¯ãé©çšãããŸãã ã¢ã¯ã·ã§ã³IDã¯IDã§ãããããéåžžã«é »ç¹ã«ãã§ãã¯ãããRootCustomerActionIdã¯ãæ¢ã«ããŒãã«ã«ãããã®ããã倧ãããªããããŒãã«å ã®RootCustomerActionIdã®æ倧å€ä»¥äžã®ãã¹ãŠã®ããŒã«ããã¯ãããããŸãã é»åã¡ãŒã«ã²ãŒããŠã§ã€ã§ã¯ãã¡ãã»ãŒãžãè€æ°ã®ã¹ããªãŒã ã§éä¿¡ããããã®çµæã次ã®ç¶æ³ãé »ç¹ã«çºçããŸãã
- DirectCRMã¯ãId NãN + 1ãN + 2ãªã©ã§éä¿¡ãããšããäºå®ã«é¢ããã¢ã¯ã·ã§ã³ãè¿œå ããŸãã
- é»åã¡ãŒã«ã²ãŒããŠã§ã€ã¯ãããããã¹ãŠã®é»åã¡ãŒã«ã䞊è¡ããŠéä¿¡ããŸãã
- ãã£ã¹ãããã¢ã¯ã·ã§ã³ãçºè¡ãããæç¹ã§ãRootCustomerActionIdã®æ倧å€ã¯N-1ã§ãã
- NãN + 1ãN + 2ãªã©ã®éä¿¡ã«æåããã¬ã³ãŒãããããã©ããã確èªããå ŽåãN-1以äžã®RootCustomerActionIdãæã€ã¬ã³ãŒãã«å ±æããã¯ã課ããããŸãã
- åãã©ã³ã¶ã¯ã·ã§ã³ã¯ãç¬èªã®éä¿¡ã¬ã³ãŒããæ¿å ¥ããããšããä»ã®ãã©ã³ã¶ã¯ã·ã§ã³ãå ±æããã¯ã解æŸããã®ãåŸ ã¡ãŸãã
- ãã®çµæããã¹ãŠã®äžŠåãã©ã³ã¶ã¯ã·ã§ã³ã®ãã¡ã1ã€ã ããå®è¡ãããæ®ãã¯ãã³ãã§æåºãããŸãã
HierarchicalCustomerActionsãç §äŒãããšãã«æŽæ°ããã¯ãé©çšãããšãçžäºããã¯ã¯ãªããªããŸããã䞊ååŠçãè¡ãããŸããããã¹ãŠã®ãã©ã³ã¶ã¯ã·ã§ã³ã¯ãæŽæ°ããã¯ã課ããã©ã³ã¶ã¯ã·ã§ã³ãçµäºãããŸã§åŸ æ©ããŸãã
ãã®åé¡ã«ã¯æ¬¡ã®è§£æ±ºçããããŸãã
- ã»ãšãã©ã®å Žåãæ°ããIDã2ã€ã®æ¢åã®IDã®éã«ããããã«ãIDãæåŠããŠã¢ã¯ã·ã§ã³IDãçæããŸãã ãããããšãçžäºãããã¯ã®å¯èœæ§ã¯å°ãããªããŸãã
- éä¿¡ã®äºå®ã«é¢ããã¢ã¯ã·ã§ã³ãçºè¡ããå ŽåãHierarchicalCustomerActionsã«ããšã³ããªãæ¿å
¥ããŸãïŒãã®ã¬ã³ãŒãRootCustomerActionIdã¯CustomerActionIdãšåãã§ãïŒã 次ã«ãRootCustomerActionId = Nã®ã¬ã³ãŒããèŠæ±ãããšãNããã³N + 1ã®å€ã«å
±æããã¯ã匷å¶ãããŸããããã¯ããã®ãããªå€ã®ã¬ã³ãŒããæ¢ã«ååšããããã§ãã Id Nããã³Id N + 1ã§éä¿¡ãããšããäºå®ã«é¢ããã¢ã¯ã·ã§ã³ãåç
§ããŠãéä¿¡æåæã«ã¢ã¯ã·ã§ã³ãæ¿å
¥ããéã®çžäºãããã¯ãåé¿ããããã«ãHierarchicalCustomerActionsã«èŠæ±ãããšãã«æŽæ°ããã¯ãé©çšããå¿
èŠããããŸãã ãã®çµæãè€æ°ã®ã¬ã³ãŒãã䞊è¡ããŠæ¿å
¥ãããšã次ã®ããšãçºçããŸãã
- ãã©ã³ã¶ã¯ã·ã§ã³1ã¯ãRootCustomerActionId = Nã®ã¬ã³ãŒããèŠæ±ããŸããåæã«ãæŽæ°ã¯ããŒå€Nããã³N + 1ãããã¯ããŸãã
- ãã©ã³ã¶ã¯ã·ã§ã³2ã¯ãRootCustomerActionId = N + 1ã®ã¬ã³ãŒããèŠæ±ããŸãã åæã«ã圌ã¯å€N + 1ããã³N + 2ã«æŽæ°ããã¯ã課ãããšããããããã©ã³ã¶ã¯ã·ã§ã³1ã®å®äºãåŸ ã¡ãŸãã
- ãã©ã³ã¶ã¯ã·ã§ã³3ã¯ãRootCustomerActionId = N + 2ã®ãšã³ããªãèŠæ±ããŸãã åæã«ãæŽæ°ã¯ããŒN + 2ããã³N + 3ã®å€ããããã¯ããŸãã ããã§ããã©ã³ã¶ã¯ã·ã§ã³2ã¯ãã©ã³ã¶ã¯ã·ã§ã³3ãå®äºãããŸã§åŸ æ©ããå¿ èŠããããŸãã
- ãã©ã³ã¶ã¯ã·ã§ã³1ãš3ã¯äžŠè¡ããŠåäœããŸãã
- ãã©ã³ã¶ã¯ã·ã§ã³2ã¯é²è¡äžã§ãã