Electricity supplier orders sub-library sub-table system

One or two sub-library sub-table program

 General industry, for orders sub-library sub-table data, there are two types of thinking: In accordance with the order number to segmentation, in accordance with the user id to segmentation.

 A scheme, in accordance with the order number to do hash dispersion orders

   The order number is treated as a string, do hash, go across multiple servers.

  Which libraries, which store data specific to table it? The number in the order number to record.

  If you want to query a user all orders it?

  Since the data is dispersed in accordance with the order number. His orders are dispersed in multiple libraries, multiple tables.

  You can not go all the libraries, all of the table scan it. This efficiency is very low. (Solution: maintain the relationship table uid and oid of this table can be used as a cache, when the amount of data increases this table must also be sub-table)

  Option II generally use more, all orders of a user, in a table inside, do pagination when displayed, it is easy.

Option II, in accordance with the user id broken up orders

  Uid to be segmentation data, there are two ideas:

The first one is what the library to a range of uid orders. 0-2 ten million uid, order data corresponding to a database, a table. 20 million to 40 million b corresponding orders to the library.

  Why is this program with less than what?

  Prone to bottlenecks do. Users within a certain range, orders more than that, then this library pressure is particularly great. Other libraries without much pressure.

The second is to use a modulo operation uid. The second program with more than the industry.

  In one aspect, the process is simple, do a modulo operation on a program like.

  On the other hand, the use of modulo manner, more evenly dispersed into a plurality of data to the library. A single library is not prone to performance bottlenecks.

  But the benefits are not there: that is, to the expansion of time, too much trouble. We need to migrate the data.

  To the expansion of time, in order to reduce the amount of data migration, increase in the general form of expansion in multiples. For example, turned out to be eight banks, expansion of time, we must increase to 16 library expansion again, it increased to 32 libraries. That amount of data migration, it is much smaller. This problem is not a big problem, after all, once expansion, can ensure a long time, and increase the use of multiple ways, has reduced the amount of data migration.

 

Analyze the following manner in accordance with the user id of the modulo sub-library sub-table.

  According to user id as key segmentation order data, as follows:

1, the positioning library name: user id end 4 Mod 32.

    Mod represents divided by a number, taking the remaining number. For example, after divided by 32, and the remaining 8, the remainder is 8.

    The code symbols are expressed in%: 15% 4 = 3.

2, the table name of the end user id positioning :( 4 Dev 32) Mod 32.

    Dev divided represents a number, the results were an integer. 25.6 obtain such a result, 25 is rounded.

    Code with / to represent: $ get_int = floor (15/4). 15 divided by 4, it is a fraction 3.75, 3 is rounded down. Must be rounded down, rounding up the 4 becomes.

   According to the above rule: a total of how many tables can say then? Library each library 32 * 32 = 1024 tables table. If you want a small number of tables, put the number of piecemeal 32.

 

Number of Libraries Library ID = userId% 4 
Quantities ID = userId / Library Number 8% Table 4 

Or 
the number of libraries ID = userId / Number 4% Table 4 Library 
Table ID = userId% Table 8 Number
 

 

The above is expressed in computer terms, the following description simple words.

1, computing library name

  4-digit user id's, modulo 32 (modulo divided by this number is, how much I). The remaining number is between 0-31.

  This can represent from between 0-31, a total of 32 digits. With this 32 numbers represent 32 library name: order_db_0, order_db_2 ......................... order_db_31

2, the table name of the computer

  Finally, the name of which table to be stored inside set to go?

  The last four digits of the user id, divided by 32, rounded. 32 is divided by an integer, to obtain the remainder can be expressed from the 32 numbers between 0-31, a table name.

  Table name like this: order_tb_1, order_tb_2 .......................... order_tb_31. Inside a library, a total of 32 table name.

  Such as user id: 19408064, 8064 with the last four digits is divided by 32 to obtain 251.9 is an integer which is taken 251.

  32 is then divided by 251, taking the remainder, the remainder is 27.

  To maintain performance, the amount of data of each table to be controlled. Single table data can be maintained at ten million - 50 million rows. 1024 * ten million. Wow, a lot of data can be represented.

Third, think the advantages and disadvantages

advantage

  Order level sub-library sub-table, why should segmentation in accordance with the user id to do?

  Benefits: Specifies the user's query for all orders to avoid cross-database queries across tables.

   Because, the node is calculated according to a user's id, user's id is a predetermined constant, then the calculated value is always fixed (x-x library table)

    So save the order, when a user of all orders, are in the library x x table inside. When a user needs to query all orders, do not be cross-database, cross-table to the query.

Shortcoming

  Disadvantage in that: the data non-uniform dispersion, the amount of data particularly some tables, some tables small amount of data. Since some single volume multiple users, particularly analogy, 1000-2000 users within this range, the next single,

  And their id based on calculation rules are assigned x x library table. After the cause of this large amount of data tables, the amount of data to support single-table limit, they supposed to do?

 

   To summarize: Each sub-library sub-table program is not perfect, they are both advantages and disadvantages. Currently, this user id to order data segmentation scheme, or by most companies to use. The actual results were pretty good. Programmers easy, as the skyrocketing amount of data, a later date yet. After all, the company's business development and to what extent, I do not know how long the project survival, the future uncertain. Kang Zhu first to say.

 

A thinking, orders b2b platform points when sellers and buyers to choose what fields to sub-library sub-table it?

Case discussed above is, b2c platform. Orders on a seller, is the platform itself.

b2b platform, supported above the shop, buyers and sellers must be able to log in to see their orders.

Let's look at points table using a sub-library sub-table id buyers and sellers id problem according to sub-library sub-table, two approaches appear

If the buyer-id to sub-library sub-table. There are sellers of goods, there will be n users to buy all his orders, will be distributed to multiple libraries to multiple tables, the seller query all of their orders, cross-database, cross-table scans, poor performance.

If you press the seller id sub-library sub-table. Buyers in the next n single shop. Orders will be spread across multiple libraries, multiple tables. Buyer inquiries all of their orders, all the same going to the library, searching all the tables, poor performance.

So, whether it is in accordance with the buyer id segmentation order form, or in accordance with the seller id segmentation Orders table. Not to please both sides.

Taobao approach is to split the buyers and sellers libraries library, that is, two libraries: libraries buyers, sellers library.

Buyers library, in accordance with the user id to sub-library sub-table. Sellers library, according to the seller's id to sub-library sub-table.

  Actually solved by data redundancy: an order, in the library there are buyers, sellers in a library which is also stored. When placing an order, write two data. First buyers library written orders to go inside, and then synchronize the order data to sellers go inside the library through messaging middleware.

 

After the buyer orders a modified library to send asynchronous messages, notifications to the seller the library to change the status.

Guess you like

Origin www.cnblogs.com/heqiyoujing/p/11297432.html