Wireshark 是一款出色的免费和开源 (GPL) 网络分析工具,在这种情况下可以发挥出色的作用。我运行了以下测试来查看到“正常”MySQL 服务器的“典型”JDBC 连接可能会产生多少流量。
我在我的测试服务器上的 MySQL (5.5.29-0ubuntu0.12.04.2) 中创建了一个名为 jdbctest 的表。
CREATE TABLE `jdbctest` (
`id` int(11) DEFAULT NULL,
`textcol` varchar(6) DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
我用 100,000 行表单填充它
id textcol
------ -------
1 ABCDEF
2 ABCDEF
3 ABCDEF
...
100000 ABCDEF
每个 id 值 4 个字节和每个 textcol 值 6 个字节,检索所有 100,000 行应该代表大约 1 MB 的数据。
我启动了 Wireshark,开始跟踪,并运行以下使用 mysql-connector-java-5.1.26 的 Java 代码:
import java.sql.*;
public class mysqlTestMain {
static Connection dbConnection = null;
public static void main(String[] args) {
try {
String myConnectionString = "";
myConnectionString =
"jdbc:mysql://192.168.1.3:3306/mytestdb";
dbConnection = DriverManager.getConnection(myConnectionString, "root", "whatever");
PreparedStatement stmt = dbConnection.prepareStatement("SELECT * FROM jdbctest");
ResultSet rs = stmt.executeQuery();
int i = 0;
int j = 0;
String s = "";
while (rs.next()) {
i++;
j = rs.getInt("id");
s = rs.getString("textcol");
}
System.out.println(String.format("Finished reading %d rows.", i));
rs.close();
stmt.close();
dbConnection.close();
} catch (SQLException ex) {
ex.printStackTrace();
}
}
}
控制台输出确认我已检索到所有 100,000 行。
查看Wireshark trace的总结,发现:
Packets captured: 1811
Avg. packet size: 992.708 bytes
Bytes: 1797795
按方向细分
packets bytes
------- -----
from me to server 636 36519
from server to me 1175 1761276
看来,为了检索我的 ~1 MB 数据,我从 MySQL 服务器收到了 1.72 MB 的总网络流量。下载开销约 72%(或包括双向流量的约 76%)肯定远不及您(速率 * 时间)计算所建议的约 5900% 开销。
我强烈怀疑 MySQL Workbench 报告的 ~1 MB/s 速率并不是整个时间的整体平均传输速率。在您的特定情况下确定开销的最佳方法是使用 Wireshark 之类的工具并自行测量。