简体   繁体   English

将MySQL数据库拆分为单独的数据库

[英]Splitting MySQL Database into separate databases

I have a requirement that the MySQL database being used in my application is scaling very aggressively. 我要求我的应用程序中使用的MySQL数据库必须非常积极地扩展。 I am in no state currently to migrate to a NoSQL Database. 我目前没有任何状态可以迁移到NoSQL数据库。

I have figured out the following areas where I can try splitting the current database into multiple databases: 我已经找出了以下方面,可以尝试将当前数据库拆分为多个数据库:

  1. There are some tables which have static content, ie it changes barely. 有些表具有静态内容,即几乎没有变化。
  2. There are user tables which store the user data upon interaction which changes drastically. 有一些用户表在交互时存储用户数据,这些表发生了巨大变化。

Now, if i split the database into two different databases, how will I handle the transaction? 现在,如果我将数据库分为两个不同的数据库,我将如何处理交易? How will I write the Data Access Layer, will i have connections to both the databases? 我将如何编写数据访问层,两个数据库都可以连接吗? The application currently uses Spring & Hibernate for Back End. 该应用程序当前使用Spring&Hibernate作为后端。 There are calls which join the user tables and the content tables in the current schema. 有些调用将当前模式中的用户表和内容表联接在一起。

The architecture follows the current structure: Controller -> Service -> DAO Layer. 该体系结构遵循当前结构:控制器->服务-> DAO层。

So, if i am willing to refactor the DAO layer which communicates with the database, what approach should i follow? 因此,如果我愿意重构与数据库通信的DAO层,应该采用哪种方法? I know only about Hibernate ORM but i would be willing to letting it go if there is something better than Hibernate. 我只了解Hibernate ORM,但如果有比Hibernate更好的东西,我将不予理会。

Multiple databases on the same server? 同一台服务器上有多个数据库? That approach will probably not improve performance on its own. 这种方法可能不会单独提高性能。 RAM, fast disks, optimization, partitioning, and correct indexing will have a far greater payback. RAM,快速磁盘,优化,分区和正确的索引将获得更大的回报。

If you have multiple databases on one server you can connect to them with a single connection, and simply use the database names with the table names in your SQL. 如果一台服务器上有多个数据库,则可以通过单个连接连接到它们,并且只需在SQL中使用数据库名称和表名称即可。 Transactions work fine within a single connection. 事务在单个连接中运行良好。

Transactions across multiple connections and multiple servers are harder. 跨多个连接和多个服务器的交易更加困难。 There's a feature in MySQL called XA transactions to help handle this. MySQL中有一个称为XA事务的功能可以帮助解决这一问题。 But it has plenty of overhead, and is therefore most useful for high-value transactions as in banking. 但是它有很多开销,因此对于银行中的高价值交易最有用。

In the jargon of the trade, adding servers is called "scale-out." 在行业术语中,添加服务器称为“横向扩展”。 The alternative is "scale-up," in which you add more RAM, faster direct-access storage, optimization, and other stuff to a single server to get it to do more. 替代方案是“向上扩展”,其中您可以在单个服务器上添加更多的RAM,更快的直接访问存储,优化和其他功能,以使其发挥更大的作用。

There are several approaches you can take to the scale-out problem. 您可以采用几种方法来解决横向扩展问题。 The classic one is to use MySQL to do a single-master multiple-load-balanced slave replication. 经典的一种是使用MySQL进行单主机多负载平衡的从机复制。 That's probably the path that's most often taken, so you can do it without reinventing a lot of wheels. 这可能是最常使用的路径,因此您无需重新发明很多轮子就可以做到。 In this solution you do all your writing to a single instance. 在此解决方案中,您将所有写入工作都写到一个实例上。 Queries that look up data can use multiple read-only load-balanced instances. 查找数据的查询可以使用多个只读负载平衡实例。

http://dev.mysql.com/doc/refman/5.5/en/replication-solutions-scaleout.html http://dev.mysql.com/doc/refman/5.5/en/replication-solutions-scaleout.html

This is a very popular approach where you have a mix of long-running reporting queries and short-running interactive queries. 这是一种非常流行的方法,其中您同时使用了长期运行的报表查询和短期运行的交互式查询。 The reporting can be run on dedicated slave servers. 该报告可以在专用的从属服务器上运行。

Another approach is multiple-master replication using MySQL Cluster. 另一种方法是使用MySQL Cluster进行多主机复制。 https://dev.mysql.com/doc/refman/5.5/en/mysql-cluster-replication-multi-master.html https://dev.mysql.com/doc/refman/5.5/en/mysql-cluster-replication-multi-master.html

Another approach, if you have money to spend, is to go with a supported MySQL Cluster. 如果您有足够的钱可用,另一种方法是使用受支持的MySQL群集。 Oracle, MariaDB, and Percona have such products on offer. Oracle,MariaDB和Percona提供了此类产品。

Scale-out is a big job no matter how you approach it. 无论您采用何种方式,向外扩展都是一项艰巨的工作。 There's some documented experience from other people who have done it. 有其他人记录的经验。 For example, https://www.facebook.com/note.php?note_id=23844338919 例如, https://www.facebook.com/note.php?note_id=23844338919

It sounds like you did not thought about the partition of your database. 听起来您好像没有考虑数据库的分区。 You should read something about database normalization first: database normalization 您应该先阅读有关数据库规范化的内容: 数据库规范化

To split the database i would export the sql code from the database, then i would make 2 new files were i copy the tables that i want to have in the specific databases. 要拆分数据库,我将从数据库中导出sql代码,然后在复制要在特定数据库中包含的表时创建2个新文件。 After that i would import the 2 files in the specific databases. 之后,我将在特定数据库中导入2个文件。

i think this might help u help me: lets say i want to print reports for a user. 我认为这可能对您有所帮助:可以说我想为用户打印报告。 the user is persisted in 'user' table and there is a score table which has the user score for every user_id. 用户被保留在“用户”表中,并且存在一个分数表,其中包含每个user_id的用户分数。 Now, my plan is to put the user table in one database, and score table in another database, making them two data sources. 现在,我的计划是将用户表放在一个数据库中,将评分表放在另一个数据库中,使它们成为两个数据源。 How can i handle such a scenario? 我该如何处理这种情况?

First to put the tables in different databases make no sence for me and i did not know if there is a ability to make select queries with to different databases mixed. 首先把表放在不同的数据库对我来说毫无意义,我也不知道是否有能力对混合在一起的不同数据库进行选择查询。

example: SELECT score, name FROM user, score WHERE score > 100 AND(score.user_id = user.user_id); 例如:SELECT得分,FROM用户的名字,WHERE得分> 100 AND(score.user_id = user.user_id);

I dont no if this fit with two databases i think not. 我不认为这是否适合两个数据库,我认为不是。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM