JDBC相比.NET SQL插入慢? (JDBC快4倍)(.NET SQL insert slo

2019-07-30 07:11发布

我做过.NET本地SQL客户端之间的一些测试,C#和相比,Java中的JDBC驱动程序,我被结果交口称赞,并想我一定是做了错误的.NET代码。 结果100万个插入表中的一个整数和文本字段我清空之前的每个尝试,运行的程序建立和释放他们的自我。

  • .NET 1个百万行1个交易与预处理语句把159秒。
  • 用java从微软1万行的JDBC驱动程序1个交易与准备statments了39秒。

JDBC是快4倍,为什么?

林有点目瞪口呆的是,.NET是如此的慢得多,真希望我犯了一个大错我的.NET代码。 因此,任何人有任何想法,什么是错,或者为什么.NET是这么慢得多然后JDBC?

.NET代码:

SqlConnection con = new SqlConnection("Data Source=localhost;Initial Catalog=testameep;User Id=sa;Password=********;");
        con.Open();
        String tempString = "";
        for(int i = 0;i<400;i++)
        {
            tempString += "insert into test1 values (@A" + i.ToString() + ",@B" + i.ToString() + " );";
        }

        SqlCommand com = new SqlCommand(tempString,con);
        for (int i = 0; i < 400; i++)
        {
            com.Parameters.Add("@A" + i.ToString(), SqlDbType.Int);
            com.Parameters.Add("@B" + i.ToString(), SqlDbType.NVarChar,4000);
        }
        com.Transaction = con.BeginTransaction();
        DateTime start = DateTime.Now;
        com.Prepare();
        for(int i=0;i<2500;i++)
        {
            for (int j=0;j<800;j+=2)
            {
                com.Parameters[j].Value = i;
                com.Parameters[j + 1].Value = "test";
            }
            com.ExecuteNonQuery();
        }
        com.Transaction.Commit();
        label1.Text = "Query took:" + (DateTime.Now - start).TotalMilliseconds + " ms";

java代码:

Class.forName( "com.microsoft.sqlserver.jdbc.SQLServerDriver" );
      String dbURL = "jdbc:sqlserver://localhost;databaseName=testameep;user=sa;password=*******";
      Connection dbCon = DriverManager.getConnection( dbURL, "sa" , "*******" );
      dbCon.setAutoCommit( false );

      String tempString = "";
      for(int i = 0;i<400;i++)
      {
          tempString += "insert into test1 values (?,?);";
      }
      System.out.println("Query start");
      long start = System.currentTimeMillis();
      PreparedStatement ps = dbCon.prepareStatement(tempString);
      for(int i=0;i<2500;i++)
      {
          for (int j=1;j<800;j+=2)
          {
              ps.setInt(j, i);
              ps.setString( j+1, "test");
          }
              ps.executeUpdate();
      //    ps.addBatch();
      }
          //This gave the same speed/result as using ps.executeUpdate();
      //ps.executeBatch();
      ps.close();
      dbCon.commit();
      double total = (double)(System.currentTimeMillis() - start) / 1000.0;
      System.out.printf("Query stop: %f seconds\n", total);

更新:SQL事件探查结果.NET:

--Creating the prepared statement .NET
 declare @p1 int
 set @p1=1
 exec sp_prepexec @p1 output,N'@A0 int,@B0 nvarchar(4000),@A1 int,@B1 nvarchar(4000),@A2    int,@B2 nvarchar(4000),@A3 int,@B3 nvarchar(4000),@A4 int,@B4 nvarchar(4000),@A5 int,...(continues)
select @p1

--Inserting rows .NET
exec sp_execute   1,@A0=4,@B0='test',@A1=4,@B1='test',@A2=4,@B2='test',@A3=4,@B3='test',@A4=4,@B4='test',@A5=4,@B5='test',@A6=4,@B6='test',@A7=4,@B7='test',@A8=4,@B8='test',@A9=4,...(continues)
...
2500 rows

SQL事件探查结果JDBC:

--Creating the prepared statment JDBC
declare @p1 int
set @p1=1
exec sp_prepexec @p1 output,N'@P0 int,@P1 nvarchar(4000),@P2 int,@P3 nvarchar(4000),@P4    int,@P5 nvarchar(4000),@P6 int,@P7 nvarchar(4000),@P8 int,@P9 nvarchar(4000),@P10 int,...  (continues)
select @p1

--Inserting rows JDBC
exec sp_execute   1,5,N'test',5,N'test',5,N'test',5,N'test',5,N'test',5,N'test',5,N'test',5,N'test',5,N'test',5,N'test',5,N'test',5,N'test',5,N'test',5,N'test',5,N'test',5,N'test',5...(continues)
...
2500 rows

Answer 1:

        com.ExecuteNonQuery();  
    }  
    com.Transaction.Commit();  

VS

     ps.addBatch(); 
  } 
  ps.executeBatch(); 
  ps.close();   
  dbCon.commit();   

执行的方法看起来diffent我



文章来源: .NET SQL insert slow compared to JDBC? (JDBC 4x faster)