{"id":6056,"date":"2024-09-21T20:36:32","date_gmt":"2024-09-21T12:36:32","guid":{"rendered":"https:\/\/dmesg.app\/?p=6056"},"modified":"2024-09-21T20:40:29","modified_gmt":"2024-09-21T12:40:29","slug":"one-api-clickhouse","status":"publish","type":"post","link":"https:\/\/dmesg.app\/one-api-clickhouse.html","title":{"rendered":"one-api\/new-api\u6027\u80fd\u4f18\u5316\uff1a\u4f7f\u7528 ClickHouse \u4f5c\u4e3a\u65e5\u5fd7\u7cfb\u7edf"},"content":{"rendered":"<p>one-api\/new-api\u662f\u4e00\u6b3e\u5f00\u6e90\u7684OpenAI \u63a5\u53e3\u7ba1\u7406 &amp; \u5206\u53d1\u7cfb\u7edf\uff0c\u652f\u6301OpenAI\u3001Anthropic\u3001Gemini\u7b49\u591a\u79cd\u6a21\u578b\u3002<\/p>\n<p>\u8fd9\u5957\u7cfb\u7edf\uff0c\u4ece\u4f7f\u7528\u8005\u7684\u89d2\u5ea6\u6765\u770b\uff0c\u7528\u8d77\u6765\u5012\u8fd8\u597d\uff0cUI\u5f88\u7b80\u6d01\uff1b\u4ece\u5f00\u53d1\u8005\u7684\u89d2\u5ea6\u6765\u770b\uff0c\u4f5c\u8005\u633a\u4e0d\u5bb9\u6613\u7684\u8981\u7528Go\u6765\u5904\u7406\u5404\u79cd\u5947\u5947\u602a\u602a\u7684\u5e8f\u5217\u5316\u95ee\u9898\uff0c\u4e00\u65e6\u8bf7\u6c42\u6570\u91cf\u8fc7\u591a\u5e76\u53d1\u5c31\u65e0\u6cd5\u63d0\u5347\uff0c\u9664\u6b64\u4e4b\u5916\u4e5f\u6709\u5f88\u591a<a href=\"https:\/\/t.me\/mikuri520\/2836\" target=\"_blank\" rel=\"noopener\">\u5947\u5947\u602a\u602a\u7684\u5730\u65b9\u5566<\/a>\u2026\u2026<\/p>\n<hr \/>\n<p>\u6211\u7684<a href=\"https:\/\/burn.hair\/\">OpenAI\u63a5\u53e3\u8f6c\u53d1\u7ad9 \u300c\u5934\u9876\u5192\u706b\u300d<\/a> \u5c31\u662f\u5728\u8fd9\u4e2a\u57fa\u7840\u4e4b\u4e0a\u642d\u5efa\u7684\u3002\u53ea\u4e0d\u8fc7\u7ecf\u8fc7\u4e86\u6211\u7684\u5927\u91cf\u4fee\u6539\uff0c\u6bd4\u5982\u52a0\u4e2a\u6570\u636e\u770b\u677f\u8fd9\u79cd\u524d\u7aef\u7684\u529f\u80fd<\/p>\n<p><a href=\"https:\/\/dmesg.app\/wp-content\/uploads\/2024\/09\/2024092119325495.jpg\" alt=\"one-api\/new-api\u6027\u80fd\u4f18\u5316\uff1a\u4f7f\u7528 ClickHouse \u4f5c\u4e3a\u65e5\u5fd7\u7cfb\u7edf\" title=\"one-api\/new-api\u6027\u80fd\u4f18\u5316\uff1a\u4f7f\u7528 ClickHouse \u4f5c\u4e3a\u65e5\u5fd7\u7cfb\u7edf\" rel=\"box\" class=\"fancybox\"><img loading=\"lazy\" decoding=\"async\" width=\"1280\" height=\"910\" class=\"alignnone size-full wp-image-6062\" src=\"https:\/\/7bc5d24.webp.ee\/wp-content\/uploads\/2024\/09\/2024092119325495.jpg\" alt=\"one-api\/new-api\u6027\u80fd\u4f18\u5316\uff1a\u4f7f\u7528 ClickHouse \u4f5c\u4e3a\u65e5\u5fd7\u7cfb\u7edf\" title=\"one-api\/new-api\u6027\u80fd\u4f18\u5316\uff1a\u4f7f\u7528 ClickHouse \u4f5c\u4e3a\u65e5\u5fd7\u7cfb\u7edf\" alt=\"\" \/><\/a><\/p>\n<p>\u4ee5\u53ca\u9ed8\u9ed8\u7684\u770b\u4e0d\u89c1\u7684\u540e\u53f0\u4f18\u5316\u3002\u7ecf\u8fc7\u6211\u7684\u4e00\u756a\u8c03\u6559\u4e4b\u540e\uff0c\u76ee\u524d\u5728Hetzner 2C 2G\u7684\u673a\u5668\u4e0a\u5df2\u7ecf\u80fd\u8fbe\u5230\u51e0\u767e\u7684QPS\u4e86\u3002\u5f53\u7136\u5b9e\u9645\u4f7f\u7528\uff0c\u8fd8\u5f97\u770b\u771f\u6b63\u7684API\u80fd\u591f\u63d0\u4f9b\u591a\u5c11TPM\u548cRPM\u3002<\/p>\n<hr \/>\n<p>\u5728\u9ad8\u5e76\u53d1\u7684\u60c5\u51b5\u4e0b\uff0c\u672c\u6765\u6570\u636e\u5e93\u7684\u538b\u529b\u5c31\u6bd4\u8f83\u5927\uff0c\u5982\u679c\u8fd8\u5f00\u542f\u4e86\u65e5\u5fd7\u8bb0\u5f55\u529f\u80fd\uff0c\u90a3\u4e48\u6570\u636e\u5e93\u5c31\u4f1a\u88ab\u5927\u91cf\u7684\u65e5\u5fd7\u5199\u5165\u76f4\u63a5\u6491\u7206\uff1b\u53e6\u5916\uff0c\u5373\u4f7f\u5728\u975e\u9ad8\u8d1f\u8f7d\u7684\u60c5\u51b5\u4e0b\uff0c\u5728\u65e5\u5fd7\u6761\u76ee\u6bd4\u8f83\u591a\u7684\u65f6\u5019\uff0c\u67e5\u770b\u6bcf\u4e00\u9875\u65e5\u5fd7\u7684\u8bf7\u6c42\u901f\u5ea6\u4e5f\u4f1a\u5f88\u6162\u3002<\/p>\n<p>\u4f18\u5316\u5e76\u53d1\u6700\u7b80\u5355\u3001\u5e76\u4e14\u5927\u6982\u7387\u597d\u7528\u7684\u529e\u6cd5\u4e4b\u4e00\u662f\u7528\u66f4\u9ad8\u914d\u7f6e\u7684\u673a\u5668\u3002\u4f46\u662f\u4e00\u822c\u6765\u8bf4\uff0c<strong>\u63d0\u5347\u5230\u4e00\u5b9a\u914d\u7f6e\u4e4b\u540e\uff0c\u518d\u63d0\u5347\u57fa\u672c\u6ca1\u5e2e\u52a9\uff0c\u4e5f\u5c31\u662f\u9677\u5165\u8fb9\u9645\u6548\u5e94\u9012\u51cf<\/strong>\u3002\u800c\u4e14\u5bf9\u4e8e\u6211\u8fd9\u79cd\u7a77\u4eba\uff0c\u81ea\u7136\u53ea\u80fd\u9760<s>\u53d8\u5f02<\/s>\u4f18\u5316\u4e86\u3002<\/p>\n<p>\u672c\u6587\u5c06\u4ecb\u7ecd\u5982\u4f55\u4f7f\u7528 ClickHouse\u66ff\u6362\u539f\u6709\u7684\u65e5\u5fd7\u7cfb\u7edf\uff0c\u907f\u514d\u5728\u9ad8\u5e76\u53d1\u7684\u65f6\u5019\u6e05\u7a7a\u6076\u5316\u5e76\u964d\u4f4e\u6027\u80fd\u3002\u81f3\u4e8e\u5176\u4ed6\u65b9\u9762\u7684\u4f18\u5316\uff0c\u7b49\u4ee5\u540e\u518d\u8bf4\uff5e<\/p>\n<h2>one-api\u7684\u6570\u636e\u5e93<\/h2>\n<p>one-api\u9ed8\u8ba4\u652f\u6301\u4e09\u79cd\u6570\u636e\u5e93\uff0cSQLite\uff0cMySQL\u548cPostgreSQL\u3002<\/p>\n<ul>\n<li>SQLite\uff1a\u5d4c\u5165\u5f0f\u7684\u6570\u636e\u5e93\uff0c\u53ea\u6709\u4e00\u4e2a\u6587\u4ef6\u3002\u5728\u9ad8\u5e76\u53d1\u4e0b\uff0c\u5373\u4f7f\u5141\u8bb8\u591a\u7ebf\u7a0b\u8bfb\u5199\uff0c\u5176\u6027\u80fd\u4e5f\u4e0d\u4f1a\u597d\u5230\u54ea\u91cc\u53bb\u3002<\/li>\n<li>MySQL\uff1a\u77e5\u540d\u7684RDBMS<\/li>\n<li>PostgreSQL\uff1aPostgreSQL\u5e94\u8be5\u6709\u66f4\u4e30\u5bcc\u7684\u529f\u80fd\u548c\u7279\u6027\u3002<\/li>\n<\/ul>\n<p>\u5982\u679c\u4f60\u5b9e\u5728\u61d2\u5f97\u81ea\u5df1\u4f18\u5316\uff0c\u76f4\u63a5\u6362\u6210 PostgreSQL+\u9ad8\u914d\u7f6e\u7684\u673a\u5668\u5427\uff0cPostgreSQL\u7684\u6027\u80fd\u5e94\u8be5\u4f1a\u597d\u8fc7MySQL\u4e00\u4e9b\u3002<\/p>\n<h2>one-api\u7684\u65e5\u5fd7\u7cfb\u7edf\uff1a\u5199\u5165<\/h2>\n<p>One-api\u7684\u666e\u901a\u6570\u636e\u548c\u65e5\u5fd7\u9ed8\u8ba4\u60c5\u51b5\u4e0b\u5747\u8bb0\u5f55\u5728\u540c\u4e00\u4e2a\u6570\u636e\u5e93\u4e4b\u4e2d\uff0c\u5176Go Struct Model \u5982\u4e0b<\/p>\n<pre class=\"prettyprint linenums\">type Log struct {\r\n\tId               int    `json:&quot;id&quot;`\r\n\tUserId           int    `json:&quot;user_id&quot; gorm:&quot;index&quot;`\r\n\tCreatedAt        int64  `json:&quot;created_at&quot; gorm:&quot;bigint;index:idx_created_at_type&quot;`\r\n\tType             int    `json:&quot;type&quot; gorm:&quot;index:idx_created_at_type&quot;`\r\n\tContent          string `json:&quot;content&quot;`\r\n\tUsername         string `json:&quot;username&quot; gorm:&quot;index:index_username_model_name,priority:2;default:&#039;&#039;&quot;`\r\n\tTokenName        string `json:&quot;token_name&quot; gorm:&quot;index;default:&#039;&#039;&quot;`\r\n\tModelName        string `json:&quot;model_name&quot; gorm:&quot;index;index:index_username_model_name,priority:1;default:&#039;&#039;&quot;`\r\n\tQuota            int    `json:&quot;quota&quot; gorm:&quot;default:0&quot;`\r\n\tPromptTokens     int    `json:&quot;prompt_tokens&quot; gorm:&quot;default:0&quot;`\r\n\tCompletionTokens int    `json:&quot;completion_tokens&quot; gorm:&quot;default:0&quot;`\r\n\tChannelId        int    `json:&quot;channel&quot; gorm:&quot;index&quot;`\r\n}\r\n<\/pre>\n<p>\u9ed8\u8ba4\u914d\u7f6e\u4e0b\uff0c\u6bcf\u4e00\u6b21\u8bf7\u6c42\uff0c\u90fd\u4f1a\u8c03\u7528 <code>RecordConsumeLog<\/code> \u5199\u5165\u65e5\u5fd7\uff0c\u5176\u4ee3\u7801\u975e\u5e38\u7b80\u5355<\/p>\n<pre class=\"prettyprint linenums\">log := &amp;Log{\r\n\tUserId:           userId,\r\n\tUsername:         GetUsernameById(userId),\r\n\tCreatedAt:        helper.GetTimestamp(),\r\n\tType:             LogTypeConsume,\r\n\tContent:          content,\r\n\tPromptTokens:     promptTokens,\r\n\tCompletionTokens: completionTokens,\r\n\tTokenName:        tokenName,\r\n\tModelName:        modelName,\r\n\tQuota:            int(quota),\r\n\tChannelId:        channelId,\r\n}\r\nerr := LOG_DB.Create(log).Error\r\n<\/pre>\n<h3>\u9010\u6761\u5199\u5165\u65e5\u5fd7<\/h3>\n<p>\u5728\u8bf7\u6c42\u6570\u91cf\u6bd4\u8f83\u591a\u7684\u60c5\u51b5\u4e0b\uff0c\u8fd9\u76f8\u5f53\u4e8e\u662f\u4e00\u6761\u4e00\u6761\u7684\u6267\u884c insert \u8bed\u53e5\u3002\u8fd9\u79cd\u60c5\u51b5\u4e0b\u4e3b\u8981\u6709\u4ee5\u4e0b\u7f3a\u70b9\uff1a<\/p>\n<ol>\n<li>\u7f51\u7edc\u5ef6\u8fdf\uff1a\u6bcf\u6b21\u64cd\u4f5c\u90fd\u6709\u5f80\u8fd4\u7684RTT\u3001\u901a\u4fe1\u5f00\u9500<\/li>\n<li>\u4e8b\u52a1\u5f00\u9500\uff1a\u6bcf\u6b21\u64cd\u4f5c\u90fd\u8981\u5f00\u542f\u548c\u5173\u95ed\u4e8b\u52a1<\/li>\n<li>\u8d1f\u8f7d\uff1a\u9891\u7e41\u63d2\u5165\u4f1a\u5bfc\u81f4CPU\u548cIO\u8d1f\u8f7d\u589e\u52a0<\/li>\n<\/ol>\n<p>\u5373\u4f7f\u4f60\u5f88\u6709\u94b1\uff0c\u7528\u5230\u4e86\u6bcf\u4e2a\u6708\u51e0\u767e\u51e0\u5343\u7f8e\u5143\u914d\u7f6e\u548c\u4ef7\u683c\u5947\u9ad8\u7684Managed database\uff0c1\u548c2\u7684\u95ee\u9898\u8fd8\u662f\u65e0\u6cd5\u907f\u514d<\/p>\n<h3>insert_many\u4f18\u5316<\/h3>\n<p>\u4f7f\u7528 insert_many\uff0c\u79ef\u6512\u4e00\u5b9a\u7684\u6570\u636e\u4e4b\u540e\u4e00\u8d77\u63d2\u5165\uff0c\u8fd9\u662f\u4e00\u79cd\u975e\u5e38\u7b80\u5355\u7684\u4f18\u5316\u601d\u8def\u3002\u8fd9\u79cd\u529e\u6cd5\u4e5f\u6709\u4e9b\u7f3a\u70b9\uff1a<\/p>\n<ol>\n<li>\u8981\u7b49\u6570\u636e\u79ef\u6512\u5230\u4e00\u5b9a\u7a0b\u5ea6\uff0c\u4f1a\u6709\u4e00\u5b9a\u7684\u5ef6\u8fdf<\/li>\n<li>\u79ef\u6512\u6570\u636e\u5230\u65f6\u5019\u53ef\u80fd\u4f1a\u6709\u5185\u5b58\u6d88\u8017\u8fc7\u591a\u7684\u98ce\u9669<\/li>\n<li>\u53ef\u80fd\u4f1a\u6709\u66f4\u9ad8\u7684\u5931\u8d25\u6982\u7387\uff0c\u4e00\u6b21\u6027\u63d2\u5165\u6570\u636e\u8fc7\u591a\uff0c\u53ef\u80fd\u4f1a\u6709 <code>max_allowed_packet<\/code> \u4e4b\u7c7b\u7684\u95ee\u9898<\/li>\n<\/ol>\n<p>\u4f18\u70b9\u5f88\u660e\u663e\uff0c\u4e00\u6b21\u6027\u5199\u5165\u6570\u636e\u53ef\u6bd4\u4e00\u6761\u4e00\u6761\u5199\u8981\u5feb\u4e0a\u5f88\u591a\u500d\u4e86\u3002<\/p>\n<p>one-api\u81ea\u5e26\u4e86\u4e00\u4e2a\u7c7b\u4f3c\u7684\u529f\u80fd\uff0c\u73af\u5883\u53d8\u91cf<code>BATCH_UPDATE_ENABLED<\/code> \u548c <code>BATCH_UPDATE_INTERVAL<\/code> \u3002\u4e0d\u8fc7\u622a\u6b62\u5230\u6211\u5199\u672c\u6587\u7684\u65f6\u5019\uff0c\u5199\u65e5\u5fd7\u7684\u8fd9\u90e8\u5206\u529f\u80fd\u4e0d\u5305\u542b\u5728<code>batchUpdate<\/code>\u5185\u2026\u800c\u662f\u5728\u4e00\u4e2ago routine\u5185\u6267\u884c\u7684\u2026<\/p>\n<p>\u66f4\u8981\u547d\u7684\u662f\uff0c\u8fd9\u4e2a<code>batchUpdate<\/code>\u4ece\u8bbe\u8ba1\u4e0a\u6765\u770b\uff0c\u53ea\u80fd\u805a\u5408\u6570\u503c\u7c7b\u578b\u7684\u6570\u636e\ud83d\ude02<\/p>\n<pre class=\"prettyprint linenums\">func addNewRecord(type_ int, id int, value int64) {\r\n\tbatchUpdateLocks[type_].Lock()\r\n\tdefer batchUpdateLocks[type_].Unlock()\r\n\tif _, ok := batchUpdateStores[type_][id]; !ok {\r\n\t\tbatchUpdateStores[type_][id] = value\r\n\t} else {\r\n\t\tbatchUpdateStores[type_][id] += value\r\n\t}\r\n}\r\n\r\n<\/pre>\n<p>\u6539\u9020\u4e00\u4e0b\u5f53\u7136\u53ef\u884c\uff0c\u6512\u591f1000\u6761\u6216\u5230\u4e00\u5b9a\u65f6\u95f4\u95f4\u9694\u4e00\u8d77\u5199\u5165\u6570\u636e\u5e93\uff0c\u96be\u5ea6\u4e0d\u9ad8\uff0cChatGPT\u53ef\u4ee5\u5e2e\u5fd9\u3002<\/p>\n<h2>one-api\u7684\u65e5\u5fd7\u7cfb\u7edf\uff1a\u8bfb\u53d6<\/h2>\n<p>\u5047\u5982\u65e5\u5fd7\u5f88\u5c11\uff0c\u90a3\u4e48\u8bfb\u53d6\u65e5\u5fd7\u5e76\u4e0d\u4f1a\u9020\u6210\u4ec0\u4e48\u5927\u95ee\u9898\u3002<\/p>\n<p>\u4f46\u662f\u5982\u679c\u6570\u636e\u91cf\u5f88\u5927\uff0c\u5e76\u4e14\u8fd8\u7528\u6237\u70b9\u51fb\u4e86\u4e0b\u4e00\u9875\uff0c\u751a\u81f3\u662f\u6700\u540e\u4e00\u9875\u8fdb\u884c\u4e86\u5206\u9875\u64cd\u4f5c\uff0c\u90a3\u4e48 <code>OFFSET<\/code>\u548c<code>LIMIT<\/code> \u4f1a\u663e\u8457\u5f71\u54cd\u6570\u636e\u5e93\u7684\u6027\u80fd\u3002<\/p>\n<p>MySQL \u5728\u6267\u884c <code>OFFSET<\/code> \u65f6\uff0c\u5fc5\u987b\u4ece\u5934\u5f00\u59cb\u626b\u63cf\u5e76\u4e22\u5f03\u524d <code>OFFSET<\/code> \u6761\u8bb0\u5f55\uff0c\u7136\u540e\u8fd4\u56de <code>LIMIT<\/code> \u6761\u8bb0\u5f55\uff0c\u5bfc\u81f4\u8f83\u5927\u7684\u65f6\u95f4\u5f00\u9500\uff0c\u5c24\u5176\u662f <code>OFFSET<\/code> \u6570\u503c\u8f83\u5927\u65f6\u3002<\/p>\n<p>\u4f18\u5316\u65b9\u6cd5\u5e94\u8be5\u662f\u6709\u4e00\u4e9b\u7684\uff0c\u6bd4\u5982\u901a\u8fc7\u4e3b\u952e\u800c\u4e0d\u662f<code>OFFSET<\/code> \uff0c\u5177\u4f53\u53ef\u4ee5\u95ee\u95eeChatGPT\uff0c\u6211\u4e0d\u662f\u5f88\u613f\u610f\u8fdb\u884c\u8fd9\u4e2a\u65b9\u5411\u7684\u7814\u7a76\ud83d\ude02<\/p>\n<h2>ClickHouse\u4ecb\u7ecd<\/h2>\n<p>ClickHouse\u662fyandex\u5f00\u53d1\u7684\u4e00\u4e2a\u5f00\u6e90\u5217\u5f0f\u6570\u636e\u5e93\u7ba1\u7406\u7cfb\u7edf\uff0c\u4e13\u95e8\u4e3a\u9ad8\u6027\u80fd\u7684\u5206\u6790\u67e5\u8be2\u800c\u8bbe\u8ba1\uff0c\u5728\u5904\u7406\u5927\u91cf\u6570\u636e\u65f6\u6709\u51fa\u8272\u7684\u8868\u73b0\u3002<\/p>\n<p>ClickHouse\u91c7\u7528\u5217\u5f0f\u5b58\u50a8\uff0c\u4e0d\u50cfMySQL\u7b49\u662f\u884c\u5f0f\u5b58\u50a8\uff0c\u7279\u522b\u9002\u5408\u5b58\u50a8\u65e5\u5fd7\u3002\u5728 WebP Cloud Services \u4e2d\u6211\u4eec\u7684\u7edf\u8ba1\u4fe1\u606f\u6765\u6e90\u4fbf\u662f ClickHouse<\/p>\n<p><a href=\"https:\/\/dmesg.app\/wp-content\/uploads\/2024\/09\/202409212031366.png\" alt=\"one-api\/new-api\u6027\u80fd\u4f18\u5316\uff1a\u4f7f\u7528 ClickHouse \u4f5c\u4e3a\u65e5\u5fd7\u7cfb\u7edf\" title=\"one-api\/new-api\u6027\u80fd\u4f18\u5316\uff1a\u4f7f\u7528 ClickHouse \u4f5c\u4e3a\u65e5\u5fd7\u7cfb\u7edf\" rel=\"box\" class=\"fancybox\"><img loading=\"lazy\" decoding=\"async\" width=\"1308\" height=\"1118\" class=\"alignnone size-full wp-image-6089\" src=\"https:\/\/7bc5d24.webp.ee\/wp-content\/uploads\/2024\/09\/202409212031366.png\" alt=\"one-api\/new-api\u6027\u80fd\u4f18\u5316\uff1a\u4f7f\u7528 ClickHouse \u4f5c\u4e3a\u65e5\u5fd7\u7cfb\u7edf\" title=\"one-api\/new-api\u6027\u80fd\u4f18\u5316\uff1a\u4f7f\u7528 ClickHouse \u4f5c\u4e3a\u65e5\u5fd7\u7cfb\u7edf\" alt=\"\" \/><\/a><\/p>\n<p>\u66f4\u91cd\u8981\u7684\u662f\uff0cClickHouse\u7684\u67e5\u8be2\u8bed\u53e5\u548cMySQL\u3001PostgreSQL\u7b49\u51e0\u4e4e\u6ca1\u4ec0\u4e48\u5dee\u522b\uff0c\u57fa\u672c\u4e0a\u6539\u6539\u5c31\u80fd\u7528\uff0c\u5b66\u4e60\u66f2\u7ebf\u5f88\u5e73\u7f13\u3002<\/p>\n<h2>ClickHouse\u521b\u5efa\u6570\u636e\u5e93\u8868<\/h2>\n<p>\u6839\u636e\u4e0a\u9762\u7684Go Struct\u548c\u5df2\u6709\u7684\u6570\u636e\u7ed3\u6784\uff0c\u6539\u6539\u57fa\u672c\u4e0a\u5c31\u53ef\u4ee5\u4e86\u3002\u4e3a\u4e86\u4fdd\u6301\u4e00\u81f4\uff0c<code>created_at<\/code> \u5c31\u7528\u6570\u5b57\u65f6\u95f4\u6233\u4e86\uff0c\u867d\u7136\u4f7f\u7528 <code>datetime<\/code>\u662f\u66f4\u597d\u7684\u529e\u6cd5\u3002<\/p>\n<pre class=\"prettyprint linenums\">CREATE TABLE logs\r\n(\r\n    user_id           Nullable(Int64),\r\n    created_at        Int64  DEFAULT toUnixTimestamp(now()),\r\n    type              Nullable(Int64),\r\n    content           Nullable(String),\r\n    username          String DEFAULT &#039;&#039;,\r\n    token_name        String DEFAULT &#039;&#039;,\r\n    model_name        String DEFAULT &#039;&#039;,\r\n    quota             Int64  DEFAULT 0,\r\n    prompt_tokens     Int64  DEFAULT 0,\r\n    completion_tokens Int64  DEFAULT 0,\r\n    ip                Nullable(String),\r\n    user_agent        Nullable(String)\r\n) ENGINE = MergeTree()\r\n      ORDER BY (created_at, username, token_name, model_name);\r\n<\/pre>\n<h2>\u8fde\u63a5\u5230ClickHouse<\/h2>\n<p>\u4f7f\u7528\u5b98\u65b9\u7684 github.com\/ClickHouse\/clickhouse-go<\/p>\n<pre class=\"prettyprint linenums\">var ch, err = clickhouse.Open(&amp;clickhouse.Options{\r\n\tAddr: []string{&quot;clickhouse:9000&quot;},\r\n\tAuth: clickhouse.Auth{\r\n\t\tDatabase: &quot;openai&quot;,\r\n\t\tUsername: &quot;default&quot;,\r\n\t\tPassword: &quot;&quot;,\r\n\t},\r\n})\r\n<\/pre>\n<h2>\u5199\u5165\u6570\u636e<\/h2>\n<pre class=\"prettyprint linenums\">query := `INSERT INTO logs ( user_id, created_at, type, content, username, token_name, model_name,quota, prompt_tokens, completion_tokens, ip, user_agent)`\r\nch.Exec(ctx, query, 1, timestamp, ...)\r\n<\/pre>\n<h2>\u6279\u91cf\u5199\u5165\u6570\u636e<\/h2>\n<p>Clickhouse\u6700\u5389\u5bb3\u7684\u5730\u65b9\u4e4b\u4e00\u662f\u53ef\u4ee5\u6279\u91cf\u5199\u5165\u6570\u636e\uff0c\u5b98\u7f51\u4e0a<a href=\"https:\/\/clickhouse.com\/docs\/en\/cloud\/bestpractices\/bulk-inserts\">\u662f\u8fd9\u4e48\u8bf4\u7684<\/a>\uff1a<\/p>\n<blockquote><p>Generally, we recommend inserting data in fairly large batches of at least 1,000 rows at a time, and ideally between 10,000 to 100,000 rows.<\/p><\/blockquote>\n<p>\u4f60\u770b\u4ed6\u4eec\u591a\u81ea\u4fe1\uff0c\u8ba9\u4f60\u6512\u591f\u4e86\u518d\u5199\uff0c\u4e00\u6b21\u5199\u51e0\u4e07\u6ca1\u95ee\u9898<\/p>\n<h3>\u521b\u5efabatch<\/h3>\n<p>\u5728Go\u91cc\u6279\u91cf\u5199\u5165\uff0c\u8981\u5148\u521b\u5efabatch<\/p>\n<pre class=\"prettyprint linenums\">var batch, err = ch.PrepareBatch(ctx, `INSERT INTO logs ( user_id, created_at, type, content, username, token_name, model_name,\r\n\tquota, prompt_tokens, completion_tokens, ip, user_agent)\r\nVALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)`)\r\n<\/pre>\n<h3>\u8ffd\u52a0\u6570\u636e<\/h3>\n<p>\u7136\u540e\u5f80\u8fd9\u4e2a batch\u91cc\u75af\u72c2<code>append<\/code>\u6570\u636e\uff0c\u4e5f\u5c31\u662f\u5728\u539f\u6765\u7684<code>RecordConsumeLog<\/code> \u4e2d\u8ffd\u52a0\u6570\u636e\u5230\u8fd9\u4e2abatch<\/p>\n<pre class=\"prettyprint linenums\">batch.Append(userId, helper.GetTimestamp(),\r\n\tLogTypeConsume, content, username, tokenName, modelName, quota, promptTokens, completionTokens, ip, ua,\r\n)\r\n<\/pre>\n<h3>\u63d0\u4ea4<\/h3>\n<p>\u65f6\u95f4\u5dee\u4e0d\u591a\u4e86\uff0c\u6216\u8005\u6570\u636e\u8db3\u591f\u4e86\uff0c\u63d0\u4ea4<\/p>\n<pre class=\"prettyprint linenums\">batch.Send()\r\n<\/pre>\n<h3>\u5b9a\u65f6\u5668<\/h3>\n<p>\u5b9a\u65f6\u5668\u53ef\u4ee5\u7528<code>ticker<\/code><\/p>\n<pre class=\"prettyprint linenums\">func ClickHouseTicker() {\r\n\tvar ticker = time.NewTicker(time.Second * 60)\r\n\tfor {\r\n\t\tselect {\r\n\t\tcase &lt;-ticker.C:\r\n\t\t\tFlushLog()\r\n\t\t}\r\n\t}\r\n}\r\n<\/pre>\n<p>\u5728 main\u91cc <code>go ClickHouseTicker()<\/code>\u5373\u53ef<\/p>\n<h3>\u6e05\u7a7abatch<\/h3>\n<p>\u5199\u5b8c\u6570\u636e\u4e4b\u540e\uff0c\u7136\u540e\u6e05\u7a7abatch\uff0c\u4e0d\u80fd\u8d8a\u5199\u8d8a\u591a<\/p>\n<pre class=\"prettyprint linenums\">batch=createBatch()\r\n<\/pre>\n<h3>\u7a0b\u5e8f\u9000\u51fa\u6e05\u7406\u5de5\u4f5c<\/h3>\n<p>\u6700\u540e\uff0c\u8981\u6355\u83b7 SIGINT\u548cSIGTERM\u4fe1\u53f7\uff0c\u4e0d\u80fd\u91cd\u542f\u7a0b\u5e8f\u7684\u4e22\u65e5\u5fd7\u554a\uff0c\u8fd9\u4e2a\u65f6\u5019\u5c31\u8981\u7528\u5230IIFE+goroutine\u4e86<\/p>\n<pre class=\"prettyprint linenums\">sigChan := make(chan os.Signal, 1)\r\nsignal.Notify(sigChan, syscall.SIGTERM, syscall.SIGINT)\r\ngo func() {\r\n\tfor {\r\n\t\tsig := &lt;-sigChan\r\n\t\tswitch sig {\r\n\t\tcase syscall.SIGINT, syscall.SIGTERM:\r\n\t\t\tlogger.SysLog(&quot;Cleaning up...&quot;)\r\n\t\t\tmodel.FlushLog()\r\n\t\t\tmodel.ConsolidateConsumeQuota()\r\n\t\t\ttime.Sleep(time.Second * 2)\r\n\t\t\tsyscall.Exit(0)\r\n\t\t}\r\n\t}\r\n}()\r\n<\/pre>\n<h2>\u8bfb\u6570\u636e<\/h2>\n<p>\u67e5\u8be2\u8bed\u6cd5\u57fa\u672c\u548cMySQL\u4e00\u6837\uff0c<code>ch.Query(ctx, query, params\u2026)<\/code>\u5c31\u884c\uff0corder by\uff0coffset\uff0climit\uff0ccount\u7b49\u90fd\u6709<\/p>\n<h2>\u5220\u9664\u65e5\u5fd7<\/h2>\n<p>\u65e5\u5fd7\u603b\u5f97\u5220\u6389\uff0c\u8981\u4e0d\u7136\u78c1\u76d8\u65e9\u665a\u4f1a\u88ab\u6491\u7206\u3002\u7528\u7c7b\u4f3c\u5982\u4e0b\u8bed\u53e5\u5c31\u884c\uff0c\u6700\u540e\u7684 <code>max_execution_time<\/code> \u53ef\u9009\uff0c\u907f\u514d\u65e5\u5fd7\u771f\u7684\u592a\u591a\u5220\u9664\u4e5f\u5f88\u8d39\u65f6\u95f4<\/p>\n<pre class=\"prettyprint linenums\">DELETE FROM logs WHERE type = 2 AND created_at &lt; timestamp SETTINGS max_execution_time=3600\r\n<\/pre>\n<h2>\u6570\u636e\u5206\u6790<\/h2>\n<p>\u901a\u8fc7 Grafana + Clickhouse\u4f5c\u4e3a\u6570\u636e\u6e90\uff0c\u53ef\u4ee5\u76f4\u63a5\u753b\u56fe\u8868\u3002\u4e4b\u524d QPS \u7684\u6570\u636e\u662f\u5199\u5165\u5230 influxdb\u7684\uff0c\u4e00\u65e6\u8d85\u8fc77\u5929\u67e5\u8be2\u5c31\u5f88\u6162\uff0c\u73b0\u5728\u6211\u5355\u72ec\u628a\u672c\u5e94\u5199\u5230 influxdb\u7684\u6570\u636e\u5199\u5230\u4e86\u4e00\u4e2a\u540d\u4e3a stats \u7684ClickHouse\u8868\uff0c\u53ea\u4fdd\u5b58\u5173\u952e\u7684\u4fe1\u606f\uff0c\u5982\u8bf7\u6c42\u65f6\u95f4\uff0c\u6a21\u578b\u540d\u79f0\u7b49\u3002<\/p>\n<p>\u6bd4\u5982\u770b\u770b\u5927\u5bb6\u7528\u5f97\u6700\u591a\u7684\u6a21\u578b\uff0c\u6700\u591a\u7684\u7adf\u7136\u662fmini\uff0c\u7136\u540e\u662f4o\uff1b\u4f46\u662f\u6d88\u8d39\u662f\u53cd\u8fc7\u6765\u7684\uff0c\u56e0\u4e3a4o\u7684\u4ef7\u683c\u6bd4\u8f83\u8d35\uff0cmini\u6bd4\u8f83\u4fbf\u5b9c<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/7bc5d24.webp.ee\/wp-content\/uploads\/2024\/09\/2024092119153889.png\" alt=\"one-api\/new-api\u6027\u80fd\u4f18\u5316\uff1a\u4f7f\u7528 ClickHouse \u4f5c\u4e3a\u65e5\u5fd7\u7cfb\u7edf\" title=\"one-api\/new-api\u6027\u80fd\u4f18\u5316\uff1a\u4f7f\u7528 ClickHouse \u4f5c\u4e3a\u65e5\u5fd7\u7cfb\u7edf\" alt=\"\u56fe\u5f62\u7528\u6237\u754c\u9762, \u997c\u56fe \u4e2d\u5ea6\u53ef\u4fe1\u5ea6\u63cf\u8ff0\u5df2\u81ea\u52a8\u751f\u6210\" \/><\/p>\n<h2>\u67e5\u8be2\u6027\u80fd\u80fd\u63d0\u5347<\/h2>\n<p>\u7b80\u5355\u7684\u6267\u884c\u4e09\u4e2a\u67e5\u8be2\uff0c\u4e00\u5171\u6709354\u4e07\u7684\u6570\u636e\u3002\u4e3b\u8981\u6d4b\u4e00\u4e0b\u5927\u91cf\u7ffb\u9875\uff0c\u53ef\u4ee5\u770b\u51fa\u5373\u4f7f\u6700\u5dee\u7684<code>select * <\/code>\u7684\u60c5\u51b5\u4e0b\uff0cClickHouse\u5927\u6982\u4e5f\u67094-5\u500d\u7684\u6027\u80fd\u63d0\u5347\u3002<\/p>\n<table>\n<tbody>\n<tr>\n<td>\u67e5\u8be2<\/td>\n<td>MySQL<\/td>\n<td>ClickHouse<\/td>\n<td>\u6bd4\u7387<\/td>\n<\/tr>\n<tr>\n<td>select count(*) from openai.logs;<\/td>\n<td>2.73<\/td>\n<td>0.002<\/td>\n<td>1365.00<\/td>\n<\/tr>\n<tr>\n<td>select user_id from openai.logs limit 3 offset 3000000;<\/td>\n<td>4.19<\/td>\n<td>0.022<\/td>\n<td>190.45<\/td>\n<\/tr>\n<tr>\n<td>select * from openai.logs limit 3 offset 3000000;<\/td>\n<td>12.16<\/td>\n<td>2.778<\/td>\n<td>4.38<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<pre class=\"prettyprint linenums\">mysql&gt; select count(*) from openai.logs;\r\n+----------+\r\n| count(*) |\r\n+----------+\r\n|  3540771 |\r\n+----------+\r\n1 row in set (2.73 sec)\r\n\r\nmysql&gt; select user_id from openai.logs limit 3 offset 3000000;\r\n....\r\n3 rows in set (4.19 sec)\r\n\r\n\r\nmysql&gt; select * from openai.logs limit 3 offset 3000000;\r\n....                                                                                                                                                                                                                                                   |\r\n3 rows in set (12.16 sec)\r\n\r\n\r\n\r\nc387e15ec5c6 :) select count(*) from openai.logs;\r\n\r\nSELECT count(*)\r\nFROM openai.logs\r\n\r\nQuery id: 9382c843-927d-471f-8f6c-85a049d58294\r\n\r\n   \u250c\u2500count()\u2500\u2510\r\n1. \u2502 3540771 \u2502 -- 3.54 million\r\n   \u2514\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2518\r\n\r\n1 row in set. Elapsed: 0.002 sec.\r\n\r\nc387e15ec5c6 :) select user_id from openai.logs limit 3 offset 3000000;\r\n\r\nSELECT user_id\r\nFROM openai.logs\r\nLIMIT 3000000, 3\r\n\r\nQuery id: 66d0ea52-6b30-4a73-b313-fc45bdcbaaf2\r\n....\r\n\r\n3 rows in set. Elapsed: 0.022 sec. Processed 3.29 million rows, 29.64 MB (146.90 million rows\/s., 1.32 GB\/s.)\r\nPeak memory usage: 4.26 MiB.\r\n\r\nc387e15ec5c6 :) select * from openai.logs limit 3 offset 3000000;\r\n\r\nSELECT *\r\nFROM openai.logs\r\nLIMIT 3000000, 3\r\n\r\nQuery id: 0faf62f4-5203-434a-8b3d-d1bcf032a8d0\r\n....\r\n3 rows in set. Elapsed: 2.778 sec. Processed 3.01 million rows, 9.26 GB (1.08 million rows\/s., 3.33 GB\/s.)\r\nPeak memory usage: 258.79 MiB.\r\n<\/pre>\n<h2>\u6700\u540e<\/h2>\n<p><span style=\"font-size: 12pt;\">\u90a3\u5f53\u7136\u662f\u8981\u81ea\u6211\u5ba3\u4f20\u4e00\u4e0b\u5566\uff01<\/span><\/p>\n<p><span style=\"font-size: 12pt;\">\ud83c\udf1f\ud83c\udf1f\ud83c\udf1f\u6b22\u8fce\u6ce8\u518c\u5e76\u4f7f\u7528<a href=\"https:\/\/burn.hair\/?lang=zh-CN&amp;utm_source=dmesg\" target=\"_blank\" rel=\"noopener\"> \u5934\u9876\u5192\u706b OpenAI \u63a5\u53e3\u8f6c\u53d1\u7ad9<\/a>\uff0c\u7a33\u5b9a\u3001\u5feb\u901f\u3001\u9ad8\u5e76\u53d1\uff0c\u6ce8\u518c\u5373\u8d60\u9001 $0.5 \u6d4b\u8bd5\u989d\u5ea6 \ud83c\udf1f\ud83c\udf1f\ud83c\udf1f<\/span><\/p>\n","protected":false},"excerpt":{"rendered":"<p>one-api\/new-api\u662f\u4e00\u6b3e\u5f00\u6e90\u7684OpenAI \u63a5\u53e3\u7ba1\u7406 &amp; \u5206\u53d1\u7cfb\u7edf\uff0c\u652f\u6301OpenAI\u3001An [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[38],"tags":[],"class_list":["post-6056","post","type-post","status-publish","format-standard","hentry","category-program"],"amp_enabled":true,"_links":{"self":[{"href":"https:\/\/dmesg.app\/wp-json\/wp\/v2\/posts\/6056","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/dmesg.app\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/dmesg.app\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/dmesg.app\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/dmesg.app\/wp-json\/wp\/v2\/comments?post=6056"}],"version-history":[{"count":38,"href":"https:\/\/dmesg.app\/wp-json\/wp\/v2\/posts\/6056\/revisions"}],"predecessor-version":[{"id":6098,"href":"https:\/\/dmesg.app\/wp-json\/wp\/v2\/posts\/6056\/revisions\/6098"}],"wp:attachment":[{"href":"https:\/\/dmesg.app\/wp-json\/wp\/v2\/media?parent=6056"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/dmesg.app\/wp-json\/wp\/v2\/categories?post=6056"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/dmesg.app\/wp-json\/wp\/v2\/tags?post=6056"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}