How does PHP calculate which database to insert based on uid?

Source: Internet
Author: User
How does PHP calculate which database to insert based on uid? Now I want to implement a function:
How can we calculate the uid of million users based on calculation rules, which correspond to tables with different insert logs?


Reply to discussion (solution)

Yes. balanced access to logs will not result in a large amount of data in each table. how can this problem be achieved?

The balanced access to logs will not result in a large amount of data in each table.

Uid is usually a number, and the Modulo is enough.

Substr ('20140901',-1); // 6

Determine which table exists based on the number of digits of the uid (0-9, 10 tables)

Please give your code as a reference. thank you.

Does this require code?
Set $ uid to the uid value, and $ m to the number of log tables.
The table name $ tbl = 'prefix'. ($ uid % $ m)

I don't know what naming rules are for multiple tables? Is it 200 million users, randomly assigned a log table, or which table should be specified when this user is created?

1. if it is random, it is best to have certain naming rules for the table, such as log001, log002... and log100. Use rand () to process a number and then combine it into a table name;
2. if you specify the table to be written when you create the table, it is better to write the table name to the session when you log on. when you log on, you can directly splice the table name into the session;
3. if you want to determine the user's first character, you can also use method 2;

Is there a code generator that automatically generates a table? if I have 300 million members and generate tables, who has the code to generate the table first, then the table name to be inserted is automatically calculated based on the uid? Please give the code

You need to define a table first, and then

For ($ I-0; $ I <300; $ I ++) mysql_query ("carete table log _ $ I select * from table where 1 = 0");
300 tables are created.

The table name at the time of insertion is calculated. I have already given it in #6.

How frequently is this operation performed on the database.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.