/*Author: Jiangong SUN*/

As I've manipulated a lot of data using SQL data reader in recent project. And people says it's not good to access the data by column name.

So I've made an performance test in reading data from SQL data reader.

Firstly, I've created a table with different data types, like int, varchar, date time etc.

CREATE TABLE UserInformation(Id BIGINT, FirstName NVARCHAR(255), LastName NVARCHAR(255), ValidDate DATETIME, Identification UNIQUEIDENTIFIER)

Then, I've filled the table with 9024728 lines data.

Why is it the exact number? It's because the sql server management studio crashes after 9024728 lines' insertion. :-)

Then, I'll use 3 methods to read the 9 millions lines data.

Method 1: Get data by column index

public void DataReaderGetDataByColumnIndex()
{
using (_dbConnection)
{
var sqlCommand = new SqlCommand(_commandText, _dbConnection); _dbConnection.Open(); SqlDataReader reader = sqlCommand.ExecuteReader(); var user = new UserInformationEntity(); _GetByIndexTime.Start();
while (reader.Read())
{
user.Id = reader.GetInt64(0);
user.FirstName = reader.GetString(1);
user.LastName = reader.GetString(2);
user.ValidDate = reader.GetDateTime(3);
user.Identification = reader.GetGuid(4);
}
_GetByIndexTime.Stop();
Console.WriteLine(string.Format("GetByIndexTime total time:{0}", _GetByIndexTime.Elapsed));
_dbConnection.Close();
}
}

Method 2: Get data by column name

public void DataReaderGetDataByColumnName()
{
using (_dbConnection)
{
var sqlCommand = new SqlCommand(_commandText, _dbConnection); _dbConnection.Open(); SqlDataReader reader = sqlCommand.ExecuteReader(); var user = new UserInformationEntity(); _GetByNameTime.Start();
while (reader.Read())
{
user.Id = Convert.ToInt64(reader["Id"]);
user.FirstName = reader["FirstName"].ToString();
user.LastName = reader["LastName"].ToString();
user.ValidDate = Convert.ToDateTime(reader["ValidDate"]);
user.Identification = new Guid(reader["Identification"].ToString());
}
_GetByNameTime.Stop();
Console.WriteLine(string.Format("GetByNameTime total time:{0}", _GetByNameTime.Elapsed));
_dbConnection.Close();
}
}

Method 3: Get column ordinal by column name, then Get data by column ordinal

public void DataReaderGetColumnIndexByColumnNameThenGetData()
{
using (_dbConnection)
{
var sqlCommand = new SqlCommand(_commandText, _dbConnection); _dbConnection.Open(); SqlDataReader reader = sqlCommand.ExecuteReader(); var user = new UserInformationEntity(); var id = reader.GetOrdinal("Id");
var firstName = reader.GetOrdinal("FirstName");
var lastName = reader.GetOrdinal("LastName");
var validDate = reader.GetOrdinal("ValidDate");
var identification = reader.GetOrdinal("Identification"); _GetByNameThenIndexTime.Start();
while (reader.Read())
{
user.Id = reader.GetInt64(id);
user.FirstName = reader.GetString(firstName);
user.LastName = reader.GetString(lastName);
user.ValidDate = reader.GetDateTime(validDate);
user.Identification = reader.GetGuid(identification);
}
_GetByNameThenIndexTime.Stop();
Console.WriteLine(string.Format("GetByNameThenIndexTime total time:{0}", _GetByNameThenIndexTime.Elapsed));
_dbConnection.Close();
}
}

When I run the program to get the execution time:

You can see that Method1 and Method3 has almost the same result, and Method2 are about 3 times longer.

So the prefered approach will be the third.

Enjoy coding!

版权声明:本文博客原创文章,博客,未经同意,不得转载。

