小言_互联网的博客

测试JAVA生成短8位UUID,是否重复问题,100万数据耗时26秒,越往后性能成指数增加

354人阅读  评论(0)

在网上有一种短8位UUID生成的方法,代码来源:

JAVA生成短8位UUID


  
  1. public static String[] chars = new String[] { "a", "b", "c", "d", "e", "f",
  2. "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s",
  3. "t", "u", "v", "w", "x", "y", "z", "0", "1", "2", "3", "4", "5",
  4. "6", "7", "8", "9", "A", "B", "C", "D", "E", "F", "G", "H", "I",
  5. "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V",
  6. "W", "X", "Y", "Z" };
  7. public static String generateShortUuid() {
  8. StringBuffer shortBuffer = new StringBuffer();
  9. String uuid = UUID.randomUUID().toString(). replace( "-", "");
  10. for ( int i = 0; i < 8; i++) {
  11. String str = uuid.substring(i * 4, i * 4 + 4);
  12. int x = Integer.parseInt(str, 16);
  13. shortBuffer.append(chars[x % 0x3E]);
  14. }
  15. return shortBuffer.toString();
  16. }

我们进行测试看到底多少会出现重复,写了一个比较简单的方法:

设置了线程池,数据库连接池,每一个线程进行处理一百万条数据,每次携带7万条数据进行数据库的插入。我们将ID设置为数据库的主键,如果出现错误,则表示数据库ID出现重复现象。

如果需要一次性插入更多的数据,或者在插入的时候报下面的错误:

Packet for query is too large (4,800,048 > 4,194,304)

  
  1. 修改 my.ini 加上 max_allowed_packet = 67108864
  2. 67108864= 64M
  3. 默认大小 4194304  也就是 4M
  4. 修改完成之后要重启mysql服务,如果通过命令行修改就不用重启mysql服务。
  5. 命令修改:
  6. 设置为 500M
  7. mysql> set global max_allowed_packet = 500* 1024* 1024
  8. 查看mysql的max_allowed_packet大小,运行
  9. show VARIABLES like '%max_allowed_packet%';

下面是插入数据代码,进行测试:


  
  1. private static ThreadPoolExecutor threadPoolExecutor= new ThreadPoolExecutor( 50, 100,
  2. 0L, TimeUnit.MILLISECONDS,
  3. new LinkedBlockingQueue<Runnable>( 1024), new DefaultManagedAwareThreadFactory(), new ThreadPoolExecutor.AbortPolicy());
  4. @Override
  5. public void saveTest() {
  6. threadPoolExecutor.execute( new saveUUID());
  7. }
  8. /**
  9. * 设置保存信息的线程
  10. */
  11. class saveUUID implements Runnable{
  12. public saveUUID(){
  13. }
  14. @Override
  15. public void run() {
  16. System.out.println( "===================>>>>>" + Thread.currentThread().getName());
  17. List<Map<String,Object>> userList = new ArrayList<>();
  18. int index= 0;
  19. for( int i = 0; i< 1000000;i++){
  20. long startTime = System.currentTimeMillis();
  21. Map<String,Object> map = new HashMap<>();
  22. map.put( "id",getUUID8());
  23. userList.add(map);
  24. index++;
  25. if (index== 70000) {
  26. int count = testMapper.saveTest(userList);
  27. if (count > 0) {
  28. userList.clear();
  29. index= 0;
  30. long time =System.currentTimeMillis()-startTime;
  31. System.out.println( Thread.currentThread().getName() + "======>>>>" +time );
  32. }
  33. }
  34. }
  35. if(!userList.isEmpty()){
  36. testMapper.saveTest(userList);
  37. }
  38. }
  39. }

