{"id":203,"date":"2023-07-05T17:12:26","date_gmt":"2023-07-05T09:12:26","guid":{"rendered":"https:\/\/miie.net\/?p=203"},"modified":"2023-07-05T17:12:26","modified_gmt":"2023-07-05T09:12:26","slug":"using-guid-uiniqueidentifier-and-time","status":"publish","type":"post","link":"https:\/\/diji.net\/?p=203","title":{"rendered":"\u6539\u8fdbGUID\u4f5c\u4e3a\u4e3b\u952e\u65f6\u7684\u95ee\u9898"},"content":{"rendered":"<p>\u6539\u8fdbGUID\u4f5c\u4e3a\u4e3b\u952e\u65f6\u7684\u95ee\u9898<\/p>\n<p>\u5728\u63a5\u624b\u67d0\u4e2a\u9879\u76ee\u7684\u65f6\u5019\uff0c \u8be5\u9879\u76ee\u6570\u636e\u5e93\u4f7f\u7528\u4e86GUID\uff08UniqueIdentifier)\u4f5c\u4e3a\u5927\u90e8\u5206\u8868\u7684\u4e3b\u952e\uff0c\u540c\u65f6\u4e5f\u9ed8\u8ba4\u4f7f\u7528\u4e86\u805a\u96c6\u7d22\u5f15\u3002 \u5f53\u6570\u636e\u91cf\u589e\u52a0\u7684\u65f6\u5019\uff0c \u53d1\u73b0\u4f1a\u4ea7\u751f\u5927\u91cf\u7684\u78c1\u76d8\u788e\u7247\u3002 \u56e0\u6b64\u8fd0\u7ef4\u8fd8\u4e13\u95e8\u5199\u4e86\u4e2ajob\u4efb\u52a1\u6765\u6574\u7406\u788e\u7247\u3002<br \/>\n\u8fd9\u4e3b\u8981\u662f\u7531\u4e8eGUID\u7684\u4e0d\u8fde\u7eed\u9020\u6210\u7684\u3002 \u5f53\u65b0\u7684\u6570\u636e\u884c\u63d2\u5165\u65f6\uff0cGUID\u53ef\u80fd\u4f1a\u9020\u6210\u5927\u91cf\u6570\u636e\u7684\u79fb\u52a8\uff0c \u56e0\u6b64\u5f71\u54cd\u4e86\u6027\u80fd\u3002<br \/>\n\u4e3a\u4ec0\u4e48\u4f7f\u7528GUID\u4f5c\u4e3a\u4e3b\u952e\uff1f\u4e3b\u8981\u662f\u4ece\u540e\u53f0\u6765\u8bb2\uff0c \u5728\u63d2\u5165\u6570\u636e\u524d\uff0c\u53ef\u4ee5\u751f\u6210\u4e00\u4e2a\u552f\u4e00\u7684Id\uff0c\u8fd9\u4e2a\u8fd8\u662f\u6709\u5f88\u591a\u5b9e\u7528\u573a\u666f\u7684\u3002 <\/p>\n<p>\u6240\u4ee5\u9700\u8981\u5bf9\u8be5\u95ee\u9898\u8fdb\u884c\u6539\u8fdb\u3002<br \/>\n\u6211\u4eec\u5728\u9879\u76ee\u4e2d\u4f7f\u7528\u7684\u662fTimestamp Based Guid\uff0c \u6216\u8005\u7b80\u79f0\u4e3aTBGuid\u3002\u5176\u5b9e\u662f\u4e00\u4e2a\u5b57\u7b26\u4e32\uff0c\u957f\u5ea640: \u524d8\u4f4d\u662f\u57fa\u4e8eUnix Time\u7684\u76f8\u5bf9\u65f6\u95f4\u79d2\uff0c\u8f6c16\u8fdb\u5236\u540e\u7684\u6587\u672c\uff1b\u540e32\u4f4d\u662fGUID\u8f6c\u6587\u672c\u540e\u53bb\u9664\u201c-\u201d\u8fde\u63a5\u53f7\u3002\u4e5f\u5c31\u662f\u65f6\u95f4+Guid\u7684\u683c\u5f0f\u3002<br \/>\n\u5229\u7528\u65f6\u95f4\u7684\u8fde\u7eed\u6027\u52a0Guid\u7684\u552f\u4e00\u6027\uff0c \u4f7f\u5176\u5373\u5177\u6709\u8fde\u7eed\u6027\uff0c\u4e0d\u81f3\u4e8e\u5728\u65b0\u589e\u6570\u636e\u7684\u65f6\u5019\u5bf9\u6570\u636e\u8fde\u7eed\u5b58\u50a8\u4ea7\u751f\u5f71\u54cd\uff1b\u53c8\u5177\u6709\u552f\u4e00\u6027\uff0c \u6ee1\u8db3\u4e3b\u952e\u8981\u6c42\u3002 <\/p>\n<p>\u4e0b\u9762\u662fC#\u4e2d\u7684\u5b9e\u73b0\u65b9\u6cd5\uff1a<\/p>\n<pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\n\/\/\/ &lt;summary&gt;\n        \/\/\/ \u8fd4\u56de\u552f\u4e00\u768440\u4f4d\u957f\u5ea6\u7684Id (Hex(UnixTime)+Guid)\n        \/\/\/ &lt;\/summary&gt;\n        \/\/\/ &lt;returns&gt;&lt;\/returns&gt;\n        public static string NewId()\n        {\n            string result = DateTimeOffset.UtcNow.ToUnixTimeSeconds().ToString(&quot;x&quot;, CultureInfo.CurrentUICulture.NumberFormat)\n                        + Guid.NewGuid().ToString(&quot;N&quot;, CultureInfo.CurrentUICulture.NumberFormat);\n            return result;\n        }\n<\/pre>\n<\/p>\n<p>\u4e0b\u9762\u662fSQL\u4e2d\u7684\u5b9e\u73b0\u65b9\u6cd5\uff1a<\/p>\n<pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\n\/*\n * Author        : Harvey Hu\n * Created On    : 2021-04-02\n * Description   :\n        \u8fd4\u56de\u4e00\u4e2a40\u4f4d\u957f\u5ea6\u7684\u5b57\u7b26\u4e32Id: \u524d8\u4f4d\u6570\u4e3aUTC\u65f6\u95f4\u76f8\u5bf9\u503c\uff0c \u540e32\u4f4d\u4e3aGUID\n * Sample\n        call        &#x5B;dbo].&#x5B;fn_NewID40](NEWID()) ,\n        return      '6066AFB6020C76DC0AC0445F8E882E3801D0F239'\n*\/\nCREATE FUNCTION &#x5B;dbo].&#x5B;fn_NewID40] (@uid uniqueidentifier)\nRETURNS nvarchar(40)\nAS\n  BEGIN\n      -- \u6ce8\u610fUTC\u65f6\u533a\uff0c \u76ee\u524d\u662f\u4e1c8\uff0c \u6240\u4ee5\n      RETURN FORMAT(Datediff(s, '1970-1-1', Dateadd(hour, -8, Getdate())), 'X')\n             + Replace( Cast(@uid AS nvarchar(36)), '-', '')\n  END\n<\/pre>\n<\/p>\n<p>\u539f\u6587\u94fe\u63a5\uff1a<a href=\"https:\/\/www.cnblogs.com\/huwz\/p\/14982119.html\"><a href=\"https:\/\/www.cnblogs.com\/huwz\/p\/14982119.html\"><a href=\"https:\/\/www.cnblogs.com\/huwz\/p\/14982119.html\">https:\/\/www.cnblogs.com\/huwz\/p\/14982119.html<\/a><\/a><\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u6539\u8fdbGUID\u4f5c\u4e3a\u4e3b\u952e\u65f6\u7684\u95ee\u9898 \u5728\u63a5\u624b\u67d0\u4e2a\u9879\u76ee\u7684\u65f6\u5019\uff0c \u8be5\u9879\u76ee\u6570\u636e\u5e93\u4f7f\u7528\u4e86GUID\uff08UniqueIdentifier)\u4f5c\u4e3a\u5927\u90e8\u5206\u8868\u7684\u4e3b\u952e\uff0c\u540c\u65f6\u4e5f\u9ed8\u8ba4\u4f7f\u7528\u4e86\u805a\u96c6\u7d22\u5f15\u3002 \u5f53\u6570\u636e\u91cf\u589e\u52a0\u7684\u65f6\u5019\uff0c \u53d1\u73b0\u4f1a\u4ea7\u751f\u5927\u91cf\u7684\u78c1\u76d8\u788e\u7247\u3002 \u56e0\u6b64\u8fd0\u7ef4\u8fd8\u4e13\u95e8\u5199\u4e86\u4e2ajob\u4efb\u52a1\u6765\u6574\u7406\u788e\u7247\u3002 \u8fd9\u4e3b\u8981\u662f\u7531\u4e8eGUID\u7684\u4e0d\u8fde\u7eed\u9020\u6210\u7684\u3002 \u5f53\u65b0\u7684\u6570\u636e\u884c\u63d2\u5165\u65f6\uff0cGUID\u53ef\u80fd\u4f1a\u9020\u6210\u5927\u91cf\u6570\u636e\u7684\u79fb\u52a8\uff0c \u56e0\u6b64\u5f71\u54cd\u4e86\u6027\u80fd\u3002 \u4e3a\u4ec0\u4e48\u4f7f\u7528GUID\u4f5c\u4e3a\u4e3b\u952e\uff1f\u4e3b\u8981\u662f\u4ece\u540e\u53f0\u6765\u8bb2\uff0c \u5728\u63d2\u5165\u6570\u636e\u524d\uff0c\u53ef\u4ee5\u751f\u6210\u4e00\u4e2a\u552f\u4e00\u7684Id\uff0c\u8fd9\u4e2a\u8fd8\u662f\u6709\u5f88\u591a\u5b9e\u7528\u573a\u666f\u7684\u3002 \u6240\u4ee5\u9700\u8981\u5bf9\u8be5\u95ee\u9898\u8fdb\u884c\u6539\u8fdb\u3002 \u6211\u4eec\u5728\u9879\u76ee\u4e2d\u4f7f\u7528\u7684\u662fTimestamp Based Guid\uff0c \u6216\u8005\u7b80\u79f0\u4e3aTBGuid\u3002\u5176\u5b9e\u662f\u4e00\u4e2a\u5b57\u7b26\u4e32\uff0c\u957f\u5ea640: \u524d8\u4f4d\u662f\u57fa\u4e8eUnix Time\u7684\u76f8\u5bf9\u65f6\u95f4\u79d2\uff0c\u8f6c16\u8fdb\u5236\u540e\u7684\u6587\u672c\uff1b\u540e32\u4f4d\u662fGUID\u8f6c\u6587\u672c\u540e\u53bb\u9664\u201c-\u201d\u8fde\u63a5\u53f7\u3002\u4e5f\u5c31\u662f\u65f6\u95f4+Guid\u7684\u683c\u5f0f\u3002 \u5229\u7528\u65f6\u95f4\u7684\u8fde\u7eed\u6027\u52a0Guid\u7684\u552f\u4e00\u6027\uff0c \u4f7f\u5176\u5373\u5177\u6709\u8fde\u7eed\u6027\uff0c\u4e0d\u81f3\u4e8e\u5728\u65b0\u589e\u6570\u636e\u7684\u65f6\u5019\u5bf9\u6570\u636e\u8fde\u7eed\u5b58\u50a8\u4ea7\u751f\u5f71\u54cd\uff1b\u53c8\u5177\u6709\u552f\u4e00\u6027\uff0c \u6ee1\u8db3\u4e3b\u952e\u8981\u6c42\u3002 \u4e0b\u9762\u662fC#\u4e2d\u7684\u5b9e\u73b0\u65b9\u6cd5\uff1a \/\/\/ &lt;summary&gt; \/\/\/ \u8fd4\u56de\u552f\u4e00\u768440\u4f4d\u957f\u5ea6\u7684Id (Hex(UnixTime)+Guid) \/\/\/ &lt;\/summary&gt; \/\/\/ &lt;returns&gt;&lt;\/returns&gt; public static string NewId() { string result = DateTimeOffset.UtcNow.ToUnixTimeSeconds().ToString(&quot;x&quot;, CultureInfo.CurrentUICulture.NumberFormat) + Guid.NewGuid().ToString(&quot;N&quot;, CultureInfo.CurrentUICulture.NumberFormat); return result; } \u4e0b\u9762\u662fSQL\u4e2d\u7684\u5b9e\u73b0\u65b9\u6cd5\uff1a \/* * Author : Harvey Hu * [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[3],"tags":[],"class_list":["post-203","post","type-post","status-publish","format-standard","hentry","category-csharp"],"_links":{"self":[{"href":"https:\/\/diji.net\/index.php?rest_route=\/wp\/v2\/posts\/203","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/diji.net\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/diji.net\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/diji.net\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/diji.net\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=203"}],"version-history":[{"count":0,"href":"https:\/\/diji.net\/index.php?rest_route=\/wp\/v2\/posts\/203\/revisions"}],"wp:attachment":[{"href":"https:\/\/diji.net\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=203"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/diji.net\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=203"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/diji.net\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=203"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}