1. Introduction When a program accesses a In fact, 2. JDBC implements streaming query Streaming query can be implemented by setting the public int execute(String sql, boolean isStreamQuery) throws SQLException { Connection conn = null; PreparedStatement stmt = null; ResultSet rs = null; int count = 0; try { //Get database connection conn = getConnection(); if (isStreamQuery) { //Set streaming query parameters stmt = conn.prepareStatement(sql, ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_READ_ONLY); stmt.setFetchSize(Integer.MIN_VALUE); } else { //Normal query stmt = conn.prepareStatement(sql); } //Execute the query to get the result rs = stmt.executeQuery(); //Traverse the results while (rs.next ()) { System.out.println(rs.getString(1)); count++; } } catch (SQLException e) { e.printStackTrace(); finally close(stmt, rs, conn); } return count; } "PS": In the above example, the parameter 3. Performance Test A test table
3.1. Test large data volume general query@Test public void testCommonBigData() throws SQLException { String sql = "select * from my_test"; testExecute(sql, false); } 3.1.1. Query time 27w data volume takes 38 seconds 3.1.2. Memory usage Uses nearly 1G memory 3.2. Testing large data volume streaming queries@Test public void testStreamBigData() throws SQLException { String sql = "select * from my_test"; testExecute(sql, true); } 3.2.1. Query time 27w data volume takes 37 seconds 3.2.2. Memory usage Since it is acquired in batches, the memory fluctuates between 30-270m 3.3. Test small data volume ordinary query@Test public void testCommonSmallData() throws SQLException { String sql = "select * from my_test limit 100000, 10"; testExecute(sql, false); } 3.3.1. Query time 10 pieces of data take 1 second 3.4. Test streaming query with small amount of data@Test public void testStreamSmallData() throws SQLException { String sql = "select * from my_test limit 100000, 10"; testExecute(sql, true); } 3.4.1. Query time 10 pieces of data take 1 second IV. ConclusionMySQL streaming query has obvious optimization effects on memory usage, but has little impact on query speed. It is mainly used to solve the scenario of high memory usage when querying large amounts of data. 「DEMO address」: https://github.com/zlt2000/mysql-stream-query This is the end of this article about using streaming queries in MySQL to avoid data OOM. For more relevant MySQL streaming query content, please search for previous articles on 123WORDPRESS.COM or continue to browse the following related articles. I hope everyone will support 123WORDPRESS.COM in the future! You may also be interested in:
|
<<: A simple way to build a Docker environment
>>: Native JS to implement login box email prompt
The information on Baidu is so diverse that it...
Table of contents 1. Original value and reference...
Mysql installation, configuration, and optimizati...
MySQL is an open source, small relational databas...
This article shares the specific code for JavaScr...
This article example shares the specific code of ...
1. Installation Package MYSQL service download ad...
Table of contents docker system df docker system ...
The cause is that the process opens a number of f...
Under the requirements of today's responsive ...
MySQL is a database that I like very much. Today,...
W3C recently released two standards, namely "...
reason: MySQL 5.7.5 and up implements detection o...
question: My blog encoding is utf-8. Sometimes whe...
Nowadays, whether you are on the sofa at home or ...