Primary key generation strategy when mysql sub-database sub-table

Portal: http://blog.csdn.net/bluishglc/article/details/7710738#java

Please refer to the above address.

This primary key generation method, compared with the twritter generation method, has the advantage that the value of id can be controlled Yes, you can achieve what kind of id you want it to generate.

Here is a test program, I7 2720, 16G on my machine. A single mysql, up to 2800/s. Two mysql concurrently, up to 4800/s s

is subject to the multi-threaded processing capability of the cpu. When testing with 3 or 4 mysql, the performance did not improve. It can be seen that the highest processing capability of the cpu multi-threading has been reached.
package mysqlJDBC;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import org.junit.Test;

import com.alibaba.druid.pool.DruidDataSource;
import com.alibaba.druid.pool.DruidPooledConnection;

public class JdbcTest {

	@Test
	public void test() throws SQLException {
		
		 getConn37();
		  getConn38();
		long begin = System.currentTimeMillis();
		int step = 10000;
		long  total=3061;
		
		
		for (int i = 0; i < step; i+=2) {
//			System.out.println("count i="+i);
			new Thread(new Runnable() {

				@Override
				public void run() {

					try {
						DruidPooledConnection conn = getConn37();
						PreparedStatement ps1 = conn
								.prepareStatement("REPLACE INTO Tickets64 (stub) VALUES ('a')");
						PreparedStatement ps2 = conn
								.prepareStatement("SELECT LAST_INSERT_ID()");
						ps1.execute();
						ResultSet rs = ps2.executeQuery(); // Execute the prepared statement to get the query result set
						while (rs.next()) {
							System.out.println(rs.getString(1));
							System.out.println(System.currentTimeMillis() - begin);
						}
						rs.close();
						 ps1.close();
						 ps2.close();
						 conn.close();

					} catch (SQLException e) {
						e.printStackTrace ();
					}
					 
				}
			}).start();
			new Thread(new Runnable() {

				@Override
				public void run() {

					try {
						DruidPooledConnection conn = getConn38();
						PreparedStatement ps1 = conn
								.prepareStatement("REPLACE INTO Tickets64 (stub) VALUES ('a')");
						PreparedStatement ps2 = conn
								.prepareStatement("SELECT LAST_INSERT_ID()");
						ps1.execute();
						ResultSet rs = ps2.executeQuery(); // Execute the prepared statement to get the query result set
						while (rs.next()) {
							System.out.println(rs.getString(1));
							System.out.println(System.currentTimeMillis() - begin);
						}
						rs.close();
						 ps1.close();
						 ps2.close();
						 conn.close();

					} catch (SQLException e) {
						e.printStackTrace ();
					}
					 
				}
			}).start();

		}
		
		
		try {
			Thread.sleep(10000);
		} catch (InterruptedException e) {
			e.printStackTrace ();
		}
		System.out.println("total is " + total);
		System.out.println("per fix is " + (step * 1000) / total);
	}

	private static DruidPooledConnection getConn37() {
		
		
		
		
		DruidPooledConnection conn = null;
		try {
			  conn = DbConnectionFactory.getDataSource37().getConnection();
		} catch (SQLException e) {
			e.printStackTrace ();
		}
		return conn;
	}
	
	
private static DruidPooledConnection getConn38() {
		
		
		
		
		DruidPooledConnection conn = null;
		try {
			  conn = DbConnectionFactory.getDataSource38().getConnection();
		} catch (SQLException e) {
			e.printStackTrace ();
		}
		return conn;
	}
	
	
}

 class DbConnectionFactory{
	 private DbConnectionFactory(){
		 
	 }
	 public static DruidDataSource getDataSource38()
	 {
		 return Holder38.druidDataSource;
	 }
	 public static DruidDataSource getDataSource37()
	 {
		 return Holder37.druidDataSource;
	 }
	 static class  Holder38{
		 public  static  DruidDataSource druidDataSource= Holder38.getConnectionPool();
		 private static   DruidDataSource getConnectionPool(){
			 DruidDataSource	dataSource = new DruidDataSource();
				dataSource.setDriverClassName("com.mysql.jdbc.Driver");
				dataSource.setUsername("root");
				dataSource.setPassword("1qaz2wsx");
				dataSource.setUrl("jdbc:mysql://192.168.1.38:3308/idserver");
				dataSource.setInitialSize(100);
				dataSource.setMinIdle(50);
				dataSource.setMaxActive(200);
				// Enable monitoring statistics function dataSource.setFilters("stat");
				dataSource.setPoolPreparedStatements(true);// for mysql
				dataSource.setMaxPoolPreparedStatementPerConnectionSize(100);
				//Configure the minimum lifetime of a connection in the pool, in milliseconds
				dataSource.setMinEvictableIdleTimeMillis(300000);
				 return dataSource;
		 }
	 }
	 
	 static class  Holder37{
		 public  static  DruidDataSource druidDataSource= Holder37.getConnectionPool();
		 private static   DruidDataSource getConnectionPool(){
			 DruidDataSource	dataSource = new DruidDataSource();
				dataSource.setDriverClassName("com.mysql.jdbc.Driver");
				dataSource.setUsername("root");
				dataSource.setPassword("1qaz2wsx");
				dataSource.setUrl("jdbc:mysql://192.168.1.37:3308/idserver");
				dataSource.setInitialSize(100);
				dataSource.setMinIdle(50);
				dataSource.setMaxActive(200);
				// Enable monitoring statistics function dataSource.setFilters("stat");
				dataSource.setPoolPreparedStatements(true);// for mysql
				dataSource.setMaxPoolPreparedStatementPerConnectionSize(100);
				//Configure the minimum lifetime of a connection in the pool, in milliseconds
				dataSource.setMinEvictableIdleTimeMillis(300000);
				 return dataSource;
		 }
	 }
 }

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326944691&siteId=291194637