/*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. 【C++基金会 06】explictkeyword

    C++提供keywordexplicit,你应该不能阻止的转换构造隐式转换发生的同意.声明explicit的构造不能在一个隐式转换使用. 1.演示样例 我们先来看一段演示样例代码: class A { ...

  2. MFC 盾webBrowser打开弹出的页面

    void CansDlg::NewWindow3Explorer1(LPDISPATCH* ppDisp, BOOL* Cancel, unsigned long dwFlags, LPCTSTR b ...

  3. android键盘锁定问题

    android经常使用KeyguardLock解锁.但需要使用后打电话reenableKeyguard()锁定被解除.否则,会导致其他进程无法锁定屏幕,使用相同的WakeLock唤醒屏幕后还需要使用r ...

  4. 用于主题检测的临时日志(18506589-369d-4505-a204-3678db17eae5 - 3bfe001a-32de-4114-a6b4-4005b770f6d7)

    这是一个未删除的临时日志.请手动删除它.(252f1b1e-5ce3-42a8-95da-bc0acbd4f637 - 3bfe001a-32de-4114-a6b4-4005b770f6d7)

  5. leaflet开源地图库源码 浏览器&移动设备判断(browser.js)备份

    <script> var isIe = !-[1,]; // alert('ie9 之前'+isIe); var ie = 'ActiveXObject' in window; //ale ...

  6. java验证手机号码是否合法

    公司开发新功能须要验证手机号码,遂自己写了个出来,暂仅仅支持中国大陆手机号验证.如有不妥之处,还望大家指教,感激不尽! /** * 验证是否是正确合法的手机号码 * * @param telephon ...

  7. Event Sourcing - ENode(三)

    接上一篇 http://www.cnblogs.com/dopeter/p/4903328.html 老板昨天在第二篇介绍中回复代码和文字无法一一对应.为了更好的让老板为大家解惑,把第二篇最后的猜测的 ...

  8. Caused by: java.lang.ClassNotFoundException: javax.transaction.TransactionManager

    1.错误叙述性说明 usage: java org.apache.catalina.startup.Catalina [ -config {pathname} ] [ -nonaming ] { -h ...

  9. CodeIgniter入门——HelloWorld

    原文:CodeIgniter入门--HelloWorld CodeIgniter(CI)是一套给PHP网站开发者使用的应用程序开发框架和工具包. 初次接触,来一个HelloWorld~~~ ^_^ 准 ...

  10. sql多表查询之一:Where 和 On的秘密

    原文 sql多表查询之一:Where 和 On的秘密 对于还在SQL初级阶段的朋友来说,sql多表查询问题是一个比较有趣也容易出错的技术.什么时候会用到sql多表查询呢?是在两张或两张以上表单中通过某 ...