XML文件:


  
  1. <insert id= "saveTest" parameterType= "java.util.List">
  2. insert into tb_user(id,user_name)
  3. values
  4. <foreach item= "item" index= "index" collection= "list" separator= ",">
  5. ( #{item.id},#{item.id})
  6. </ foreach>
  7. </insert>

进行测试当数据达到:2129580,也就是两百万的时候出现第一次重复,进行数据库数据查询,发现重复原因是因为MySQL不区分大小写。

关于MySQL主键不区分大小写,或则其他查询不区分大小写 Duplicate entry 'AOVbrXXF' for key 'PRIMARY'

将MySQL进行大小写区分。再进行测试。

数据达到400万条无重复,系统插入缓慢。

在基础数据为0的情况下,插入数据100万。系统花费时间为:

26.318秒

 

在基础数据为0的情况下,插入数据200万。系统花费的时间为:

64.097秒

 

在基础数据为0的情况下,插入数据,系统花费时间:


  
  1. DefaultManagedAwareThreadFactory- 1====== >>>> 3209
  2. DefaultManagedAwareThreadFactory- 1====== >>>> 1033
  3. DefaultManagedAwareThreadFactory- 1====== >>>> 1089
  4. DefaultManagedAwareThreadFactory- 1====== >>>> 952
  5. DefaultManagedAwareThreadFactory- 1====== >>>> 921
  6. DefaultManagedAwareThreadFactory- 1====== >>>> 1119
  7. DefaultManagedAwareThreadFactory- 1====== >>>> 825
  8. DefaultManagedAwareThreadFactory- 1====== >>>> 911
  9. DefaultManagedAwareThreadFactory- 1====== >>>> 919
  10. DefaultManagedAwareThreadFactory- 1====== >>>> 850
  11. DefaultManagedAwareThreadFactory- 1====== >>>> 949
  12. DefaultManagedAwareThreadFactory- 1====== >>>> 983
  13. DefaultManagedAwareThreadFactory- 1====== >>>> 720
  14. DefaultManagedAwareThreadFactory- 1====== >>>> 1097
  15. DefaultManagedAwareThreadFactory- 1====== >>>> 862
  16. DefaultManagedAwareThreadFactory- 1====== >>>> 1173
  17. DefaultManagedAwareThreadFactory- 1====== >>>> 942
  18. DefaultManagedAwareThreadFactory- 1====== >>>> 808
  19. DefaultManagedAwareThreadFactory- 1====== >>>> 1159
  20. DefaultManagedAwareThreadFactory- 1====== >>>> 845
  21. DefaultManagedAwareThreadFactory- 1====== >>>> 998
  22. DefaultManagedAwareThreadFactory- 1====== >>>> 1478
  23. DefaultManagedAwareThreadFactory- 1====== >>>> 1420
  24. DefaultManagedAwareThreadFactory- 1====== >>>> 974
  25. DefaultManagedAwareThreadFactory- 1====== >>>> 1398
  26. DefaultManagedAwareThreadFactory- 1====== >>>> 4005
  27. DefaultManagedAwareThreadFactory- 1====== >>>> 2922
  28. DefaultManagedAwareThreadFactory- 1====== >>>> 2126
  29. DefaultManagedAwareThreadFactory- 1====== >>>> 1198
  30. DefaultManagedAwareThreadFactory- 1====== >>>> 931
  31. DefaultManagedAwareThreadFactory- 1====== >>>> 2197
  32. DefaultManagedAwareThreadFactory- 1====== >>>> 1014
  33. DefaultManagedAwareThreadFactory- 1====== >>>> 1231
  34. DefaultManagedAwareThreadFactory- 1====== >>>> 3416
  35. DefaultManagedAwareThreadFactory- 1====== >>>> 4471
  36. DefaultManagedAwareThreadFactory- 1====== >>>> 960
  37. DefaultManagedAwareThreadFactory- 1====== >>>> 1199
  38. DefaultManagedAwareThreadFactory- 1====== >>>> 1142
  39. DefaultManagedAwareThreadFactory- 1====== >>>> 940
  40. DefaultManagedAwareThreadFactory- 1====== >>>> 2211
  41. DefaultManagedAwareThreadFactory- 1====== >>>> 23994
  42. DefaultManagedAwareThreadFactory- 1====== >>>> 37751
  43. DefaultManagedAwareThreadFactory- 1====== >>>> 51163
  44. DefaultManagedAwareThreadFactory- 1====== >>>> 66993
  45. DefaultManagedAwareThreadFactory- 1====== >>>> 79591
  46. DefaultManagedAwareThreadFactory- 1====== >>>> 91846

由上可见当数据量达到一定量的时候,时间成指数上升。

如何能突破这个时间节点,估计还需要进一步优化。

生产UUID满足千万条数据

目前1147万条数据,没有出现重复,满足原作者说的千万条数据不重复


转载:https://blog.csdn.net/weixin_41986096/article/details/105579978
查看评论
* 以上用户言论只代表其个人观点,不代表本网站的观点或立场