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.