我正在做一个简单的SQL查询来获取大量数据.查询的复杂性不是问题.执行大约需要200ms.但是,数据量似乎是个问题.
我们检索大约40k行.每行有8列,每行数据量约为几百千字节.比如说,我们为此查询共下载15megs.
让我难以置信的是:当我从一个基本的C#代码执行查询时,需要1分钟和44秒.但是当我从SSMS做到它需要10秒.当然我是从同一台机器上做的,而且我使用的是同一个数据库.我清楚地看到UI和实时填充的行.在10secs中,整个数据表已满.
我们尝试了:
它没有改变任何东西.有道理:它的读取速度很慢.不是查询(恕我直言).
这while(reader.Read())需要时间.而且,我们尝试了一个空的while循环.所以这不包括装箱/拆箱的东西或将结果放在内存中.
这是一个测试程序,我们弄清楚它是Read()需要时间:
using System;
using System.Data;
using System.Data.SqlClient;
using System.Threading.Tasks;
using System.Transactions;
namespace SqlPerfTest
{
class Program
{
const int GroupId = 1234;
static readonly DateTime DateBegin = new DateTime(2017, 6, 19, 0, 0, 0, DateTimeKind.Utc);
static readonly DateTime DateEnd = new DateTime(2017, 10, 20, 0, 0, 0, DateTimeKind.Utc);
const string ConnectionString = "CENSORED";
static void Main(string[] args)
{
TransactionOptions transactionOptions = new TransactionOptions
{
IsolationLevel = System.Transactions.IsolationLevel.ReadUncommitted
}; …Run Code Online (Sandbox Code Playgroud)