Batch Processing: executeBatch() for Bulk INSERT/UPDATE Performance
Batch Processing: executeBatch() for Bulk INSERT/UPDATE Performance
Introduction
जब हम JDBC (Java Database Connectivity) के जरिए database operations perform करते हैं, तो कई बार हमें एक ही तरह के multiple queries execute करनी पड़ती हैं — जैसे कि कई records insert करना या update करना। अगर हम हर query को अलग-अलग execute करें तो performance बहुत slow हो जाती है। इसी problem को solve करने के लिए JDBC में एक बहुत ही powerful feature दिया गया है — Batch Processing।
Batch Processing का मतलब होता है — कई SQL statements को एक साथ group करके execute करना, ताकि database बार-बार connect और disconnect न हो। इसके लिए Statement या PreparedStatement के साथ executeBatch() method use किया जाता है।
What is Batch Processing?
Batch Processing एक ऐसी technique है जिससे हम multiple SQL statements को एक ही time पर execute कर सकते हैं। इसका main purpose performance को improve करना और execution time को कम करना होता है।
Simple शब्दों में कहें तो, अगर हमें 100 records insert करने हैं, तो हम उन्हें 100 बार execute करने के बजाय एक batch में add करके एक बार में execute कर सकते हैं।
Key Points of Batch Processing
- Multiple SQL statements को एक batch में group किया जाता है।
addBatch()method से statements को batch में add किया जाता है।executeBatch()method से सभी statements एक साथ run किए जाते हैं।- यह performance को कई गुना बढ़ा देता है।
Why Use Batch Processing?
जब हम database में बहुत सारे insert या update operations करते हैं, तो हर बार network connection open और close होने में time लगता है। इससे performance कम हो जाती है। Batch processing इस overhead को कम करता है।
इसका फायदा यह है कि:
- Execution time काफी कम हो जाता है।
- Server load कम होता है।
- Application performance काफी बेहतर हो जाती है।
How Batch Processing Works in JDBC
JDBC में batch processing को implement करने के लिए तीन simple steps होते हैं:
- SQL statements को batch में add करना।
- Batch को execute करना।
- Batch को clear करना या handle करना।
Batch को add करने के लिए addBatch() method और execute करने के लिए executeBatch() method का use किया जाता है।
Basic Flow of Batch Processing
- Database Connection बनाओ।
PreparedStatementयाStatementobject बनाओ।- Queries को
addBatch()के जरिए जोड़ो। - सभी queries को एक साथ
executeBatch()से execute करो। - Connection को close करो।
Example Using Statement
नीचे एक simple example दिया गया है जहाँ हम Statement का use करके multiple insert statements एक batch में execute कर रहे हैं:
try {
Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/studentdb", "root", "password");
Statement stmt = con.createStatement();
stmt.addBatch("INSERT INTO students VALUES (101, 'Ravi', 'CSE')");
stmt.addBatch("INSERT INTO students VALUES (102, 'Amit', 'ECE')");
stmt.addBatch("INSERT INTO students VALUES (103, 'Neha', 'IT')");
int[] count = stmt.executeBatch();
System.out.println("Records inserted: " + count.length);
con.close();
} catch (Exception e) {
e.printStackTrace();
}
इस example में हमने तीन insert statements को batch में add किया है और फिर उन्हें एक साथ execute किया है। इससे overall execution time काफी कम हो जाता है।
Example Using PreparedStatement
अब देखते हैं कि PreparedStatement के साथ batch processing कैसे की जाती है। यह approach ज्यादा efficient और secure होती है क्योंकि यह SQL Injection को prevent करती है और query को precompile करती है।
try {
Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/studentdb", "root", "password");
PreparedStatement ps = con.prepareStatement("INSERT INTO students VALUES (?, ?, ?)");
ps.setInt(1, 201);
ps.setString(2, "Rohan");
ps.setString(3, "CSE");
ps.addBatch();
ps.setInt(1, 202);
ps.setString(2, "Priya");
ps.setString(3, "IT");
ps.addBatch();
ps.setInt(1, 203);
ps.setString(2, "Karan");
ps.setString(3, "ECE");
ps.addBatch();
int[] count = ps.executeBatch();
System.out.println("Total Records Inserted: " + count.length);
con.close();
} catch (Exception e) {
e.printStackTrace();
}
इस example में हमने PreparedStatement का use किया है जो SQL statements को compile करके रखता है और हर बार केवल data values bind करता है। इससे performance और security दोनों improve होती हैं।
Batch Processing for Bulk UPDATE
Batch Processing केवल insert के लिए ही नहीं बल्कि update, delete जैसे DML operations के लिए भी use किया जा सकता है।
PreparedStatement ps = con.prepareStatement("UPDATE students SET branch=? WHERE id=?");
ps.setString(1, "AI");
ps.setInt(2, 101);
ps.addBatch();
ps.setString(1, "ML");
ps.setInt(2, 102);
ps.addBatch();
ps.setString(1, "DS");
ps.setInt(2, 103);
ps.addBatch();
int[] result = ps.executeBatch();
System.out.println("Records updated: " + result.length);
इस तरह हम multiple update queries को भी एक batch में group करके execute कर सकते हैं।
Advantages of Batch Processing
- Execution speed बहुत ज्यादा बढ़ जाती है।
- Network calls कम होते हैं।
- Database load कम हो जाता है।
- PreparedStatement के साथ use करने पर security बढ़ती है।
- Code clean और maintainable रहता है।
Performance Comparison (Single vs Batch Execution)
| Operation Type | Records | Execution Time |
|---|---|---|
| Single Execution | 1000 | ~5.2 seconds |
| Batch Execution | 1000 | ~0.8 seconds |
ऊपर के table से साफ है कि Batch Execution में performance कई गुना बेहतर होती है।
Best Practices for Batch Processing
- Auto-commit को disable करके batch execute करें।
- Batch size को optimize करें (जैसे 500 या 1000 records)।
- Exception handling के लिए
try-catchका use करें। - Resources को हमेशा close करें।
- PreparedStatement का use करें — ये ज्यादा efficient है।
Error Handling in Batch Processing
Batch execution के दौरान अगर कोई एक query fail हो जाए तो बाकी queries पर भी effect पड़ सकता है। इसलिए error handling बहुत जरूरी है।
JDBC में हम BatchUpdateException को catch करके पता कर सकते हैं कि कौन-सी statement fail हुई है।
try {
ps.executeBatch();
} catch (BatchUpdateException e) {
int[] updateCounts = e.getUpdateCounts();
System.out.println("Failed at: " + updateCounts.length);
}
Real-Life Use Cases
- Bulk data insertion (जैसे CSV या Excel से data import करना)
- Student records update करना
- Large-scale inventory management
- Bank transactions या payroll updates
Important Points to Remember
addBatch()से SQL statement batch में जोड़ो।executeBatch()से एक साथ execute करो।clearBatch()से batch को clear कर सकते हो।- PreparedStatement का use करना ज्यादा efficient है।
Notes for Exam
- Batch Processing का use performance improve करने के लिए किया जाता है।
addBatch()औरexecuteBatch()methods key role play करते हैं।- PreparedStatement का use SQL Injection से बचाता है।
- Bulk insert और update operations के लिए सबसे effective तरीका यही है।
- Exam में अक्सर पूछा जाता है — “Explain executeBatch() method with example।”