目錄
- 一、前言
- 二、JDBC實(shí)現(xiàn)流式查詢
- 三、性能測(cè)試
- 3.1. 測(cè)試大數(shù)據(jù)量普通查詢
- 3.2. 測(cè)試大數(shù)據(jù)量流式查詢
- 3.3. 測(cè)試小數(shù)據(jù)量普通查詢
- 3.4. 測(cè)試小數(shù)據(jù)量流式查詢
- 四、總結(jié)
一、前言
程序訪問(wèn)MySQL
數(shù)據(jù)庫(kù)時(shí),當(dāng)查詢出來(lái)的數(shù)據(jù)量特別大時(shí),數(shù)據(jù)庫(kù)驅(qū)動(dòng)把加載到的數(shù)據(jù)全部加載到內(nèi)存里,就有可能會(huì)導(dǎo)致內(nèi)存溢出(OOM)。
其實(shí)在MySQL
數(shù)據(jù)庫(kù)中提供了流式查詢,允許把符合條件的數(shù)據(jù)分批一部分一部分地加載到內(nèi)存中,可以有效避免OOM;本文主要介紹如何使用流式查詢并對(duì)比普通查詢進(jìn)行性能測(cè)試。
二、JDBC實(shí)現(xiàn)流式查詢
使用JDBC的PreparedStatement/Statement
的setFetchSize
方法設(shè)置為Integer.MIN_VALUE
或者使用方法Statement.enableStreamingResults()
可以實(shí)現(xiàn)流式查詢,在執(zhí)行ResultSet.next()
方法時(shí),會(huì)通過(guò)數(shù)據(jù)庫(kù)連接一條一條的返回,這樣也不會(huì)大量占用客戶端的內(nèi)存。
public int execute(String sql, boolean isStreamQuery) throws SQLException {
Connection conn = null;
PreparedStatement stmt = null;
ResultSet rs = null;
int count = 0;
try {
//獲取數(shù)據(jù)庫(kù)連接
conn = getConnection();
if (isStreamQuery) {
//設(shè)置流式查詢參數(shù)
stmt = conn.prepareStatement(sql, ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_READ_ONLY);
stmt.setFetchSize(Integer.MIN_VALUE);
} else {
//普通查詢
stmt = conn.prepareStatement(sql);
}
//執(zhí)行查詢獲取結(jié)果
rs = stmt.executeQuery();
//遍歷結(jié)果
while(rs.next()){
System.out.println(rs.getString(1));
count++;
}
} catch (SQLException e) {
e.printStackTrace();
} finally {
close(stmt, rs, conn);
}
return count;
}
「PS」:上面的例子中通過(guò)參數(shù)isStreamQuery
來(lái)切換「流式查詢」與「普通查詢」,用于下面做測(cè)試對(duì)比。
三、性能測(cè)試
創(chuàng)建了一張測(cè)試表my_test
進(jìn)行測(cè)試,總數(shù)據(jù)量為27w
條,分別使用以下4個(gè)測(cè)試用例進(jìn)行測(cè)試:
- 大數(shù)據(jù)量普通查詢(27w條)
- 大數(shù)據(jù)量流式查詢(27w條)
- 小數(shù)據(jù)量普通查詢(10條)
- 小數(shù)據(jù)量流式查詢(10條)
3.1. 測(cè)試大數(shù)據(jù)量普通查詢
@Test
public void testCommonBigData() throws SQLException {
String sql = "select * from my_test";
testExecute(sql, false);
}
3.1.1. 查詢耗時(shí)
27w 數(shù)據(jù)量用時(shí) 38 秒
3.1.2. 內(nèi)存占用情況
使用將近 1G 內(nèi)存
3.2. 測(cè)試大數(shù)據(jù)量流式查詢
@Test
public void testStreamBigData() throws SQLException {
String sql = "select * from my_test";
testExecute(sql, true);
}
3.2.1. 查詢耗時(shí)
27w 數(shù)據(jù)量用時(shí) 37 秒
3.2.2. 內(nèi)存占用情況
由于是分批獲取,所以內(nèi)存在30-270m波動(dòng)
3.3. 測(cè)試小數(shù)據(jù)量普通查詢
@Test
public void testCommonSmallData() throws SQLException {
String sql = "select * from my_test limit 100000, 10";
testExecute(sql, false);
}
3.3.1. 查詢耗時(shí)
10 條數(shù)據(jù)量用時(shí) 1 秒
3.4. 測(cè)試小數(shù)據(jù)量流式查詢
@Test
public void testStreamSmallData() throws SQLException {
String sql = "select * from my_test limit 100000, 10";
testExecute(sql, true);
}
3.4.1. 查詢耗時(shí)
10 條數(shù)據(jù)量用時(shí) 1 秒
四、總結(jié)
MySQL 流式查詢對(duì)于內(nèi)存占用方面的優(yōu)化還是比較明顯的,但是對(duì)于查詢速度的影響較小,主要用于解決大數(shù)據(jù)量查詢時(shí)的內(nèi)存占用多的場(chǎng)景。
「DEMO地址」:https://github.com/zlt2000/mysql-stream-query
到此這篇關(guān)于MySQL中使用流式查詢避免數(shù)據(jù)OOM的文章就介紹到這了,更多相關(guān)MySQL 流式查詢內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
您可能感興趣的文章:- MySQL OOM(內(nèi)存溢出)的解決思路
- MySQL Slave 觸發(fā) oom-killer解決方法
- MySQL OOM 系列三 擺脫MySQL被Kill的厄運(yùn)
- MySQL OOM 系統(tǒng)二 OOM Killer
- MySQL OOM 系列一 Linux內(nèi)存分配