Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/355.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/56.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java mysql插入太慢_Java_Mysql_Innodb - Fatal编程技术网

Java mysql插入太慢

Java mysql插入太慢,java,mysql,innodb,Java,Mysql,Innodb,这是mysql配置,一个非常简单的表,innodb,索引不多,但是有时候insert op太慢了,有人能告诉我为什么吗 public Long insertCoursewarePage(Env env, Long coursewareId, Integer pageType, String teacherId) throws SQLException { env.logTiming("beforeGetConnection"); Connection conn = MySql.g

这是mysql配置,一个非常简单的表,innodb,索引不多,但是有时候insert op太慢了,有人能告诉我为什么吗

public Long insertCoursewarePage(Env env, Long coursewareId, Integer pageType, String teacherId) throws SQLException {
    env.logTiming("beforeGetConnection");
    Connection conn = MySql.getInst_education_biz().getConnection();
    env.logTiming("afterGetConnection");
    PreparedStatement pstmt = null;
    ResultSet rs = null;
    String sql = "INSERT INTO `courseware_page`(`courseware_id`, `page_type`, `teacher_id`, `create_time`) VALUES(?,?,?,?)";
    try {
        pstmt = conn.prepareStatement(sql, Statement.RETURN_GENERATED_KEYS);
        env.logTiming("afterPrepareStatement");
        pstmt.setLong(1, coursewareId);
        pstmt.setInt(2, pageType);
        pstmt.setString(3, teacherId);
        pstmt.setLong(4, System.currentTimeMillis());
        pstmt.executeUpdate();
        env.logTiming("afterExecuteUpdate");
        rs = pstmt.getGeneratedKeys();
        env.logTiming("afterGetGeneratedKeys");
        if (rs.next()) {
            return rs.getLong(1);
        }
    } finally {
        MySql.closeConnection(rs, pstmt, conn);
    }
    return null;
}
/**
Here is my java code, use jdbc get a connection and I profiled it and the result is below here. 
beforeGetConnection=14
afterGetConnection=20
afterPrepareStatement=20
afterExecuteUpdate=5344
afterGetGeneratedKeys=5345
mysql server is on a dell r820, SSD, 64 core cpu, 320G memory, it seems no any problem.so could anybody tell me why
**/

这可能是因为每次要插入时,都会使用
connection conn=MySql.getInst_education_biz().getConnection()打开连接
然后执行查询,然后关闭它
MySql.closeConnection(rs、pstmt、conn)


您的插入时间可能很短,但为每个查询打开和关闭连接的成本很高。如果您的数据库经常被查询,您可能希望使用一个连接池,该连接池只会在需要时“暂停”连接并“恢复”连接。以下是使用它的一些原因:

[3]:[4]:[5]:请将所有相关代码以文本形式发布。代码编辑器就是为了这个目的而存在的。很抱歉,我没有回复您的权限,代码img再次出现在这里,删除图像并将所有相关代码作为文本包含在这里。@反斜杠代码在这里谢谢您n0xew,但我确信与get Connection没有任何关系。因为我记录了执行时间,所以从线程开始到
env.logTiming(“beforeGetConnection”)该程序花费14毫秒,当它运行到
Connection conn=MySql.getInst_education_biz().getConnection()时它是20毫秒,这意味着获得连接只需要6毫秒,您可以在代码下面看到其他成本。大部分时间花费在
pstmt.executeUpdate()上我在mysql server cli上尝试了这个sql,结果也是一样的。我的mysql服务器硬件配置也在我的java代码下面的注释中。我很确定数据库在连接关闭后会释放一些资源,从而花更多时间处理可能传入的连接和查询。这当然取决于您的查询频率。无论如何,已将我的答案更新为使用
连接池
,这只能在出现其他问题时帮助您。我已经有了连接池,我说上次原始返回连接到连接池对不起,没有看到您的答复。另外,我无法在您的屏幕截图上看到,每个查询的间隔时间是多少?