{"id":201642,"date":"2025-05-10T09:35:49","date_gmt":"2025-05-10T01:35:49","guid":{"rendered":"https:\/\/server.hk\/cnblog\/201642\/"},"modified":"2025-05-10T09:35:49","modified_gmt":"2025-05-10T01:35:49","slug":"redis%e5%a6%82%e4%bd%95%e5%88%86%e6%9e%90%e6%85%a2%e6%9f%a5%e8%af%a2%e6%93%8d%e4%bd%9c%ef%bc%9f","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/201642\/","title":{"rendered":"Redis\u5982\u4f55\u5206\u6790\u6162\u67e5\u8be2\u64cd\u4f5c\uff1f"},"content":{"rendered":"<p><b><\/b> <\/p>\n<h1>Redis\u5982\u4f55\u5206\u6790\u6162\u67e5\u8be2\u64cd\u4f5c\uff1f<\/h1>\n<p><span style=\"cursor: pointer\"><i><\/i>\u6536\u85cf<\/span> <\/p>\n<p>\u77e5\u8bc6\u70b9\u638c\u63e1\u4e86\uff0c\u8fd8\u9700\u8981\u4e0d\u65ad\u7ec3\u4e60\u624d\u80fd\u719f\u7ec3\u8fd0\u7528\u3002\u4e0b\u9762golang\u5b66\u4e60\u7f51\u7ed9\u5927\u5bb6\u5e26\u6765\u4e00\u4e2a\u6570\u636e\u5e93\u5f00\u53d1\u5b9e\u6218\uff0c\u624b\u628a\u624b\u6559\u5927\u5bb6\u5b66\u4e60\u300aRedis\u5982\u4f55\u5206\u6790\u6162\u67e5\u8be2\u64cd\u4f5c\uff1f\u300b\uff0c\u5728\u5b9e\u73b0\u529f\u80fd\u7684\u8fc7\u7a0b\u4e2d\u4e5f\u5e26\u5927\u5bb6\u91cd\u65b0\u6e29\u4e60\u76f8\u5173\u77e5\u8bc6\u70b9\uff0c\u6e29\u6545\u800c\u77e5\u65b0\uff0c\u56de\u5934\u770b\u770b\u8bf4\u4e0d\u5b9a\u53c8\u6709\u4e0d\u4e00\u6837\u7684\u611f\u609f\uff01<\/p>\n<h3><\/h3>\n<h3><strong>\u4ec0\u4e48\u662f\u6162\u67e5\u8be2<\/strong><\/h3>\n<p>\u548cmysql\u7684\u6162SQL\u65e5\u5fd7\u5206\u6790\u4e00\u6837\uff0credis\u4e5f\u6709\u7c7b\u4f3c\u7684\u529f\u80fd\uff0c\u6765\u5e2e\u52a9\u5b9a\u4f4d\u4e00\u4e9b\u6162\u67e5\u8be2\u64cd\u4f5c\u3002<\/p>\n<p>Redis slowlog\u662fRedis\u7528\u6765\u8bb0\u5f55\u67e5\u8be2\u6267\u884c\u65f6\u95f4\u7684\u65e5\u5fd7\u7cfb\u7edf\u3002<\/p>\n<p>\u67e5\u8be2\u6267\u884c\u65f6\u95f4\u6307\u7684\u662f\u4e0d\u5305\u62ec\u50cf\u5ba2\u6237\u7aef\u54cd\u5e94(talking)\u3001\u53d1\u9001\u56de\u590d\u7b49IO\u64cd\u4f5c\uff0c\u800c\u5355\u5355\u662f\u6267\u884c\u4e00\u4e2a\u67e5\u8be2\u547d\u4ee4\u6240\u8017\u8d39\u7684\u65f6\u95f4\u3002<\/p>\n<p>\u53e6\u5916\uff0cslow log\u4fdd\u5b58\u5728\u5185\u5b58\u91cc\u9762\uff0c\u8bfb\u5199\u901f\u5ea6\u975e\u5e38\u5feb\uff0c\u56e0\u6b64\u4f60\u53ef\u4ee5\u653e\u5fc3\u5730\u4f7f\u7528\u5b83\uff0c\u4e0d\u5fc5\u62c5\u5fc3\u56e0\u4e3a\u5f00\u542fslow log\u800c\u635f\u5bb3Redis\u7684\u901f\u5ea6\u3002<\/p>\n<h3>\u6162\u67e5\u8be2\u53c2\u6570<\/h3>\n<p>\u9996\u5148\u6765\u5173\u6ce8\u4e0b\u6162\u65e5\u5fd7\u5206\u6790\u5bf9\u5e94\u7684\u4e24\u4e2a\u53c2\u6570\uff1a<\/p>\n<p>1\u3001slowlog-log-slower-than\uff1a\u9884\u8bbe\u9600\u503c\uff0c\u5373\u8bb0\u5f55\u8d85\u8fc7\u591a\u5c11\u65f6\u95f4\u7684\u8bb0\u5f55\uff0c\u9ed8\u8ba4\u4e3a10000\u5fae\u79d2\uff0c\u537310\u6beb\u79d2\u3002<\/p>\n<p>2\u3001slowlog-max-len\uff1a\u8bb0\u5f55\u6162\u67e5\u8be2\u7684\u6761\u6570\uff0c\u9ed8\u8ba4\u4e3a128\u6761\uff0c\u5f53\u8d85\u8fc7\u8bbe\u7f6e\u7684\u6761\u6570\u65f6\u6700\u65e9\u8fdb\u5165\u961f\u5217\u7684\u5c06\u88ab\u79fb\u9664\u3002\u7ebf\u4e0a\u5efa\u8bae\u589e\u5927\u6570\u503c\uff0c\u5982\uff1a1000\uff0c\u8fd9\u6837\u53ef\u51cf\u5c11\u961f\u5217\u79fb\u9664\u7684\u9891\u7387\u3002&nbsp;<\/p>\n<\/p>\n<pre>127.0.0.1:6379&gt;&nbsp;config&nbsp;get&nbsp;slowlog-log-slower-than&nbsp;&nbsp;\r\n1)&nbsp;\"slowlog-log-slower-than\"&nbsp;&nbsp;\r\n2)&nbsp;\"10000\"&nbsp;&nbsp;\r\n127.0.0.1:6379&gt;&nbsp;config&nbsp;get&nbsp;slowlog-max-len&nbsp;&nbsp;\r\n1)&nbsp;\"slowlog-max-len\"&nbsp;&nbsp;\r\n2)&nbsp;\"128\"&nbsp;&nbsp;\r\n<\/pre>\n<p>\u53ef\u4ee5\u7528config set\u5bf9\u8fd9\u4e24\u4e2a\u53c2\u6570\u8fdb\u884c\u8c03\u6574\uff0c\u6216\u8005\u5728\u914d\u7f6e\u6587\u4ef6\u4e2d\u8bbe\u7f6e\u3002&nbsp;<\/p>\n<\/p>\n<pre>##################################&nbsp;SLOW&nbsp;LOG&nbsp;###################################&nbsp;&nbsp;\r\n#&nbsp;The&nbsp;Redis&nbsp;Slow&nbsp;Log&nbsp;is&nbsp;a&nbsp;system&nbsp;to&nbsp;log&nbsp;queries&nbsp;that&nbsp;exceeded&nbsp;a&nbsp;specified&nbsp;&nbsp;\r\n#&nbsp;execution&nbsp;time.&nbsp;The&nbsp;execution&nbsp;time&nbsp;does&nbsp;not&nbsp;include&nbsp;the&nbsp;I\/O&nbsp;operations&nbsp;&nbsp;\r\n#&nbsp;like&nbsp;talking&nbsp;with&nbsp;the&nbsp;client,&nbsp;sending&nbsp;the&nbsp;reply&nbsp;and&nbsp;so&nbsp;forth,&nbsp;&nbsp;\r\n#&nbsp;but&nbsp;just&nbsp;the&nbsp;time&nbsp;needed&nbsp;to&nbsp;actually&nbsp;execute&nbsp;the&nbsp;command&nbsp;(this&nbsp;is&nbsp;the&nbsp;only&nbsp;&nbsp;\r\n#&nbsp;stage&nbsp;of&nbsp;command&nbsp;execution&nbsp;where&nbsp;the&nbsp;thread&nbsp;is&nbsp;blocked&nbsp;and&nbsp;can&nbsp;not&nbsp;serve&nbsp;&nbsp;\r\n#&nbsp;other&nbsp;requests&nbsp;in&nbsp;the&nbsp;meantime).&nbsp;\r\n#&nbsp;You&nbsp;can&nbsp;configure&nbsp;the&nbsp;slow&nbsp;log&nbsp;with&nbsp;two&nbsp;parameters:&nbsp;one&nbsp;tells&nbsp;Redis&nbsp;&nbsp;\r\n#&nbsp;what&nbsp;is&nbsp;the&nbsp;execution&nbsp;time,&nbsp;in&nbsp;microseconds,&nbsp;to&nbsp;exceed&nbsp;in&nbsp;order&nbsp;for&nbsp;the&nbsp;&nbsp;\r\n#&nbsp;command&nbsp;to&nbsp;get&nbsp;logged,&nbsp;and&nbsp;the&nbsp;other&nbsp;parameter&nbsp;is&nbsp;the&nbsp;length&nbsp;of&nbsp;the&nbsp;&nbsp;\r\n#&nbsp;slow&nbsp;log.&nbsp;When&nbsp;a&nbsp;new&nbsp;command&nbsp;is&nbsp;logged&nbsp;the&nbsp;oldest&nbsp;one&nbsp;is&nbsp;removed&nbsp;from&nbsp;the&nbsp;&nbsp;\r\n#&nbsp;queue&nbsp;of&nbsp;logged&nbsp;commands.&nbsp;&nbsp;&nbsp;\r\n#&nbsp;The&nbsp;following&nbsp;time&nbsp;is&nbsp;expressed&nbsp;in&nbsp;microseconds,&nbsp;so&nbsp;1000000&nbsp;is&nbsp;equivalent&nbsp;&nbsp;\r\n#&nbsp;to&nbsp;one&nbsp;second.&nbsp;Note&nbsp;that&nbsp;a&nbsp;negative&nbsp;number&nbsp;disables&nbsp;the&nbsp;slow&nbsp;log,&nbsp;while&nbsp;&nbsp;\r\n#&nbsp;a&nbsp;value&nbsp;of&nbsp;zero&nbsp;forces&nbsp;the&nbsp;logging&nbsp;of&nbsp;every&nbsp;command.&nbsp;&nbsp;\r\nslowlog-log-slower-than&nbsp;10000&nbsp;&nbsp;&nbsp;\r\n#&nbsp;There&nbsp;is&nbsp;no&nbsp;limit&nbsp;to&nbsp;this&nbsp;length.&nbsp;Just&nbsp;be&nbsp;aware&nbsp;that&nbsp;it&nbsp;will&nbsp;consume&nbsp;memory.&nbsp;&nbsp;\r\n#&nbsp;You&nbsp;can&nbsp;reclaim&nbsp;memory&nbsp;used&nbsp;by&nbsp;the&nbsp;slow&nbsp;log&nbsp;with&nbsp;SLOWLOG&nbsp;RESET.&nbsp;&nbsp;\r\nslowlog-max-len&nbsp;128&nbsp;\r\n<\/pre>\n<h3>\u6162\u67e5\u8be2\u547d\u4ee4<\/h3>\n<p>\u8bed\u6cd5\uff1aslowlog subcommand [argument]<\/p>\n<p>\u5982\uff0c\u8fdb\u884c\u67e5\u8be2\u6162\u67e5\u8be2\u3001\u83b7\u53d6\u6162\u67e5\u8be2\u8bb0\u5f55\u7684\u6570\u91cf\u3001\u91cd\u7f6e\u6162\u67e5\u8be2\u65e5\u5fd7\u7b49\u64cd\u4f5c\uff1a<\/p>\n<\/p>\n<pre>192.168.10.38:9001&gt;&nbsp;slowlog&nbsp;get&nbsp;&nbsp;\r\n(empty&nbsp;list&nbsp;or&nbsp;set)&nbsp;&nbsp;\r\n192.168.10.38:9001&gt;&nbsp;slowlog&nbsp;get&nbsp;10&nbsp;&nbsp;\r\n(empty&nbsp;list&nbsp;or&nbsp;set)&nbsp;&nbsp;\r\n192.168.10.38:9001&gt;&nbsp;slowlog&nbsp;len&nbsp;&nbsp;&nbsp;\r\n(integer)&nbsp;0&nbsp;&nbsp;\r\n192.168.10.38:9001&gt;&nbsp;slowlog&nbsp;reset&nbsp;&nbsp;\r\nOK&nbsp;&nbsp;&nbsp;\r\n<\/pre>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Redis\u5982\u4f55\u5206\u6790\u6162\u67e5\u8be2\u64cd\u4f5c\uff1f &#46;&#46;&#46;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[101],"tags":[],"class_list":["post-201642","post","type-post","status-publish","format-standard","hentry","category-database"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/201642","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/comments?post=201642"}],"version-history":[{"count":0,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/201642\/revisions"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=201642"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=201642"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=201642"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}