简体   繁体   English

单元测试Sql的逻辑读取

[英]Unit testing Sql for logical reads

We have unit tests for our data access layer. 我们对数据访问层进行了单元测试。 This has helped to spot sql syntax errors. 这有助于发现sql语法错误。
Now that we have these tests I would like to take it one step further. 现在我们已经进行了这些测试,我想将它进一步向前。 I would like to run the unit tests and find sql that has a high number of logical reads, Automatically.(to find sql in need of tuning) 我想运行单元测试并自动查找具有大量逻辑读取的sql(以查找需要调整的sql)

Adding "set statistics IO" to the sql is not difficult. 向SQL添加“ set statistics IO”并不困难。 What I am looking for is a stored procedure where I send it the db and it returns a string containing the logical read information about the sql. 我正在寻找的是一个存储过程,在其中向它发送db,它返回一个包含有关sql的逻辑读取信息的字符串。 I am working in Sybase, but if you know of stored proc/etc that does this in a different DB, that would still be a huge help. 我在Sybase中工作,但是如果您知道在其他数据库中执行此操作的存储的proc / etc,那仍将是巨大的帮助。

Thanks! 谢谢!

This is not possible with a TSQL procedure, but the output can be captured in .net by subscribing to the InfoMessage event on the SqlConnection object. 这对于TSQL过程是不可能的,但是可以通过订阅SqlConnection对象上的InfoMessage事件,在.net中捕获输出。

  ...
  using (var connection = new SqlConnection("connectionstring")
  using (var command = new SqlCommand("SET STATISTICS IO ON"))
  {
      connection.Open();
      connection.InfoMessage += OnInfoMessage;

      using (var reader = command.ExecuteReader())
      {
          ....
      }
  }
  ...

  private static void OnInfoMessage(object sender, SqlInfoMessageEventArgs args)
  {
      foreach (SqlError err in args.Errors)
      {
           Console.WriteLine(err.Message);
      }
  }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM