当前位置:网站首页>Using multithreaded callable to query Oracle Database
Using multithreaded callable to query Oracle Database
2022-07-02 00:05:00 【Hu Bayi】
Use single threaded queries 10 Ten thousand data are too slow , Use multithreading to query , Increase of efficiency .
ThredQuery Method of querying data
public class ThredQuery implements Callable<List<HashVO>> {
private MyUtil util = new MyUtil();
private int bindex;// The current number of pages
private int num;// How many queries per page
private List page;// The data found in each page
private String customerName;
private String customerType;
private String idType;
private String idNumb;
private String updateTime;
public ThredQuery(int bindex,int num,String customerName, String customerType, String idType, String idNumb, String updateTime) {
this.bindex=bindex;
this.num=num;
this.customerName=customerName;
this.customerType=customerType;
this.idType=idType;
this.idNumb=idNumb;
this.updateTime=updateTime;
}
@Override
public List<HashVO> call() throws Exception {
String sql = "";
if (StringUtils.isNotBlank(customerName)) {
sql += " and customer_name like '%" + customerName + "%' ";
}
if (StringUtils.isNotBlank(customerType)) {
sql += " and customer_type = '" + customerType + "' ";
}
if (StringUtils.isNotBlank(idType)) {
sql += " and id_type = '" + idType + "' ";
}
if (StringUtils.isNotBlank(idNumb)) {
sql += " and id_numb like '%" + idNumb + "%' ";
}
if (StringUtils.isNotBlank(updateTime)) {
sql += " and (" + updateTime + ")";
}
sql += " order by id";
String finalSql = "SELECT * FROM " +
"(" +
"SELECT A.*, ROWNUM RN " +
"FROM ("
+ sql +
" ) A " +
" WHERE ROWNUM <= "+ (bindex*num)+
" ) " +
"WHERE RN >= "+(((bindex-1)*num)+1);
return Arrays.asList(util.getHashVOs(finalSql));
}
}
Be careful :call Inside is the query method .util Is to query data , perform sql
oracle Paging method , With pageIndex and pageSize For example
String sql= "SELECT * FROM " +
"(" +
"SELECT A.*, ROWNUM RN " +
"FROM ("
+ sql +
" ) A " +
" WHERE ROWNUM <= "+ (pageIndex*pageSize)+
" ) " +
"WHERE RN >= "+(((pageIndex-1)*pageSize)+1);
Calling method :
private void getVoListMultiThreading(String customerName, String customerType, String idType, String idNumb, String updateTime) {
long start = System.currentTimeMillis();// Starting time
List<HashVO> result = new ArrayList<>();// Return results
// Query the total number of databases
int count = queryCount();
int num = 8000;// How many items are queried at a time
// Number of queries required
int times = count / num;
if (count % num != 0) {
times = times + 1;
}
// Start page number The connection is orcle The database of Encapsulated paging Mine is from 1 Start
//Callable Used to produce results
List<Callable<List<HashVO>>> tasks = new ArrayList<>();
for (int i = 1; i <= times; i++) {
// Multithreaded query
Callable<List<HashVO>> qfe = new ThredQuery(i, num, customerName, customerType, idType, idNumb, updateTime);
tasks.add(qfe);
}
// Define a fixed length thread pool Prevent too many threads
ExecutorService executorService = Executors.newFixedThreadPool(15);
//Future Used to get results
try {
List<Future<List<HashVO>>> futures=executorService.invokeAll(tasks);
// The processing thread returns the result
if(futures!=null&&futures.size()>0){
for (Future<List<HashVO>> future:futures){
result.addAll(future.get());
}
}
} catch (InterruptedException e) {
throw new RuntimeException(e);
} catch (ExecutionException e) {
throw new RuntimeException(e);
}
executorService.shutdown();// Close thread pool
long end = System.currentTimeMillis();// End time
log.info(" Query time consuming :" + (end - start) / 1000 + " second "," result : "+result);
}
Query time is only used 30s
边栏推荐
- .env.xxx 文件,加了常量,却undefined
- [embedded system course design] a single key controls the LED light
- 在长城证券上买基金安全吗?
- Iota in golang
- leetcode96不同的二叉搜索樹
- LDR6035智能蓝牙音响可充可放(5.9.12.15.20V)快充快放设备充电
- Leetcode96 different binary search trees
- cookie、session、tooken
- Kubernetes resource object introduction and common commands (III)
- Chapter 6 data flow modeling
猜你喜欢

Algolia's search needs are almost closed

Concurrentskiplistmap -- principle of table skipping

Ldr6035 smart Bluetooth audio can be charged and released (5.9.12.15.20v) fast charging and fast releasing device charging

PWN attack and defense world cgpwn2

Review data desensitization system

GCC compilation

Learn online case practice
![[embedded system course design] a single key controls the LED light](/img/c9/076618208bbab0b95faa5a7e644a07.png)
[embedded system course design] a single key controls the LED light

.env.xxx 文件,加了常量,却undefined

门级建模—课后习题
随机推荐
Vue force cleaning browser cache
使用uni-simple-router,动态传参 TypeError: Cannot convert undefined or null to object
Graduation season is both a farewell and a new beginning
基于全志H3的QT5.12.9移植教程
RPA教程01:EXCEL自动化从入门到实操
Using uni simple router, dynamically pass parameters typeerror: cannot convert undefined or null to object
PyCharm调用matplotlib绘图时图像弹出问题怎么解决
第六章 数据流建模
Selectively inhibiting learning bias for active sampling
algolia 搜索需求,做的快自闭了...
vs2015 AdminDeployment.xml
下载在线视频 m3u8使用教程
SQL数据分析之子查询的综合用法和案例题【耐心整理】
Deep learning | three concepts: epoch, batch, iteration
JPA handwritten SQL, received with user-defined entity classes
cookie、session、tooken
Selectively inhibiting learning bias for active sampling
Why does blocprovider feel similar to provider?
Relevant settings of wechat applet cache expiration time (recommended)
GCC compilation