同步桌面应用程序的数据库设计
我正在构建一个可以在多台笔记本电脑上运行的桌面应用程序。每当用户回到办公室并再次访问时,它都需要同步到中央数据库。
我要克服的最大问题是如何设计数据库以便它可以轻松地与中央数据库服务器同步。主要障碍之一是试图确定如何处理密钥,以便它们不会在将使用的多个笔记本电脑数据库中重复。
例如,假设笔记本电脑 1 输入一个名为“客户 A”的新客户 - 使用唯一 ID,可能会为其分配客户 ID 20。笔记本电脑 2 输入“客户 C” - 它还可以为该客户分配 ID 20 。当需要同步时,客户 A 和客户 A 都需要进行同步。 C 最终会在服务器上具有重复的 ID。
有没有人使用过类似的应用程序,并且有一个优雅的解决方案?
I'm building a desktop application that will run on multiple laptops. It will need to sync up to a central database whenever the user is back in the office and has access again.
My biggest problem to overcome is how to design the database so that it is easily synced with the central database server. One of the major hurdles is trying to determine how to handle keys such that they don't duplicate across the multiple laptop databases that'll be in use.
For example, say Laptop 1 enters a new customer called "Customer A" - using a unique id, it might be assigned a customer ID of 20. Laptop 2 enters "Customer C" - it could also assign the ID of 20 to that customer. When it comes time to sync, both customer A & C would end up on the server with a duplicate ID.
Has anyone worked with an app similar to this that has an elegant solution?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
替代方案是:
Alternatives are:
使用Guids(又名
uniqueidentifier
)作为主键,所有复制问题都将消失。自动增量int
PK 的支持者几乎总是大大夸大了使用 Guid 的惩罚。它们需要的额外大小(16 个字节与int
的 4 个字节)是如此微不足道,以至于我很惊讶它竟然出现在这个主题的讨论中。在Guid
PK 上JOIN
的查询运行速度会比在int
PK 上JOIN
的查询运行得慢,但这不是一个速度慢一个数量级(即 10 倍)。您的结果可能会有所不同,但我发现Guid
JOIN 查询的执行时间大约要长 40%(在您记住摩尔定律之前,这似乎是一个很大的损失)。使用 Guid 进行 PK 还可以让您在插入相关数据时不必做一些非常棘手的事情。您不必插入子记录,然后在插入父行之前检索刚刚插入的行的 ID,您只需使用
Guid.NewGuid()
在客户端创建所有 ID。我之前曾使用过复制系统,使用
int
PK 的指定范围来解决此问题,但我永远不会再这样做。Use Guids (aka
uniqueidentifier
) for your primary keys, and all of your problems with replication will go away. The penalties for using Guids are almost always drastically overstated by proponents of auto-incrementint
PKs. The extra size they require (16 bytes vs. 4 bytes for anint
) is so trivial that I'm amazed it ever comes up in discussions on this subject. Queries thatJOIN
onGuid
PKs will run slower than queries thatJOIN
onint
PKs, but it is not an order-of-magnitude (i.e. 10X) slower. Your results may vary, but I've found thatGuid
JOIN queries take around 40% longer to execute (which may seem like a large penalty until you remember Moore's law).Using Guids for PKs also gets you out of doing really hacky stuff when you're inserting related data. Instead of having to insert the child records and then retrieve the IDs for the just-inserted rows before inserting the parent rows, you just create all the IDs client-side with
Guid.NewGuid()
.I've worked with replicated systems before that used assigned ranges with
int
PKs to solve this problem, but I will never ever do that again.我不知道这是一个“优雅”的解决方案来解决这个非常不优雅的问题。 Remus 有很好的想法,也建议你阅读一些有关复制的内容。
您还最好设计一个重复数据删除流程,因为可以肯定的是,代表 A 将在客户 A 中进行更新,而代表 B 也将在客户 A 中进行更新,并且由于它们来自具有不同主键的不同来源,因此它们将是不同的记录。
I don't know that the is an "elegant" solution to this very inelegant problem. Remus has good thoughts, also suggest you do some reading on replication.
You had also better design a de-dup process because sure as anything, rep A is going to upt in customer A and rep b is going to put in customer A as well and because they came from differnt sources with differnt primary keys, they will be different records.