"流"从SQL Server中的表中读取超过1000万行 [英] "Streaming" read of over 10 million rows from a table in SQL Server

查看:310
本文介绍了"流"从SQL Server中的表中读取超过1000万行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

什么是从表中读出数以百万计的记录(在SQL Server 2012中,BI实例),以流媒体的方式(如SQL Server Management Studio中呢)?


$ B $的最佳策略b

我需要在本地缓存这些记录(C#控制台应用程序)进行进一步的处理。



更新 -
示例代码与SqlDataReader的



<$ P $工作p> 使用系统;
使用System.Collections.Generic;
使用System.Linq的;
使用System.Text;使用System.Threading.Tasks
;
使用System.Data这;
使用System.Data.SqlClient的;
使用的System.Threading;


命名空间ReadMillionsOfRows
{
类节目
{
静态的ManualResetEvent做=新的ManualResetEvent(假);


静态无效的主要(字串[] args)
{

处理();
done.WaitOne();
}

公共静态异步任务处理()
{
串CONNSTRING = @服务器=;数据库=;用户ID =;密码=;异步处理=真正;
字符串SQL =从tab_abc选择*;使用(SqlConnection的康恩=新的SqlConnection(CONNSTRING))
{


等待conn.OpenAsync();使用
(通讯的SqlCommand =新的SqlCommand(SQL))
{
comm.Connection =康恩;
comm.CommandType = CommandType.Text;使用(SqlDataReader的读卡器=等待comm.ExecuteReaderAsync())
{
,而(等待reader.ReadAsync())
{
//过程

在这里
}
}
}
}

done.Set();
}

}
}


解决方案

使用一个SqlDataReader是只进和快。而这是在阅读它的范围将只持有至创纪录的参考。


What is the best strategy to read millions of records from a table (in SQL Server 2012, BI instance), in a streaming fashion (like SQL Server Management Studio does)?

I need to cache these records locally (C# console application) for further processing.

Update - Sample code that works with SqlDataReader

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data;
using System.Data.SqlClient;
using System.Threading;


namespace ReadMillionsOfRows
{
    class Program
    {
        static ManualResetEvent done = new ManualResetEvent(false);


        static void Main(string[] args)
        {

          Process();
          done.WaitOne();
        }

        public static async Task Process()
        {
            string connString = @"Server=;Database=;User Id=;Password=;Asynchronous Processing=True";
            string sql = "Select * from tab_abc";

            using (SqlConnection conn = new SqlConnection(connString))
            {
                await conn.OpenAsync();
                using (SqlCommand comm = new SqlCommand(sql))
                {
                    comm.Connection = conn;
                    comm.CommandType = CommandType.Text;

                    using (SqlDataReader reader = await comm.ExecuteReaderAsync())
                    {
                        while (await reader.ReadAsync())
                        {
                            //process it here
                        }
                    }
                }
            }

            done.Set();
        }

    }
}

解决方案

Use a SqlDataReader it is forward only and fast. It will only hold a reference to a record while it is in the scope of reading it.

这篇关于&QUOT;流&QUOT;从SQL Server中的表中读取超过1000万行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