TE
科技回声
首页24小时热榜最新最佳问答展示工作
GitHubTwitter
首页

科技回声

基于 Next.js 构建的科技新闻平台,提供全球科技新闻和讨论内容。

GitHubTwitter

首页

首页最新最佳问答展示工作

资源链接

HackerNews API原版 HackerNewsNext.js

© 2025 科技回声. 版权所有。

Hi/Lo Algorithm

4 点作者 0x54MUR41大约 1 个月前

1 comment

theamk大约 1 个月前
What a confusing article about a trivial thing. I am surprised it has not been deleted yet.<p>Summary (because I&#x27;ve wasted my time on it, so might as well spare others pain):<p>1. Let&#x27;s say you want to get unique IDs from database _before_ inserting data. This means you now have to do 2 transactions&#x2F;database queries: first query to allocate yourself a unique ID value, and second query to insert an entry with this ID.<p>2. This is slow, so you decide to optimize: you allocate a unique ID value only every X rows (where &quot;X&quot; is a large number of 1000). So you allocate an ID, database gives you 123, and you insert records 123000, 123001, 123002, 123003... Once you get to 123999, you&#x27;ve need to ask database for another ID.<p>That&#x27;s it, that is the whole algorithm.<p>Note I don&#x27;t see it used widely - most of the time database can allocate the id number automatically during insert (sequence type or integer primary index). If this does not work, then there are plenty of UUID algorithms that can allocate record IDs without any database hits at all.
评论 #43669180 未加载