原创

生产事故:mysql查询超时

1.问题说明

收到反馈app不能用,查看服务器日志,发现查询数据时候报读取超时

2024-11-06 21:36:06.749 ERROR 12319 --- [http-nio-8080-exec-1] com.alibaba.druid.pool.DruidDataSource   : {conn-10347} discard

com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: Communications link failure

The last packet successfully received from the server was 10,003 milliseconds ago.  The last packet sent successfully to the server was 10,002 milliseconds ago.
    at sun.reflect.GeneratedConstructorAccessor181.newInstance(Unknown Source) ~[na:na]
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45) ~[na:1.8.0_232]
    at java.lang.reflect.Constructor.newInstance(Constructor.java:423) ~[na:1.8.0_232]
    at com.mysql.jdbc.Util.handleNewInstance(Util.java:425) ~[mysql-connector-java-5.1.47.jar:5.1.47]
    ......
    at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61) [tomcat-util.jar:8.5.50]
    at java.lang.Thread.run(Thread.java:748) [na:1.8.0_232]
Caused by: java.net.SocketTimeoutException: Read timed out
    at java.net.SocketInputStream.socketRead0(Native Method) ~[na:1.8.0_232]
    at java.net.SocketInputStream.socketRead(SocketInputStream.java:116) ~[na:1.8.0_232]
    at java.net.SocketInputStream.read(SocketInputStream.java:171) ~[na:1.8.0_232]
    at java.net.SocketInputStream.read(SocketInputStream.java:141) ~[na:1.8.0_232]
    at com.mysql.jdbc.util.ReadAheadInputStream.fill(ReadAheadInputStream.java:101) ~[mysql-connector-java-5.1.47.jar:5.1.47]
    at com.mysql.jdbc.util.ReadAheadInputStream.readFromUnderlyingStreamIfNecessary(ReadAheadInputStream.java:144) ~[mysql-connector-java-5.1.47.jar:5.1.47]
    at com.mysql.jdbc.util.ReadAheadInputStream.read(ReadAheadInputStream.java:174) ~[mysql-connector-java-5.1.47.jar:5.1.47]
    at com.mysql.jdbc.MysqlIO.readFully(MysqlIO.java:3011) ~[mysql-connector-java-5.1.47.jar:5.1.47]
    at com.mysql.jdbc.MysqlIO.reuseAndReadPacket(MysqlIO.java:3472) ~[mysql-connector-java-5.1.47.jar:5.1.47]
    ... 126 common frames omitted

2.解决办法

先测试下这个sql是否查询成功,查询耗时发现超过10秒,增加表索引,重启服务,问题解决

#先查询表 where字段列是否有索引
SHOW INDEX FROM order_table;


#增加索引
CREATE INDEX idx_order_no ON order_table (order_no);
正文到此结束