SQL data reader reading data performance test
/*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的更多相关文章
- 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 - ...
- 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 ...
- 【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 ...
- 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 ************************ ...
- 浅析基于微软SQL Server 2012 Parallel Data Warehouse的大数据解决方案
作者 王枫发布于2014年2月19日 综述 随着越来越多的组织的数据从GB.TB级迈向PB级,标志着整个社会的信息化水平正在迈入新的时代 – 大数据时代.对海量数据的处理.分析能力,日益成为组织在这个 ...
- 转:浅析基于微软SQL Server 2012 Parallel Data Warehouse的大数据解决方案
综述 随着越来越多的组织的数据从GB.TB级迈向PB级,标志着整个社会的信息化水平正在迈入新的时代 – 大数据时代.对海量数据的处理.分析能力,日益成为组织在这个时代决胜未来的关键因素,而基于大数据的 ...
- 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 *************************** ...
- OpenTSDB-Querying or Reading Data
Querying or Reading Data OpenTSDB offers a number of means to extract data such as CLI tools, an HTT ...
- 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 ...
随机推荐
- .Net程序猿乐Android发展---(10)框架布局FrameLayout
帧布局FrameLayout中全部的控件都在界面的左上側,后绘制的空间会覆盖之前的控件.布局内控件以层叠方式显示,用在游戏开发方面可能多些. 1.层叠展示 以下这个样例 ...
- use grep & awk to get ed2k links in the webpage
in cygwin grep "href=\"ed2k" c.htm |awk -F '\"' '{print $2}' >ed2k.txt
- JavaScript运行命令
前言 动人js一段时间,我认为事情仅仅是一个很肤浅的理解.是非常欠缺的.所以開始使用博客来对这一部分的知识做个慢慢的记录和积累. 相信积少成多,慢慢的将这一部分的知识攻克! 第一篇记录的不是相关的应用 ...
- Gradle sourceCompatibility has no effect to subprojects(转)
I have Java 6 and 7 installed on my machine. Gradle uses 1.7 (checked using gradle -v). But I need t ...
- lodoop打印控制具体解释
注意:这就需要引进的打印控制(我上传Demo同时): install_lodop32.exe install_lodop64.exe LodopFuncs.js jquery-1.10.0.min.j ...
- 四、Linux/UNIX操作命令积累【chmod、chown、tail】
正在使用Linux/UNIX下一个.经常使用文本界面来设置系统或操作系统,笔者也是在指挥这方面工作的过程中不断的接触.因此,为了此特酝酿.准备.開始了本文的编写.本文主要记录自己平时遇到的一些Linu ...
- 软测试综述——PV操作
在操作系统中,进程之间常常会存在相互排斥(都须要共享独占性资源时)和同步(完毕异步的两个进程的协作)两种关系.而信号量和PV操作完美有效的处理了这两种情况. 相互排斥:就好比过独木桥,一 ...
- Linux内核分析(二)----内核模块简介|简单内核模块实现
原文:Linux内核分析(二)----内核模块简介|简单内核模块实现 Linux内核分析(二) 昨天我们开始了内核的分析,网上有很多人是用用源码直接分析,这样造成的问题是,大家觉得很枯燥很难理解,从某 ...
- MoveWindow and SetWindowPos
转自:http://blog.sina.com.cn/s/blog_82c346de0100u7kq.html MoveWindow and SetWindowPos (2011-09-14 15:5 ...
- windows平台搭建lighttpd+php+sqlite
(一)php 1. 下载及安装 http://www.appservnetwork.com/ 从上面的网址下载appserv-win32-2.5.10并安装,在安装的时候,仅仅选择安装php. 由于, ...