zoukankan      html  css  js  c++  java
  • 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!


    版权声明:本文博客原创文章,博客,未经同意,不得转载。

  • 相关阅读:
    IOS添加pch预编译文件
    UITableview控件基本使用
    两种单例模式的写法
    提交app Store审核时,Missing 64-bit support问题的解决办法
    IOS开发NSString与int和float的相互转换以及字符串拼接、NSString、NSData、char* 类型之间的转换
    获取UITableView每行中不同的UITextField输入的内容(例如修改登陆密码)
    UITextField常用属性归纳:文本框样式、文字样式、键盘样式、左右视图样式、清除按钮设置等,iosuitextfield
    UITabBarController(标签栏控制器)
    iOS基础控件UINavigationController中的传值
    IOS开发之多线程
  • 原文地址:https://www.cnblogs.com/mengfanrong/p/4720476.html
Copyright © 2011-2022 走看看