/*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. POJ 2418 Hardwood Species(STL在map应用)

    职务地址:id=2418">POJ 2418 通过这个题查了大量资料..知道了非常多曾经不知道的东西. . .. 在代码中凝视说明吧. 代码例如以下: #include <ios ...

  2. Java Web整合开发(20) -- Hibernate入门

    Spring与Hibernate整合

  3. 给定一个set字符和一个正数k,找出所有该做set它可以由长度构成k该字符串集合 print-all-combinations-of-given-length

    // 给定一个set字符和一个正数k,找出所有该做set它可以由长度构成k该字符串集合 /* Input: set[] = {'a', 'b'}, k = 3 Output: aaa aab aba ...

  4. android详情请务必保持手机屏幕不锁屏

    今天做这个项目采用了非常有趣的东西,互联网搜索下一个轮廓,需求就是点击一个按钮来锁定屏幕让屏幕不亮. 几个测试.我们发现以下措辞如此简单, getWindow().addFlags(WindowMan ...

  5. jquery.validate.unobtrusive

    ASP.NET MVC Unobtrusive JavaScript 实现 onfocusout 验证, onfocusin 清除错误 在 ASP.NET MVC 中启用 Unobtrusive Ja ...

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

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

  7. 如何获得 oracle RAC 11g asm spfile S档

     方法一: [root@vmrac1 ~]# su - grid [grid@vmrac1 ~]$ sqlplus / as sysasm SQL*Plus: Release 11.2.0.3.0 ...

  8. Android 异步消息处理机制 让你在深入了解 Looper、Handler、Message之间的关系

    转载请注明出处:http://blog.csdn.net/lmj623565791/article/details/38377229 ,本文出自[张鸿洋的博客] 非常多人面试肯定都被问到过,请问And ...

  9. leetcode 名单 Insertion Sort List

    Insertion Sort List Total Accepted: 24444 Total Submissions: 96639My Submissions Sort a linked list ...

  10. discuz 7.2 faq.php sql注入了一些研究

    6.2号码(可能更早)上网本见exp,是一家discuz 7.2的sql注入漏洞 经过反复研究.最高在线人数exp它们存在于这些或那些问题,经过我自己的使用和变更摘要,使用的方法如以下: Discuz ...