SQL data reader reading data performance test的更多相关文章

  1. Got fatal error 1236 from master when reading data from binary log: 'Could not find first log file name in binary log index file'系列三:重置主从同步

    1:停止slave服务器的主从同步 stop slave; 2:对Master数据库加锁 flush tables with read lock; 3:备份Master上的数据 mysqldump - ...

  2. Got fatal error 1236 from master when reading data from binary log: 'Could not find first log file name in binary log index file'

    setup slave from backup i got error Got fatal error 1236 from master when reading data from binary l ...

  3. 【MySQL】MySQL同步报错-> Last_IO_Error: Got fatal error 1236 from master when reading data from binary log

    这个报错网上搜索了一下,大部分是由于MySQL意外关闭或强制重启造成的binlog文件事务点读取异常造成的主从同步报错 Last_IO_Error: Got fatal error 1236 from ...

  4. mysql 主从 Got fatal error 1236 from master when reading data from binary log: 'Could not find first 错误

    本地MySQL环境,是两台MySQL做M-M复制.今天发现错误信息: mysql 5.5.28-log> show slave status\G ************************ ...

  5. 浅析基于微软SQL Server 2012 Parallel Data Warehouse的大数据解决方案

    作者 王枫发布于2014年2月19日 综述 随着越来越多的组织的数据从GB.TB级迈向PB级,标志着整个社会的信息化水平正在迈入新的时代 – 大数据时代.对海量数据的处理.分析能力,日益成为组织在这个 ...

  6. 转:浅析基于微软SQL Server 2012 Parallel Data Warehouse的大数据解决方案

    综述 随着越来越多的组织的数据从GB.TB级迈向PB级,标志着整个社会的信息化水平正在迈入新的时代 – 大数据时代.对海量数据的处理.分析能力,日益成为组织在这个时代决胜未来的关键因素,而基于大数据的 ...

  7. Last_IO_Errno: 1236 Last_IO_Error: Got fatal error 1236 from master when reading data from binary lo

    mysql> show slave status\G *************************** 1. row ***************************         ...

  8. OpenTSDB-Querying or Reading Data

    Querying or Reading Data OpenTSDB offers a number of means to extract data such as CLI tools, an HTT ...

  9. Got fatal error 1236 from master when reading data from binary log: 'Could not find first log file name in binary log index file'系列一:

    从库报这个错误:Got fatal error 1236 from master when reading data from binary log: 'Could not find first lo ...

随机推荐

  1. 面向对象的软件project——面向对象分析

    为了解决软件危机.一些IT前辈国产软件project这个词汇,软件project它被引入到整个软件开发过程的维护. 软件project从程序的设计角度能够分为两类.一类是面向结构的软件project. ...

  2. svn import usage and not import .so file

    svn import usage: svn import PATH URL 可以不检查仓库来实现.入文件或目录 for example: svn import /home/scott/xxx svn: ...

  3. SQL Server 2005------函数

    原文:SQL Server 2005------函数 SQL Server 2005支持用户自定义函数和内置系统函数,根据返回值类型又分为标量函数和表值函数. 1.标量函数标量函数:返回单个数据值,返 ...

  4. 【LeetCode从零单排】No189 .Rotate Array

    称号 Rotate an array of n elements to the right by k steps. For example, with n = 7 and k = 3, the arr ...

  5. 工厂模式IDAL具体解释

    IDAL 一. IDAL主要功能: 1.这全然是"工厂模式"的一部分实现而已 2.这是一组接口类,当中包含了每一个要公开的数据訪问方法.为每一个数据库产品单独编写的DAL(数据訪问 ...

  6. DFT 展开式和 FFT推导

    C语言的FFT //---------------------------------------------------------------------------------- //----- ...

  7. ASP.NET验证控件

    在此过程中房间的收费制度时,.为了验证文本框是否为空.用户存在.合法等等.我们都要单独写代码.学习了ASP.NET的验证控件,省了非常多事. ASP.NET能够轻松实现对用户输入的验证. 让我们好好回 ...

  8. jquery-ui-bootstrap动态添加和删除标签页封装【效果更炫】

    1.效果图 2.导入js和css <link rel="stylesheet" href="css/bootstrap/css/bootstrap.min.css& ...

  9. requestWindowFeature()应用

    我们在开发程序是常常会须要软件全屏显示.自己定义标题(使用button等控件)和其它的需求,今天这一讲就是怎样控制Android应用程序的窗口显示. 首先介绍一个重要方法那就是requestWindo ...

  10. Java 反射 想

    所谓反射.是指在执行时状态中,获取类中的属性和方法.以及调用当中的方法的一种机制. 这样的机制的作用在于获取执行时才知道的类(Class)及当中的属性(Field).方法(Method)以及调用当中的 ...