就是一堆id比对嘛,一条sql就搞定了,一个一个比对明显是效率最低的处理方式[/quote]
还请大牛赐条sql语句?多谢多谢!
要完成的任务就是10w个ID和某个表中的id比对,查找数据库中没有但那10w个id里有的所有ID。[/quote]
把10w个id load进临时表里,做个join就出来了
或者SELECT * FROM table WHERE id IN (1,2,3,4) 查出结果再在内存取个差集也可以[/quote]
现在我已经解决这个问题了,我的思路是:
先从数据库中取出所有的id放在一个list里,然后跟我这边的list对比,当然,这里使用了特殊的算法,效率提高了不少,原来5分钟都执行不完,而现在只需要20几秒。算法如下:
public static void main(String[] args) {
int max_len = 50000;
ArrayList<Integer> list1 = new ArrayList<Integer>();
ArrayList<Integer> list2 = new ArrayList<Integer>();
for (int i=0; i<max_len; i++) {
list1.add((int)(Math.random()*max_len));
list2.add((max_len/2)+(int)(Math.random()*max_len));
}
System.out.printf("list1:%d, list2:%d\n", list1.size(), list2.size());
long start = System.currentTimeMillis();
HashSet<Integer> set_all = new HashSet<Integer>();
for (int i=0; i<list1.size(); i++) {
set_all.add(list1.get(i));
}
HashSet<Integer> set_dup = new HashSet<Integer>();
ArrayList<Integer> list2_clean = new ArrayList<Integer>();
for (int i=0; i<list2.size(); i++) {
if (set_all.add(list2.get(i))) { //in list2 but not in list1
list2_clean.add(list2.get(i));
} else {
set_dup.add(list2.get(i)); //in list2 and also in list1
}
}
ArrayList<Integer> list1_clean = new ArrayList<Integer>();
for (int i=0; i<list1.size(); i++) {
if (set_dup.add(list1.get(i))) { //in list1 but not in the duplicated set
list1_clean.add(list1.get(i));
}
}
long end = System.currentTimeMillis();
System.out.printf("list1 clean:%d, list2 clean:%d\n", list1_clean.size(), list2_clean.size());
System.out.printf("time spent : %dms\n", end-start);
}[/quote]
那你的问题就解决了。
再啰嗦一句:10w个id放进内存做处理是没什么压力,数据大了以后你内存就会爆。[/quote]
高手就是高手,以发展的眼光看问题,厉害,厉害!我会考虑这个问题的,谢谢啦!
就是一堆id比对嘛,一条sql就搞定了,一个一个比对明显是效率最低的处理方式[/quote]
还请大牛赐条sql语句?多谢多谢!
要完成的任务就是10w个ID和某个表中的id比对,查找数据库中没有但那10w个id里有的所有ID。[/quote]
把10w个id load进临时表里,做个join就出来了
或者SELECT * FROM table WHERE id IN (1,2,3,4) 查出结果再在内存取个差集也可以[/quote]
现在我已经解决这个问题了,我的思路是:
先从数据库中取出所有的id放在一个list里,然后跟我这边的list对比,当然,这里使用了特殊的算法,效率提高了不少,原来5分钟都执行不完,而现在只需要20几秒。算法如下:
public static void main(String[] args) {
int max_len = 50000;
ArrayList<Integer> list1 = new ArrayList<Integer>();
ArrayList<Integer> list2 = new ArrayList<Integer>();
for (int i=0; i<max_len; i++) {
list1.add((int)(Math.random()*max_len));
list2.add((max_len/2)+(int)(Math.random()*max_len));
}
System.out.printf("list1:%d, list2:%d\n", list1.size(), list2.size());
long start = System.currentTimeMillis();
HashSet<Integer> set_all = new HashSet<Integer>();
for (int i=0; i<list1.size(); i++) {
set_all.add(list1.get(i));
}
HashSet<Integer> set_dup = new HashSet<Integer>();
ArrayList<Integer> list2_clean = new ArrayList<Integer>();
for (int i=0; i<list2.size(); i++) {
if (set_all.add(list2.get(i))) { //in list2 but not in list1
list2_clean.add(list2.get(i));
} else {
set_dup.add(list2.get(i)); //in list2 and also in list1
}
}
ArrayList<Integer> list1_clean = new ArrayList<Integer>();
for (int i=0; i<list1.size(); i++) {
if (set_dup.add(list1.get(i))) { //in list1 but not in the duplicated set
list1_clean.add(list1.get(i));
}
}
long end = System.currentTimeMillis();
System.out.printf("list1 clean:%d, list2 clean:%d\n", list1_clean.size(), list2_clean.size());
System.out.printf("time spent : %dms\n", end-start);
}[/quote]
那你的问题就解决了。
再啰嗦一句:10w个id放进内存做处理是没什么压力,数据大了以后你内存就会爆。
就是一堆id比对嘛,一条sql就搞定了,一个一个比对明显是效率最低的处理方式[/quote]
还请大牛赐条sql语句?多谢多谢!
要完成的任务就是10w个ID和某个表中的id比对,查找数据库中没有但那10w个id里有的所有ID。[/quote]
把10w个id load进临时表里,做个join就出来了
或者SELECT * FROM table WHERE id IN (1,2,3,4) 查出结果再在内存取个差集也可以
就是一堆id比对嘛,一条sql就搞定了,一个一个比对明显是效率最低的处理方式[/quote]
还请大牛赐条sql语句?多谢多谢!
要完成的任务就是10w个ID和某个表中的id比对,查找数据库中没有但那10w个id里有的所有ID。[/quote]
把10w个id load进临时表里,做个join就出来了
或者SELECT * FROM table WHERE id IN (1,2,3,4) 查出结果再在内存取个差集也可以[/quote]
现在我已经解决这个问题了,我的思路是:
先从数据库中取出所有的id放在一个list里,然后跟我这边的list对比,当然,这里使用了特殊的算法,效率提高了不少,原来5分钟都执行不完,而现在只需要20几秒。算法如下:
public static void main(String[] args) {
int max_len = 50000;
ArrayList<Integer> list1 = new ArrayList<Integer>();
ArrayList<Integer> list2 = new ArrayList<Integer>();
for (int i=0; i<max_len; i++) {
list1.add((int)(Math.random()*max_len));
list2.add((max_len/2)+(int)(Math.random()*max_len));
}
System.out.printf("list1:%d, list2:%d\n", list1.size(), list2.size());
long start = System.currentTimeMillis();
HashSet<Integer> set_all = new HashSet<Integer>();
for (int i=0; i<list1.size(); i++) {
set_all.add(list1.get(i));
}
HashSet<Integer> set_dup = new HashSet<Integer>();
ArrayList<Integer> list2_clean = new ArrayList<Integer>();
for (int i=0; i<list2.size(); i++) {
if (set_all.add(list2.get(i))) { //in list2 but not in list1
list2_clean.add(list2.get(i));
} else {
set_dup.add(list2.get(i)); //in list2 and also in list1
}
}
ArrayList<Integer> list1_clean = new ArrayList<Integer>();
for (int i=0; i<list1.size(); i++) {
if (set_dup.add(list1.get(i))) { //in list1 but not in the duplicated set
list1_clean.add(list1.get(i));
}
}
long end = System.currentTimeMillis();
System.out.printf("list1 clean:%d, list2 clean:%d\n", list1_clean.size(), list2_clean.size());
System.out.printf("time spent : %dms\n", end-start);
}