論“萬劍歸宗”思想對開發設計的一點“膚淺”作用

{"type":"doc","content":[{"type":"heading","attrs":{"align":null,"level":1},"content":[{"type":"text","text":"爲什麼叫“萬劍歸宗”?","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"這裏有一個富含仙俠中二氣息的霸氣名字:“","attrs":{}},{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":"萬劍歸宗","attrs":{}},{"type":"text","text":"”","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"這其實只是爲了忽悠大家注意力而故意取得名字,","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"實際上就是","attrs":{}}]},{"type":"bulletedlist","content":[{"type":"listitem","content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"機器學習中大家說的比較多的 歸一化 (參考 ","attrs":{}},{"type":"link","attrs":{"href":"https://zhuanlan.zhihu.com/p/27627299","title":null},"content":[{"type":"text","text":"爲什麼要對數據進行歸一化處理? - 知乎 (zhihu.com)","attrs":{}}]},{"type":"text","text":")","attrs":{}}]}],"attrs":{}},{"type":"listitem","content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"數學歸納法 (參考 ","attrs":{}},{"type":"link","attrs":{"href":"https://zh.wikipedia.org/wiki/%E6%95%B0%E5%AD%A6%E5%BD%92%E7%BA%B3%E6%B3%95","title":null},"content":[{"type":"text","text":"數學歸納法 - 維基百科,自由的百科全書 (wikipedia.org)","attrs":{}}]},{"type":"text","text":")","attrs":{}}]}],"attrs":{}}],"attrs":{}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"我們不用學術性的嚴謹講法去闡述這些概念,","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"畢竟我也就是個數學的戰5渣,說也說不清楚","attrs":{}}]},{"type":"heading","attrs":{"align":null,"level":1},"content":[{"type":"text","text":"先入爲主誤區思維舉例","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"下面我們先舉個","attrs":{}},{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":"真實案例","attrs":{}},{"type":"text","text":"(具體業務內容有所調整)來闡述先入爲主的誤區","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"導致看起來沒毛病卻實際上不好用挺麻煩的情況","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"有類似這樣一個業務:","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"爲了吸引用戶,業務部門打算推出像任天堂的switch 賬號可以一臺機器關聯多個不同賬號且能共享遊戲使用權一樣的福利,","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"讓原本美國,中國,加拿大三個國家不同子站點不同賬號管理機制下分開的會員合併在一起,讓任意一個國家賬號開通了會員,玩家其他國家賬號同樣享有相等的會員權力,","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"至於玩家關聯的賬號提供通道給玩家自己關聯","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"ps:這裏的例子,請大家先忽略爲什麼不讓三個國家都能使用同一個賬號這麼明顯最簡單的解決方案,就當這是鎖區或者前人留下的一動要炸掉整個系統的不可抗力因素","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"heading","attrs":{"align":null,"level":2},"content":[{"type":"text","text":"業務需求分析","attrs":{}}]},{"type":"numberedlist","attrs":{"start":1,"normalizeStart":1},"content":[{"type":"listitem","content":[{"type":"paragraph","attrs":{"indent":0,"number":1,"align":null,"origin":null},"content":[{"type":"text","text":"美國,中國,加拿大 三個國家三個不同賬號,","attrs":{}}]}],"attrs":{}},{"type":"listitem","content":[{"type":"paragraph","attrs":{"indent":0,"number":2,"align":null,"origin":null},"content":[{"type":"text","text":"本來業務上所有賬號都不會有同名存在,也就是一個賬號是唯一,即使不同國家也不允許相同","attrs":{}}]}],"attrs":{}},{"type":"listitem","content":[{"type":"paragraph","attrs":{"indent":0,"number":3,"align":null,"origin":null},"content":[{"type":"text","text":"一個玩家可以有 1個或多個賬號","attrs":{}}]}],"attrs":{}},{"type":"listitem","content":[{"type":"paragraph","attrs":{"indent":0,"number":4,"align":null,"origin":null},"content":[{"type":"text","text":"一個玩家可以有 0個或1個會員","attrs":{}}]}],"attrs":{}},{"type":"listitem","content":[{"type":"paragraph","attrs":{"indent":0,"number":5,"align":null,"origin":null},"content":[{"type":"text","text":"一個會員可以在美國,中國,加拿大 三個國家都起作用","attrs":{}}]}],"attrs":{}},{"type":"listitem","content":[{"type":"paragraph","attrs":{"indent":0,"number":6,"align":null,"origin":null},"content":[{"type":"text","text":"需要保存新的賬號關聯關係","attrs":{}}]}],"attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":6,"align":null,"origin":null},"content":[{"type":"text","text":"總結:核心問題在於實現保存賬號關聯關係","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"heading","attrs":{"align":null,"level":2},"content":[{"type":"text","text":"樸實無華的設計","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"這時候小白同學已經明白了一切根本焦點 保存賬號關聯關係,","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"就開始了樸實無華的設計思考:","attrs":{}}]},{"type":"numberedlist","attrs":{"start":1,"normalizeStart":1},"content":[{"type":"listitem","content":[{"type":"paragraph","attrs":{"indent":0,"number":1,"align":null,"origin":null},"content":[{"type":"text","text":"我們需要一段關係記錄來記錄一個玩家的賬號關係,","attrs":{}}]}],"attrs":{}}]},{"type":"paragraph","attrs":{"indent":1,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"所以新增 表 [PlayerMapping] 來保存","attrs":{}}]},{"type":"numberedlist","attrs":{"start":1,"normalizeStart":2},"content":[{"type":"listitem","content":[{"type":"paragraph","attrs":{"indent":0,"number":2,"align":null,"origin":null},"content":[{"type":"text","text":"一個玩家一段關係一條記錄數據少,看起來簡單,找起來方便","attrs":{}}]}],"attrs":{}},{"type":"listitem","content":[{"type":"paragraph","attrs":{"indent":0,"number":3,"align":null,"origin":null},"content":[{"type":"text","text":"多個國家,嗯,現在三個國家三個字段,簡單明瞭","attrs":{}}]}],"attrs":{}},{"type":"listitem","content":[{"type":"paragraph","attrs":{"indent":0,"number":4,"align":null,"origin":null},"content":[{"type":"text","text":"嗯,這樣看來,沒有多個賬號mapping,mapping 表也不需要寫數據","attrs":{}}]}],"attrs":{}},{"type":"listitem","content":[{"type":"paragraph","attrs":{"indent":0,"number":5,"align":null,"origin":null},"content":[{"type":"text","text":"會員表原來就會記錄哪個賬號是會員,join一下mapping表,就可以拿到玩家所有的會員賬號了,簡單","attrs":{}}]}],"attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":5,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":5,"align":null,"origin":null},"content":[{"type":"text","text":"最後 PlayerMapping 表就是這樣","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"| id | USAPlayer | ChinaPlayer | CANPlayer |","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"| ----| ---- | ---- | ---- |","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"| 1 | Gold | Shen | girl |","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"| 2 | Gddd | Sddd | null |","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"看起來好像也沒啥大問題,雖然感覺要新加國家會加字段這點非常彆扭","attrs":{}}]},{"type":"heading","attrs":{"align":null,"level":2},"content":[{"type":"text","text":"實際使用","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"這裏爲了簡單,就只舉sql僞碼的兩個例子說明:","attrs":{}}]},{"type":"heading","attrs":{"align":null,"level":3},"content":[{"type":"text","text":"新增mapping關係","attrs":{}}]},{"type":"codeblock","attrs":{"lang":"sql"},"content":[{"type":"text","text":"IF not EXISTS(select top 1 1 from [dbo].[PlayerMapping] C WITH(NOLOCK)\nON c.USAPlayer = @Player OR c.ChinaPlayer = @Player OR c.CANPlayer = @Player)\nBEGIN\n\n insert into [dbo].[PlayerMapping]\n (\n USAPlayer \n ,ChinaPlayer \n ,CANPlayer \n )\n select CASE when @Country = 'USA' then @Player when @MappingCountry = 'USA' then @MappingPlayer else null end,\n CASE when @Country = 'China' then @Player when @MappingCountry = 'China' then @MappingPlayer else null end,\n CASE when @Country = 'CAN' then @Player when @MappingCountry = 'CAN' then @MappingPlayer else null end\n\nelse \n UPDATE c \n set c.USAPlayer = CASE when @Country = 'USA' then @Player when @MappingCountry = 'USA' then @MappingPlayer else c.USAPlayer end,\n c.ChinaPlayer= CASE when @Country = 'China' then @Player when @MappingCountry = 'China' then @MappingPlayer else c.ChinaPlayer end,\n c.CANPlayer = CASE when @Country = 'CAN' then @Player when @MappingCountry = 'CAN' then @MappingPlayer else c.CANPlayer end\n from [dbo].[PlayerMapping] c\n where c.USAPlayer = @Player OR c.ChinaPlayer = @Player OR c.CANPlayer = @Player\nend ","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"heading","attrs":{"align":null,"level":3},"content":[{"type":"text","text":"查找所有有會員折扣的賬號信息","attrs":{}}]},{"type":"codeblock","attrs":{"lang":"sql"},"content":[{"type":"text","text":"DECLARE @Players table \n(\n USAPlayer varCHAR(60)\n ,ChinaPlayer varCHAR(60)\n ,CANPlayer varCHAR(60)\n ,Discount DECIMAL(18,2)\n)\n\nINSERT INTO @Players\n(\n Discount\n ,USAPlayer\n ,ChinaPlayer\n ,CANPlayer\n)\nSELECT \n A.Discount\n ,CASE when A.Country = 'USA' then A.Player else isnull(c.USAPlayer,'') end\n ,CASE when A.Country = 'China' then A.Player else isnull(C.ChinaPlayer,'') end\n ,CASE when A.Country = 'CAN' then A.Player else isnull(c.CANPlayer,'') end\nFROM dbo.Player A WITH(NOLOCK) \nLEFT JOIN [dbo].[PlayerMapping] C WITH(NOLOCK)\nON c.USAPlayer = A.Player OR c.ChinaPlayer = A.Player OR c.CANPlayer = A.Player\nWHERE A.Status='會員'\n\nSELECT DISTINCT\n Discount\n ,Player\nFROM (\nSELECT \n Discount\n ,USAPlayer AS Player\nFROM @Players\nWHERE USAPlayer <> ''\nUNION ALL\nSELECT \n Discount\n ,ChinaPlayer AS Player\nFROM @Players\nWHERE ChinaPlayer <> ''\nUNION ALL\nSELECT \n Discount\n ,CANPlayer AS Player\nFROM @Players\nWHERE CANPlayer <> ''\n) A\n","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"en.... ","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"是不是大家都覺得這兩段sql 都挺複雜的呢?","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"可能是作者sql功力不行,大家可以試試看能不能寫出在這樣設計下更好更快更簡單的sql呢?","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"heading","attrs":{"align":null,"level":1},"content":[{"type":"text","text":"如果重頭用\"萬劍歸宗\"來思考呢?","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"好吧,其實我們是用 ","attrs":{}},{"type":"link","attrs":{"href":"https://zh.wikipedia.org/wiki/%E7%A9%B7%E4%B8%BE%E6%B3%95","title":""},"content":[{"type":"text","text":"窮舉法","attrs":{}}]},{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":" 和","attrs":{}},{"type":"link","attrs":{"href":"https://zh.wikipedia.org/wiki/%E6%95%B0%E5%AD%A6%E5%BD%92%E7%BA%B3%E6%B3%95","title":""},"content":[{"type":"text","text":"數學歸納法","attrs":{}}]},{"type":"text","text":"來思考","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"大家都知道 窮舉法 實際是一種數學證明法,","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"一個證明法怎麼能幫助我們思考呢?","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"當然是直接不行,但它可以用來證明我們想法簡不簡單,好不好用","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"比如上面樸實無華的設計","attrs":{}}]},{"type":"heading","attrs":{"align":null,"level":2},"content":[{"type":"text","text":"如何用 窮舉法 證明設計是不是簡明?","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"我們設計 mapping 表的目的是爲了幫助我們找到用戶實際有多少個賬號,","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"那麼我們需要證明的命題就是當 mapping 表 有N條mapping 記錄時能幫我們用一個player名字找到實際有多少個賬號","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"假設 這個player 叫 Gold","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"當N = 0,mapping表不存在 Gold,找到賬號爲0 , 不對勁,玩家肯定得有個賬號,否則Gold 從那裏來? ","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"當N= 1, mapping 記錄有三個字段 USAPlayer,ChinaPlayer, CANPlayer, 無法確定Gold唯一確定規則,只能其中一個等於Gold 就算有效","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"如上,每一個證明,我們都沒辦法直接用最簡單的條件確定,要不得已用多餘的規則確定","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"所以根據以上問題,我們可以嘗試做出如下調整","attrs":{}}]},{"type":"blockquote","content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"當N = 0,mapping表不存在 Gold,找到賬號爲0 , 不對勁,玩家肯定得有個賬號,否則Gold 從那裏來?","attrs":{}}]}],"attrs":{}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"那麼我們保證即使玩家只有一個賬號,mapping 表也有數據,那麼 N=0的情況不存在了,那麼也是證明有效","attrs":{}}]},{"type":"blockquote","content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"當N= 1, mapping 記錄有三個字段 USAPlayer,ChinaPlayer, CANPlayer, 無法確定Gold唯一確定規則,只能其中一個等於Gold 就算有效","attrs":{}}]}],"attrs":{}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"問題出在有三個字段,那麼我們把","attrs":{}},{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":"三個字段砍成一個字段","attrs":{}},{"type":"text","text":"不就解決這個問題了嗎?","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"PlayerMapping 表就是這樣","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"| id | Player | ","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"| ----| ---- | ","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"| 1 | Gold | ","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"| 2 | Gddd | ","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"| 3 | Shen |","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"| 4 | girl |","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"| 5 | Sddd |","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"但是這樣好像無法mapping,應該加mapping 綁定id 去確定關係,就是這樣","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"| id | Player | MappingId |","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"| ----| ---- | ---- |","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"| 1 | Gold | M |","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"| 3 | Shen | M |","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"| 4 | girl | M |","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"這樣再次套用窮舉法:","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"假設 這個player 叫 Gold,MappingId 都爲M","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"當N = 0, 不存在這樣情況,證明成立","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"當N= 1,那必定只有一條 Gold ,證明成立","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"當N=2, Gold 對應的MappingId 可以找賬號信息,證明成立","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"當N=3, Gold 對應的MappingId 可以找賬號信息,證明成立","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"當N=4,一個玩家暫時沒有那麼過country,證明成立","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"當N= 。。。。。 ","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"哇哦,這樣好像沒有問題了耶","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"那麼我們確實沒有問題了嗎?","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"細看我們的窮舉內容,只列舉了一個玩家的三個國家情況,如果國家還要擴充呢?100個玩家呢?10000個玩家呢?","attrs":{}}]},{"type":"heading","attrs":{"align":null,"level":2},"content":[{"type":"text","text":"如何用 數學歸納法 證明設計是不是簡明?","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"其實吧,對我們程序員來說,","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"我們不是數學家,我們不需要寫什麼證明式,","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"我們的證明式其實就是我們的程序,","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"比如上面的命題","attrs":{}}]},{"type":"blockquote","content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"當 mapping 表 有N條mapping 記錄時能幫我們用一個player名字找到實際有多少個賬號","attrs":{}}]}],"attrs":{}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"我們按照現在的mapping 表 設計","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"| id | Player | MappingId |","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"| ----| ---- | ---- |","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"| 1 | Gold | M1 |","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"| 2 | Gddd | M2 |","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"| 3 | Shen | M1 |","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"| 4 | girl | M1 |","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"| 5 | Sddd | M2 |","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"寫出來的sql 就是這樣","attrs":{}}]},{"type":"codeblock","attrs":{"lang":"sql"},"content":[{"type":"text","text":"declare @mappingid = SELECT top 1 mappingid from [dbo].[PlayerMapping] C WITH(NOLOCK) where c.Player = @Player\n\nselect Player \nFROM [dbo].[PlayerMapping] C WITH(NOLOCK)\nwhere c.MappingId = @mappingid","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"這樣的 證明式 我們自己看出合不合理,簡明不簡明,實在不行還可以跑程序測試","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"當然如果我們寫不出這樣的sql,必須添加if else ,那必然是我們方案還存在一些特殊情況,這種時候就需要我們自行取捨,找到一個平衡的點","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"heading","attrs":{"align":null,"level":2},"content":[{"type":"text","text":"新方案實際使用","attrs":{}}]},{"type":"heading","attrs":{"align":null,"level":3},"content":[{"type":"text","text":"新增mapping關係","attrs":{}}]},{"type":"codeblock","attrs":{"lang":"sql"},"content":[{"type":"text","text":"declare @mappingid VARCHAR(60) = SELECT top 1 mappingid from [dbo].[PlayerMapping] C WITH(NOLOCK) where c.Player = @Player\n\nIF(@mappingid IS NULL)\nBEGIN\nSET @mappingid = UUID()\nEND\n\ninsert into [dbo].[PlayerMapping]\n (\n Player \n ,MappingId \n )\n select @mappingPlayer,@mappingid","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"heading","attrs":{"align":null,"level":3},"content":[{"type":"text","text":"查找所有有會員折扣的賬號信息","attrs":{}}]},{"type":"codeblock","attrs":{"lang":"sql"},"content":[{"type":"text","text":"SELECT \n A.Discount\n ,c.Player\nFROM dbo.Player A WITH(NOLOCK) \ninner JOIN [dbo].[PlayerMapping] b WITH(NOLOCK)\nON b.Player = A.Player \ninner JOIN [dbo].[PlayerMapping] c WITH(NOLOCK)\nON c.MappingId = b.MappingId \nWHERE A.Status='會員'","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"heading","attrs":{"align":null,"level":1},"content":[{"type":"text","text":"總結","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"其實說了那麼多,大家是不是已經發現了,","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"什麼 窮舉法啊 數學歸納法啊 從初中開始,","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"我們都是學過的,","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"並且我們經常做事情也是這樣做的,","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"但是爲什麼我們卻時常做出東東總是差別人家孩子那麼一丟丟呢?","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"個人覺得還是我們心中差了一個字: 靜","attrs":{}}]}]}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章