简体   繁体   English

如何对 SQL Server 运行多个查询?

[英]How to run multiple queries against SQL Server?

I have read and implemented several different versions of Microsofts suggested methods for querying a SQL Server database.我已经阅读并实现了几个不同版本的 Microsoft 建议的查询 SQL Server 数据库的方法。 In all that I have read, each query is surrounded by a using statement, eg In some method DoQuery :在我读过的所有内容中,每个查询都被 using 语句包围,例如在某些方法DoQuery

List<List<string>> DoQuery(string cStr, string query)  
{
    using(SqlConnection c = new SqlConnection(cStr)) 
    {
        c.Open();

        using(SqlCommand cmd = new SqlCommand(queryStr, c)) 
        {
            using(SqlDataReader reader = cmd.ExecuteReader()) 
            {
                while (reader.Read() ) 
                {
                     ...
                     //read columns and put into list to return
                }

           // close all of the using blocks
           }
        }
    }

    // return the list of rows containing the list of column values.
}

I need to run this code several hundreds of times for different query strings against the same database.我需要针对同一数据库的不同查询字符串运行此代码数百次。 It seems that creating a new connection each time would be inefficient and dropping it each time wasteful.似乎每次创建一个新连接效率低下,每次丢弃它都是浪费。

How should I structure this so that it is efficient?我应该如何构建它以使其有效? When I tried not using a using block and passing the connection into the DoQuery method, I got messages about the connection had not been closed.当我尝试不使用 using 块并将连接传递到DoQuery方法时,我收到有关连接尚未关闭的消息。 If I closed it after the query, then I got messages about it wasn't open.如果我在查询后关闭它,那么我会收到有关它未打开的消息。

I'm also trying to improve this because I keep getting somewhat random我也在努力改进这一点,因为我总是变得有些随机

IOException: Unable to read data from the transport connection: Operation on non-blocking socket would block. IOException: 无法从传输连接读取数据:非阻塞套接字上的操作将阻塞。

I'm the only user of the database at this time and I'm not doing anything in multiple threads or async, etc. Just looping through query strings and running DoQuery on them.目前我是数据库的唯一用户,我没有在多线程或异步等中做任何事情。只是循环查询字符串并在它们上运行DoQuery

Could my structure be part of that problem, ie not releasing the resources fast enough and thereby seeing the connection blocked?我的结构是否可能是该问题的一部分,即没有足够快地释放资源,从而导致连接被阻塞?

I'm stuck here on efficiency and this blocking problem.我被困在效率和这个阻塞问题上。 Thanks in advance.提前致谢。

As it turns out, the query structure was fine and the queries were fine.事实证明,查询结构很好,查询也很好。 The problem was that I had an 'order by X desc' on each query and that column was not indexed.问题是我在每个查询上都有一个“按 X 描述排序”,并且该列没有编入索引。 This caused a full table scan to order the rows even if only returning 2. The table has about 3 million rows and I thought it could handle that better than it does.这会导致全表扫描对行进行排序,即使只返回 2。该表有大约 300 万行,我认为它可以比它更好地处理。 It timed out with 360 second connection timeout!它超时了 360 秒连接超时! I indexed the column and no more 'blocking' nonsense, which BTW, is a horrible message to return when it was actually a timeout.我为该列建立了索引,不再有“阻塞”废话,顺便说一句,当它实际上是超时时,这是一条可怕的消息。 The queries now run fine if I index every column that appears in a where clause.如果我索引出现在 where 子句中的每一列,查询现在可以正常运行。

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

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