大資料量查詢容易OOM?試試MySQL流式查詢
阿新 • • 發佈:2021-01-04
![mark](https://img2020.cnblogs.com/other/1769816/202101/1769816-20210104093317640-597087048.png)
## 一、前言
程式訪問 `MySQL` 資料庫時,當查詢出來的資料量特別大時,資料庫驅動把載入到的資料全部載入到記憶體裡,就有可能會導致記憶體溢位(OOM)。
其實在 `MySQL` 資料庫中提供了流式查詢,允許把符合條件的資料分批一部分一部分地載入到記憶體中,可以有效避免OOM;本文主要介紹如何使用流式查詢並對比普通查詢進行效能測試。
## 二、JDBC實現流式查詢
使用JDBC的 `PreparedStatement/Statement` 的 `setFetchSize` 方法設定為 `Integer.MIN_VALUE` 或者使用方法 `Statement.enableStreamingResults()` 可以實現流式查詢,在執行 `ResultSet.next()` 方法時,會通過資料庫連線一條一條的返回,這樣也不會大量佔用客戶端的記憶體。
```java
public int execute(String sql, boolean isStreamQuery) throws SQLException {
Connection conn = null;
PreparedStatement stmt = null;
ResultSet rs = null;
int count = 0;
try {
//獲取資料庫連線
conn = getConnection();
if (isStreamQuery) {
//設定流式查詢引數
stmt = conn.prepareStatement(sql, ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_READ_ONLY);
stmt.setFetchSize(Integer.MIN_VALUE);
} else {
//普通查詢
stmt = conn.prepareStatement(sql);
}
//執行查詢獲取結果
rs = stmt.executeQuery();
//遍歷結果
while(rs.next()){
System.out.println(rs.getString(1));
count++;
}
} catch (SQLException e) {
e.printStackTrace();
} finally {
close(stmt, rs, conn);
}
return count;
}
```
>**PS**:上面的例子中通過引數 `isStreamQuery` 來切換**流式查詢**與**普通查詢**,用於下面做測試對比。
## 三、效能測試
建立了一張測試表 `my_test` 進行測試,總資料量為 `27w` 條,分別使用以下4個測試用例進行測試:
1. 大資料量普通查詢(27w條)
2. 大資料量流式查詢(27w條)
3. 小資料量普通查詢(10條)
4. 小資料量流式查詢(10條)
### 3.1. 測試大資料量普通查詢
```java
@Test
public void testCommonBigData() throws SQLException {
String sql = "select * from my_test";
testExecute(sql, false);
}
```
#### 3.1.1. 查詢耗時
27w 資料量用時 38 秒
![file](https://img2020.cnblogs.com/other/1769816/202101/1769816-20210104093318158-1302731894.png)
#### 3.1.2. 記憶體佔用情況
使用將近 1G 記憶體
![](https://img2020.cnblogs.com/other/1769816/202101/1769816-20210104093318502-279531128.png)
### 3.2. 測試大資料量流式查詢
```java
@Test
public void testStreamBigData() throws SQLException {
String sql = "select * from my_test";
testExecute(sql, true);
}
```
#### 3.2.1. 查詢耗時
27w 資料量用時 37 秒
![](https://img2020.cnblogs.com/other/1769816/202101/1769816-20210104093318779-1909265854.png)
#### 3.2.2. 記憶體佔用情況
由於是分批獲取,所以記憶體在30-270m波動
![](https://img2020.cnblogs.com/other/1769816/202101/1769816-20210104093319029-1605463363.png)
### 3.3. 測試小資料量普通查詢
```java
@Test
public void testCommonSmallData() throws SQLException {
String sql = "select * from my_test limit 100000, 10";
testExecute(sql, false);
}
```
#### 3.3.1. 查詢耗時
10 條資料量用時 1 秒
![](https://img2020.cnblogs.com/other/1769816/202101/1769816-20210104093319314-86334942.png)
### 3.4. 測試小資料量流式查詢
```java
@Test
public void testStreamSmallData() throws SQLException {
String sql = "select * from my_test limit 100000, 10";
testExecute(sql, true);
}
```
#### 3.4.1. 查詢耗時
10 條資料量用時 1 秒
![](https://img2020.cnblogs.com/other/1769816/202101/1769816-20210104093319476-2097636636.png)
## 四、總結
MySQL 流式查詢對於記憶體佔用方面的優化還是比較明顯的,但是對於查詢速度的影響較小,主要用於解決大資料量查詢時的記憶體佔用多的場景。
**DEMO地址**:https://github.com/zlt2000/mysql-stream-query
**掃碼關注有驚喜!**
![file](https://img2020.cnblogs.com/other/1769816/202101/1769816-20210104093319672-1472451