简体   繁体   English

我应该为每个用户创建单独的 SQL Server 数据库吗?

[英]Should I create separate SQL Server database for each user?

I am working on Asp.Net MVC web application, back-end is SQL Server 2012.我正在开发 Asp.Net MVC Web 应用程序,后端是 SQL Server 2012。

This application will provide billing, accounting, and inventory management.此应用程序将提供计费、会计和库存管理。 The user will create an account by signup.用户将通过注册创建一个帐户。 just like http://www.quickbooks.in .就像http://www.quickbooks.in一样。 Each user will create some masters and various transactions.每个用户都会创建一些主人和各种交易。 There is no limit, user can make unlimited records in the database.没有限制,用户可以在数据库中进行无限记录。

I want to keep stable database performance, after heavy data load.我想在繁重的数据负载后保持稳定的数据库性能。 I am maintaining proper indexing and primary keys in it, but there would be a heavy load on the database, per user.我在其中维护正确的索引和主键,但是每个用户的数据库负载会很重。

So, should I create a separate database for each user, or should maintain one database with UserID.那么,我应该为每个用户创建一个单独的数据库,还是应该使用 UserID 维护一个数据库。 Add UserID in each table and making a partition based on UserID?在每个表中添加 UserID 并根据 UserID 进行分区?

I am not an expert in SQL Server, so please provide suggestions with clear specifications.我不是 SQL Server 的专家,所以请提供具有明确规范的建议。

Please inform me if there is any lack of information.如果有任何信息不足,请通知我。

A DB per user is what happens when customers need to be able pack up and leave taking the actual database with them.每个用户的数据库是当客户需要能够打包并随身携带实际数据库时发生的情况。 Think of a self hosted wordpress website.想想一个自托管的 wordpress 网站。 Or if there are incredible risks to one user accidentally seeing another user's data, so it's safer to rely on the servers security model than to rely on remembering to add the UserId filter to all your queries.或者,如果一个用户意外看到另一个用户的数据存在难以置信的风险,那么依赖服务器安全模型比依赖记住将 UserId 过滤器添加到所有查询更安全。 I can't imagine a scenario like that, but who knows-- maybe if the privacy laws allowed for jail time, I would rather data partitioned by security rules rather than carefully writing WHERE clauses.我无法想象这样的场景,但谁知道呢——也许如果隐私法允许监禁,我宁愿数据按安全规则分区,也不愿仔细编写 WHERE 子句。

If you did do user-per-database, creating a new user will be 10x more effort.如果您确实按数据库执行用户操作,则创建新用户的工作量将增加 10 倍。 While INSERT, UPDATE and so on stay the same from version to version, with each upgrade the syntax for database, user creation, permission granting and so on will evolve enough to break those scripts each SQL version upgrade.虽然 INSERT、UPDATE 等从版本到版本保持不变,但每次升级数据库、用户创建、权限授予等的语法都会发展到足以破坏每个 SQL 版本升级的脚本。

Also, this will multiply your migration headaches by the number of users.此外,这将使您的迁移难题乘以用户数量。 Let's say you have 5000 users and you need to add some new columns, change a columns data type, update a trigger, and so on.假设您有 5000 个用户,您需要添加一些新列、更改列数据类型、更新触发器等。 Instead of needing to run that change script 1x, you need to run it 5000 times.您不需要运行该更改脚本 1 次,而是需要运行它 5000 次。

Per user Dbs also probably wastes disk space.每个用户的 Dbs 也可能会浪费磁盘空间。 Each of those databases is going to have a transaction log, sitting idle taking up the minimum log space.这些数据库中的每一个都将有一个事务日志,闲置占用最少的日志空间。

As for load, if collectively your 5000 users are doing 1 billion inserts, updates and so on per day, my intuition tells me that it's going to be faster on one database, unless there is some sort of contension issue (everyone reading and writing to the same table at the same time and the same pages of the same table).至于负载,如果您的 5000 个用户每天总共执行 10 亿次插入、更新等操作,我的直觉告诉我,在一个数据库上它会更快,除非存在某种冲突问题(每个人都在同一张表,同一张表的同页)。 Each database has machine resources (probably threads and memory) per database doing housekeeping, so these extra DBs can't be free.每个数据库都有机器资源(可能是线程和内存),每个数据库都在做内务处理,所以这些额外的 DB 不可能是免费的。

