/*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. Linux 多学习过程

    1Linux流程概述 过程是,一旦运行过程中的程序,他和程序本质上的区别.程序是静态的,他奉命收集指令存储在磁盘上. 进程是动态的概念.他是执行者的程序,包括进程的动态创建.调度和消亡,是Linux的 ...

  2. 默认情况下安装的应用程序C盘后提示权限不足,当你开始介意。。。

    最近,不少用户抱怨的经销商.正在使用win 7我们的计算机系统上安装软件后,提示权限不够开放系统,无法启动软件. 在xp该系统是没有问题的.原因是,我们会选择在默认安装路径系统C-disk.和win ...

  3. mumu血压计母亲节“拼团”盛大开幕,百度和厂家创造一个双赢的局面

        在BAT的互联网时代.似乎不论什么新兴产品和服务都不能脱离BAT的支持,作为中国互联网体量最庞大的三家企业.BAT代表的是资源优势.用户优势.品牌优势.因此,一旦脱离BAT的支持,想迅速做大无 ...

  4. WCF分布式事务(EF)

    才说分布式事务,首先,了解一下什么是交易. 事务有四个特性:ACID A是Atomicity,原子性.一个事务往往涉及到很多的子操作,原子性则保证这些子操作要么都做,要么都不做,而不至于出现事务的部分 ...

  5. SQL Server 2005中的CHECKSUM功能

    原文:SQL Server 2005中的CHECKSUM功能 转自此处 页面 checksum 是SQL2005的新功能,提供了一种比残缺页检测强大的机制检测IO方面的损坏.以下是详细描述: 页面 C ...

  6. lua三底漆:lua转让c/c++库(动态链接模式)

    dll按功能luaL_openlib出口,然后lua使用package.loadlib导入库函数,基本就是这么个过程,以下上代码来说明一切. #include "stdafx.h" ...

  7. JSON-C结构简介、使用

    官方站点介绍http://www.json.org JSON (JavaScript Object Notation) is a lightweight data-interchange format ...

  8. android 推断应用程序是系统程序还是用户程序

    直接上代码: AppInfo.java <span style="font-size:18px;">package com.example.packages; publ ...

  9. 在win8.1 64位系统+cocos2d-x2.2.3下搭建android交叉编译环境

    搭建前须要下载的软件包(默认已搭建好cocos2d-x而且可在VS上执行,本人VS版本号为2013): 1:java 下载地址:http://www.java.com/zh_CN/download/m ...

  10. Java的结构之美【1】——构造对象

    当我们遇到多个构造器參数的时候可能会想到用构件器,代码例如以下: /** * 构建器 * @author 阳光小强 * */ public class Lunch { private String c ...