How to Batch query (select) with NamedParameterJdbcTemplate - spring-boot

I have to run a select SQL on DB using NamedParameterJdbcTemplate which involved 'IN' clause and since IN Clause has a limit of 1000 values I want to perform a batch select operation.
Is something similar to batchUpdate available in Spring 3.x?

Related

query ORACLE database during springboot application no response

A sql like "SELECT COUNT(1) FROM tablename..." was executed by my springboot application with mybatis,but no response during a long time.
But when I executed the same sql directly in ORACLE,it worked and returned the response.
With executing these sql:
select event,count(*) from v$session_wait group by event order by 2 desc;
I found the 'latch:cache buffers chains' event.
I asked my frined and he told me that I can execute analyze table tablename compute statistics.
After executed the sql,the problem resolved,the select count sql executed by springboot application worked !
I feel puzzled,why the analyze sql worked?
Thank you.

Using JdbcCursorItemReader and IN clause in the SQL query

I am developing a spring batch application and using the chunk based processing.
For Reader i am using the org.springframework.batch.item.database.JdbcCursorItemReader class.
When configuring the Reader and specifying the SQL property, how can we use the combination of preparedStatementSetter to set values for an IN clause in the SQL?
My SQL:
select * from Employee where employeeId IN (,,,,,,).
How can I dynamically set the values for IN clause using preparedStatementSetter?

Batch update using Spring

I am trying to have batch update query However each update Query is different but running on the same table. The Where clause is the same.
For example :
TABLE : Column A,B,C,D,ID
update A where ID=1
update B,C where ID=1
update D,B where ID=1 and so on ... ( all the combinations of A,B,C,D)
I have investigated spring jdbc (JDBCTemplate and JDBCNamedParameter ) and QueryDsl but its not possible to have such updates.
Is there any other method by which such update as batch is possible ? I have stick to Spring-JDBC.
Do you want to use a prepared statement passing in the arguments for each update? If so, it's not possible to do this as a batch. You could batch multiple statements, but then you would have to create these statements without using placeholders for the arguments. In this scenario you would use the int[] JdbcTemplate.batchUpdate(String[] sql) method (http://docs.spring.io/spring/docs/4.0.3.RELEASE/javadoc-api/org/springframework/jdbc/core/JdbcTemplate.html#batchUpdate-java.lang.String:A-).
It's not possible to batch different prepared statements using the JDBC API. You can batch individual statements without arguments (http://docs.oracle.com/javase/7/docs/api/java/sql/Statement.html#addBatch(java.lang.String)) or batch multiple sets of arguments for a prepared statement (http://docs.oracle.com/javase/7/docs/api/java/sql/PreparedStatement.html#addBatch()), but the SQL statement would have to be the same for all sets of arguments.
You can still wrap multiple update calls in a transaction, but there would be multiple roundtrips to the database server.
You can wrap your update with a stored proc, then you can batch round trips to the database.
Inside the stored proc you'll need to generate the update based on the arguments passed in. So you could test for null, or pass a separate flag for each column. If the flag is set, then generate SQL that updates that column.

Hibernate named query re-parsed

We are using hibernate named query as follows :
named_query : Select this_ from TableA this_ where this_.id in( select max(id) from TableA where COLA is null group by COLB ) and rownum=1
Query query = getNamedQuery("nq.select.DirtySubject.onMaxDirtySubjectRecId");
List<SomeObject> objectList = query.list();
The query has been flagged by the DBAs and the comments verbatim are as follows
These indicate the SQL statements that are consuming the most parsing
resources every time they execute.
The SQL statements that appear in
this report are probably being reparsed. Excessively-parsed SQL
statements should be optimized to reduce their parsing frequency. This
involves using bind variables and identical statement syntax and case,
in order to be able to reuse any previously-parsed statements in the
SQL cache.
Examine these queries to see if any SQL optimization is
possible and reasonable.
Other material facts:
This query is part of a polling logic and gets fired repeatedly.
Database:Oracle 11G
Technology stack : Java,Hibernate,Tomcat,Linux,Oracle 11G
Questions:
1: Behind the scene- Hibernate will be using Prepared statement - correct ?
2: What can we possibly do more from the application side - to avoid re-parsing of this query ?
3: Anything we can do on the Database server to avoid re-parsing ?
The comment talks about parsing resources (in contrast to execution time/resources), but the statement itself does not really look that difficult to parse. The most interesting information would be the execution plan. I assume that the query is always the same (but the comment contains the word "probably").
As for question 1:
If you want to make sure that prepared statements do help, you can try to run the same query without Hibernate - just plain java.sql, PreparedStatement.
If so, there is a Hibernate prepared statement cache, but I have never used it.
As for question 2:
If this is your hotspot, you can use "direct SQL" with Hibernate (if it and your connection pool allows the caching of prepared statements) or even without it: Then you have full control.
As for question 3: Have a look on Oracle 11g result cache.

java 1.4 :how to insert multiple records in a database with one single hit using executeBatch?

i am reading records data from a file(records count can be up to thousands ).Now i want to insert each record in to database.I want to insert all of records in one hit to reduce performance hit. If i use addBatch(String sqlQuery ) on statment object,my sql query should be static .but in my case query will be non static.Please tell me possible solutions with best performance?
platform
java 1.4
sql server 2000.
From Wiki
A SQL feature (since SQL-92) is the use of row value constructors to insert multiple rows at a time in a single SQL statement:
INSERT INTO ''TABLE'' (''column1'', [''column2, ... ''])
VALUES (''value1a'', [''value1b, ...'']),
(''value2a'', [''value2b, ...'']),
...

Resources