如何在数据库中单独保存用户特定数据?
我正处于设计一个应用程序的规划阶段,该应用程序将涉及用户在我的网站上注册并存储数据。我已决定使用 Spring 和 Hibernate,但我有几个问题希望得到答案。
我的第一个问题涉及数据分离。当用户在我的网站注册时,他们将能够存储从其他来源导入的数据。每个用户的数据量(行数)将增长得非常快。如果我将所有用户数据存储在一个数据库中,我预计很快就会遇到性能问题。为了解决这个问题,我计划为每个注册的用户创建一个新的数据库。这将通过某些脚本或其他编程解决方案即时发生。这可行吗?有谁知道有什么设计模式可以解决这个问题?
我的第二个问题与我的第一个问题相关。如果为每个用户创建一个新数据库是可行的,那么这将如何与hibernate一起工作?显然,我不会在每次新用户注册时重新启动我的应用程序。可以动态配置 hibernate 以根据某些条件(当前登录的用户)连接到不同的数据库吗?如果我有两个用户同时登录,则预期的行为是每个用户只能访问其数据库中的数据。
谢谢。
I'm in the planning phase of designing an app that will involve users registering with my website and storing data. I've decided on using Spring and Hibernate but I have a couple questions for which I would like to get answers.
My first question involves data separation. When a user registers with my site, they will be able to store data imported from another source. The amount of data (number of rows) for each user will grow very fast. If I store all user data in a single database I expect to run into performance problems very quickly. To address this, I was planning to create a new database for each user that signs up. This would happen on the fly via some script or other programmatic solution. Is this feasible? Does anyone know of any design pattern that addresses this problem?
My second question is related to my first question. If it is feasible to create a new database for each user, how will this work with hibernate? Obviously I won't restart my application every time a new user signs up. Can hibernate be configured dynamically to connect to a different database based on some criteria (currently logged in user)? If I have two users logged in simultaneously, the expected behavior would be that each user only have access to data in their database.
Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
数据库性能问题通常归结为糟糕的索引、内存不足和/或糟糕的 SQL 查询。每个用户一个数据库到目前为止并不能解决这个问题,这只是过于复杂。对此并没有真正的设计模式,只需拥有具有足够内存的可靠硬件和具有良好索引列的良好标准化数据库即可。放置正确的索引相对简单,通常只需将其放置在具有键约束的列和涉及
WHERE
子句的列上即可。 Hibernate/JPA 反过来通常生成优化良好的 SQL 查询并提供二级缓存可能性。DB performance problems usually boils down to poor indexing, not enough memory and/or poor SQL queries. A single DB per user does by far not solve this, that's only overcomplicated. There is not really a design pattern for this, just have solid hardware with enough memory and a well-normalized database with well-indexed columns. Putting the right indexes is relatively trivial, generally it suffices to just put it on columns which have key constraints and columns which are involved in
WHERE
clauses. Hibernate/JPA in turn generates in general well-optimized SQL queries and offer second-level caching possiblities.