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.

SQL data reader reading data performance test,布布扣,bubuko.com

时间: 2024-10-05 04:33:12

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'系列二:reset slave

reset slave会清除从库的所有复制信息.一般应用场景:如切换为不同的Master, 主从重做等: 1. 命令在slave上执行,执行前一定要stop slave. 2. 执行reset slave后,会清除复制相关的所有信息,包括:master.info, relay-log.info, 及无条件删除所有的中继日志(relay logs). 注意是无条件的,也就是不管理你Slave SQL线程是否把所有的relay log重放完了. 3. 注意,stop slave后,先保存show s

Data Dictionary and Dynamic Performance Views(数据字典和动态性能视图)

Overview of the Data Dictionary Because Oracle Database stores data dictionary data in tables, just like other data, users can query the data with SQL. Contents of the Data Dictionary The data dictionary consists of the following types of objects: Ba

sql基础之DDL(Data Definition Languages)

好久没写SQL语句了,复习一下. DDL数据定义语言,DDL定义了不同的数据段.数据库.表.列.索引等数据库对象的定义.常用的DDL语句包括create.drop.alter等等. 登录数据:mysql -uroot -p sql 命令一般以英文分号或者\g结束,注意这个\不是/. 1.查看数据库 show databases;注意这个后面的s. 2.创建数据库create database wangking;注意这里没有s. 3.选择数据库use siyecao;列出数据库中的表show ta

mysql从库Last_IO_Error: Got fatal error 1236 from master when reading data from binary log: 'Could not find first log file name in binary log index file'报错处理

年后回来查看mysql运行状况与备份情况,登录mysql从库查看主从同步状态 1 mysql> show slave status\G; 2 *************************** 1. row *************************** 3 Slave_IO_State: 4 Master_Host: 101.200.*.* 5 Master_User: backup 6 Master_Port: 3306 7 Connect_Retry: 60 8 Master_

Google Protobuf Reader - Read Data

新项目需要 用 Java 读取 protobuf-net-data encode之后的数据,实在是个让人蛋疼的任务. protobuf-net-data 把 查询结果封装成 DataTable,用法自己参考原文. 这里说说我要做的事情 Understand Protobuf Encoding Decoding Java ResultSet Protobuf how to encoding: Google Doc a protocol buffer message is a series of k

OpenTSDB-Querying or Reading Data

Querying or Reading Data OpenTSDB offers a number of means to extract data such as CLI tools, an HTTP API and as a GnuPlot graph. Querying with OpenTSDB's tag based system can be a bit tricky so read through this document and checkout the following p

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

主库添加log-bin-index 参数后,从库报这个错误: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 next l

SQL Server 2008 R2——Data Types

In SQL Server, each column, local variable, expression, and parameter has a related data type. A data type is an attribute that specifies the type of data that the object can hold: integer data, character data, monetary data, date and time data, bina

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 *************************** 1. row ***************************                Slave_IO_State:                   Master_Host: 88.88.88.88                   Master_User: replicate