Anyhow, the best thing to do is to simulate the two architectures and use a random data generator to simulate load and see how they perform.无论如何,最好的办法是模拟这两种架构,并使用随机数据生成器来模拟负载并查看它们的性能。

It's not an easy answer to give.这不是一个容易给出的答案。

First, there is logical design to be considered.首先,需要考虑逻辑设计。 Then you have integrity, security, management and performance (in this very order).然后是完整性、安全性、管理和性能(按此顺序)。

A database is a logical unit of data, self contained.数据库是数据的逻辑单元,自包含。 Ideally, you should be able to take a database, move it to another instance, probably change the connection strings and be running again.理想情况下,您应该能够获取一个数据库,将其移动到另一个实例,可能更改连接字符串并再次运行。 All the constraints are database-level.所有的约束都是数据库级别的。 No foreign keys can exist referencing some object outside the database.不能存在引用数据库外的某个对象的外键。 So, try thinking in these terms first.因此,请先尝试从这些方面进行思考。

How would you reliably prevent one user messing up the other user's data?您将如何可靠地防止一个用户弄乱另一个用户的数据? Keep in mind that it's just a matter of time before someone opens an excel sheet and fire up queries on the database bypassing your application.请记住,有人打开 Excel 工作表并绕过您的应用程序启动对数据库的查询只是时间问题。 Row level security in SQL Server is something you don't want to deal with. SQL Server 中的行级安全性是您不想处理的。

Multiple databases mean that all management tasks should be scripted out and executed on all databases.多个数据库意味着所有管理任务都应该在所有数据库上编写和执行。 Yes, there is some overhead to it, but once you set it up it's just the matter of monitoring.是的,它有一些开销,但是一旦设置好,它就只是监控的问题。 If a database goes suspect, it's a single customer down, not all of them.如果数据库出现问题,则是单个客户宕机,而不是所有客户。 You can even have different versions for different customes if each customer have it's own database.如果每个客户都有自己的数据库,您甚至可以为不同的客户提供不同的版本。 Additionally, if you roll an upgrade, you can do it per customer, so the inpact will be much less.此外,如果您进行升级,您可以按客户进行升级,因此影响会小得多。

Performance is the least relevant factor here.性能在这里是最不相关的因素。 Of course, it really depends on how many customers and how much data, but proper indexing will solve these issues.当然,这真的取决于有多少客户和多少数据,但适当的索引将解决这些问题。 Scale-out is much easier with multiple databases.使用多个数据库进行横向扩展要容易得多。

BTW, partitioning, as you mentioned it, is never a performance booster, it's simply a management feature, allowing for faster loading and evicting of data from a table.顺便说一句,正如您提到的,分区从来都不是性能助推器,它只是一种管理功能,允许更快地从表中加载和驱逐数据。

I'd probably put each customer in separate database, but it's up to you eventually to make a decision for yourself.我可能会将每个客户放在单独的数据库中,但最终由您自己做出决定。 Hope I've helped some with this.希望我已经帮助了一些人。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 我应该为每个应用程序创建一个单独的数据库吗? - Should I make a separate database for each application? 如何将txt文件中的每一行插入到SQL Server数据库的单独行中? - How can I insert each line from txt file into separate rows in a SQL Server database? 在每个数据库的动态SQL中创建用户 - CREATE USER in Dynamic SQL for each database 如何用用户创建SQL Server数据库? - how to Create SQL Server Database with User? 我应该序列化我的数据还是为SQL Server CE数据库创建更多列? - Should I serialize my data or create more columns for a SQL Server CE database? 如何在添加到项目的SQL Server Express数据库中创建用户? - How can I create a user in SQL Server Express database I added to my project? 如何在SQL Server中为每个用户创建所有考勤记录 - How to create all Time Attendance records for Each User In SQL Server 在哪些情况下,我应该为多列创建一个索引而不是为每一列创建一个单独的索引? - In which cases should I create one index for multiple columns instead of a separate index for each column? SQL查询为4个数据库列的值的每个不同组合创建单独的数据框? - SQL query to create separate dataframes for each distinct combination of values of 4 database columns? 我应该设计一个SQL Server数据库来依赖UNION还是避免它? - Should I Design a SQL Server database to rely on UNION or avoid it?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM