赞
踩
针对之前存在的问题,现在做出了改进。对于2个经常要查询的表,制定了propertyblock类和propertycontrol类,propertyblock类里3个数组,分别存放名字,id和活跃度。propertycontrol里则有一个37*37的二维数组,共1369个propertyblock类作为入口(为什么是37*37呢,26个字母+10个数字+其他字符)为了加快查找速度,每次得到一个新的名字,就会根据前2个字符进行分类,调入相应入口进行处理。总的来讲,这么做是为了把需要频繁查找的2个表搬到内存里,免去了频繁与数据库连接耗去的大量时间。另外还在写入数据的时候使用了addBatch()和executeBatch()这一对指令,批量处理写入请求,加快了时间。
这次因为使用了自定义类,相应的结构和使用代码需要自己写,所以比较复杂,为了方便起见分成了3个类,分别是propertycontrol,propertyblock,以及javaConSQL类。经过改进以后,现在处理360W行只需要3分钟左右。如果是处理跟插入连在一起的话,则需要30分钟左右。
下面是代码
peropertyblock代码:
package databasetest;
public class propertyblock {
//类的属性
public String[] name ;
public int[] id;
public int[] activ;
public int currentindex; //表示待插入的数的位置
public int storage ;
public propertyblock next;
//类的方法
public propertyblock(){ //构造函数1
name=new String[10] ; //默认是10个一组
id=new int[10] ;
activ=new int[10];
currentindex=0 ;
storage=10;
next=null ;
}
public propertyblock(int storage){ //构造函数2
name=new String[storage] ; //默认是10个一组
id=new int[storage] ;
activ=new int[storage];
currentindex=0 ;
this.storage=storage ;
next=null ;
}
public void insert(String name,int id,int activ){ //插入函数
this.name[currentindex]=name;
this.id[currentindex]=id;
this.activ[currentindex]=activ ;
currentindex++;
}
public boolean isFull(){
if (currentindex>=storage) return true;
else return false;
}
public boolean isEmpty(){
if (currentindex==0) return true;
else return false ;
}
public int find(String name){ //查找函数
int count=0 ;
while (count<currentindex){
if (this.name[count].equals(name)){
this.activ[count]++ ; //活跃度+1
return this.id[count] ;
}
else count++ ;
}
return -1;
}
}
propertycontrol代码
package databasetest;
public class propertycontrol {
public propertyblock[][] index ; //26+10+1 共计37个propertyblock数组
public int id;
public propertycontrol(){
id=0 ; //当前设定的id
index=new propertyblock[37][37]; //此处要记住 自定义类数组的初始化方法,如果没有下面循环,则报错Java.lang.NullPointerException
for(int i=0;i<37;i++){
for(int j=0;j<37;j++)
index[i][j]=new propertyblock() ; //37数组初始化
}
}
public int deal(String name){
int guide;
int guide_b ;
int resultid;
int returnid;
//分配入口
guide=name.charAt(0);
if ((guide>=97)&&(guide<=122)) guide=guide-97 ; //0-25
else if ((guide>=65)&&(guide<=90)) guide=guide-65 ;
else if ((guide>=48)&&(guide<=57)) guide=guide-22 ;//26-35
else guide=36;
if (name.length()>1){
guide_b=name.charAt(1);
if ((guide_b>=97)&&(guide_b<=122)) guide_b=guide_b-97 ; //0-25
else if ((guide_b>=65)&&(guide_b<=90)) guide_b=guide_b-65 ;
else if ((guide_b>=48)&&(guide_b<=57)) guide_b=guide_b-22 ;//26-35
else guide_b=36;
}
else guide_b=36 ;
propertyblock currentblock=index[guide][guide_b]; //起始当前块
while (true){
resultid=currentblock.find(name);
if (resultid==-1){ //如果当前块找不到
if (currentblock.next==null){ //如果所有块已找完
if (currentblock.isFull()){ //如果当前块已满
propertyblock temp=new propertyblock();
temp.insert(name, id, 1);
returnid=id ;
id++;
currentblock.next=temp;
currentblock=temp;
break;
}
else{
currentblock.insert(name, id, 1);
returnid=id;
id++;
break ;
}
}
else
currentblock=currentblock.next;
}
else{ //如果在当前块找到了
returnid=resultid;
break;
}
}
return returnid;
}
/*
public static void main(String[] args){
propertycontrol a=new propertycontrol();
a.deal("adfadfa");
a.deal("dbdfdfdf");
a.deal("Alfjlaksf");
a.deal("Ddkljlwe");
a.deal("123ljlkdf");
a.deal("-alsdjf");
a.deal("adfadfa");
a.deal("adfadfadfdf");
System.out.println(a.id);
}
*/
}
javaConSQL类代码
package databasetest;
import java.sql.*;
public class javaConSQL{
public static void main(String[] args) {
String JDriver="com.microsoft.sqlserver.jdbc.SQLServerDriver";//SQL数据库引擎
String connectDB="jdbc:sqlserver://127.0.0.1:1433;DatabaseName=multiangle";//数据源
try{
Class.forName(JDriver);//加载数据库引擎,返回给定字符串名的类
}catch(ClassNotFoundException e){ //e.printStackTrace();
System.out.println("加载数据库引擎失败");
System.exit(0);
}
System.out.println("数据库驱动成功");
try{
String query ;
String user="sa";
String password="admin";
Connection con=DriverManager.getConnection(connectDB,user,password);//连接数据库对象
System.out.println("连接数据库成功");
Statement stmt=con.createStatement();//创建SQL命令对象
Statement stmt_initial=con.createStatement(); //用作插入用
int bookid,userid;
System.out.println("删除旧的initial");
query="drop table initial";
stmt_initial.executeUpdate(query);
System.out.println("开始创建initial数据库");
query="create table initial(row int,bookid int,bookname varchar(50),userid int,username varchar(50),score int);";
stmt_initial.executeUpdate(query);
System.out.println("开始读取数据");
query="select *,row_number() over (order by bookname) as row from dbo.douban";
//query="select *,row_number() over (order by bookname) as row from dbo.douban where username='eastwolf'";
ResultSet rs=stmt.executeQuery(query); //返回SQL语句查询结果集(集合)
propertycontrol reader=new propertycontrol();
propertycontrol book=new propertycontrol();
int batchcount=0 ;
while(rs.next()){
bookid=book.deal(rs.getString("bookname"));
userid=reader.deal(rs.getString("username"));
if (batchcount>1000) {
stmt_initial.executeBatch();
batchcount=0;
System.out.println("在第"+rs.getInt("row")+"行的时候执行了一次写入");
}
else{
query="insert into dbo.initial (row,bookid,bookname,userid,username,score) values ("+rs.getInt("row")+','+bookid+",'"+rs.getString("bookname")+"',"+userid+",'"+rs.getString("username")+"',"+Integer.parseInt(rs.getString("score"))+");";
stmt_initial.addBatch(query);
batchcount++;
}
//System.out.println(query) ; //测试用
if (batchcount>0)stmt_initial.executeBatch();
//将寻找到的再加上处理过的数值插入initial表
//System.out.println("line "+rs.getInt("row")+" is dealed");
}
//关闭连接
stmt.close();//关闭命令对象连接
stmt_initial.close();
System.out.println("initial表填写完毕") ;
System.out.println("The num of reader is "+reader.id);
System.out.println("The num of book is "+book.id);
//进行user_table和book的填写
Statement stmt_book=con.createStatement(); //创建book查询对象
Statement stmt_user=con.createStatement() ; //创建user查询对象
System.out.println("删除旧的user_table");
query="drop table user_table";
stmt_user.executeUpdate(query);
System.out.println("开始创建user_table数据库");
query="create table user_table(username varchar(50),userid int,useractiv int);";
stmt_user.executeUpdate(query);
batchcount=0 ;
int detect_book=0,detect_user=0 ;
for(int i=0;i<37;i++){
for(int j=0;j<37;j++){
propertyblock current=reader.index[i][j];
while(true){
for(int k=0;k<current.currentindex;k++){
detect_user++;
if (batchcount>1000){
stmt_user.executeBatch();
batchcount=0;
}
else{
query="insert into user_table values ('"+current.name[k]+"',"+current.id[k]+','+current.activ[k]+");";
stmt_user.addBatch(query);
batchcount++;
}
}
//处理当前块的打印
if (current.next==null) break;
else current=current.next ;
}
}
}
if (batchcount>0)stmt_user.executeBatch();
System.out.println(detect_user+" users is inserted into user_table");
batchcount=0;
System.out.println("删除旧的book");
query="drop table book";
stmt_book.executeUpdate(query);
System.out.println("开始创建book数据库");
query="create table book(bookname varchar(50),bookid int,bookactiv int)";
stmt_book.executeUpdate(query);
for(int i=0;i<37;i++){
for(int j=0;j<37;j++){
propertyblock current=book.index[i][j];
while(true){
for(int k=0;k<current.currentindex;k++){
detect_book++ ;
if (batchcount>1000){
stmt_book.executeBatch() ;
batchcount=0;
}
else{
query="insert into book values ('"+current.name[k]+"',"+current.id[k]+','+current.activ[k]+");";
stmt_book.addBatch(query);
batchcount++;
}
}
//处理当前块的打印
if (current.next==null) break;
else current=current.next ;
}
}
}
if (batchcount>0) stmt_book.executeBatch();
System.out.println(detect_book+" books is inserted into book table");
stmt_user.close();
stmt_book.close();
con.close();//关闭数据库连接
}
catch(SQLException e){
e.printStackTrace();
//System.out.println("数据库连接错误");
System.exit(0);
}
}
}
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。