SQL Query - Slow execution time in C#, fast execution in Toad

隐身守侯 提交于 2021-01-29 08:56:19

问题


I want to use Apache Ignite as a caching layer, and I am trying to load a table into the cache. I have a .NET console project where I use Oracle.ManagedDataAccess.Client to go through every row in TABLE.

TABLE has 500,000 entries and its size is around 300MB.

Executing the C# code takes ~50 minutes. However, executing SELECT * FROM TABLE takes about 250 msecs to load 500 entries in Toad and, at most, 65 seconds for all 500,000 entries.

Program.cs:

class Program
{
    static void Main(string[] args)
    {

        // Create Connection
        OracleConnection con = new OracleConnection();

        // Create ConnectionString using Builder
        OracleConnectionStringBuilder ocsb = new OracleConnectionStringBuilder();
        ocsb.Password = "PASSWORD";
        ocsb.UserID = "USER";
        ocsb.DataSource = "URL";

        // Connect 
        con.ConnectionString = ocsb.ConnectionString;
        con.Open();
        Console.WriteLine();
        Console.WriteLine(">>> Connection Established");

        // Execute a SQL SELECT
        OracleCommand cmd = con.CreateCommand();
        cmd.CommandText = "select * from TABLE";
        OracleDataReader reader = cmd.ExecuteReader();

        // Cache configuration
        var cfg = new IgniteConfiguration
        {
            PeerAssemblyLoadingMode = Apache.Ignite.Core.Deployment.PeerAssemblyLoadingMode.CurrentAppDomain,
            CacheConfiguration = new[]
            {
                new CacheConfiguration
                {
                    Name = "cacheName",
                    Backups = 1,
                    QueryEntities = new[]
                    {
                        new QueryEntity(typeof(int), typeof(Table))
                    }
                }
            }
        }

        IIgnite ignite = Ignition.Start(cfg);
        ICache<int, Table> cache = ignite.GetOrCreateCache<int, Table>("cacheName");


        using (var stmr = ignite.GetDataStreamer<int, Table>("cacheName"))
        {
            // Add entries to cacheName
            while (reader.Read())
            {
                Table table = new Table();
                table.SetObject(reader);
                stmr.AddData(table.Id, table.Name);
            }
            // Clean up
            reader.Dispose();
            cmd.Dispose();
            con.Dispose();
        }
    }
}

Table.cs:

class Table
    {
        [QuerySqlField(IsIndexed = true)]
        public int Id { get; set; }
        [QuerySqlField]
        public string Name { get; set; }

        public void SetObject(OracleDataReader reader)
        {
            Id = reader.IsDBNull(0) ? 0 : reader.GetInt32(0);
            Name = reader.IsDBNull(1) ? "" : reader.GetString(1);
        }
    }

Additionally, I took the suggestion of debugging it - I minimalized the code and added one breakpoint at cmd.CommandText = "SELECT * FROM TABLE"; and a second at reader.Dispose(); in order to let the while loop run in order to see how long it takes to go from the first breakpoint to the second.

Program.cs (without Apache Ignite/cache configurations):

class Program
    {
        static void Main(string[] args)
        {

            // Create Connection
            OracleConnection con = new OracleConnection();

            // Create ConnectionString using Builder
            OracleConnectionStringBuilder ocsb = new OracleConnectionStringBuilder();
            ocsb.Password = "PASSWORD";
            ocsb.UserID = "USER";
            ocsb.DataSource = "URL";

            // Connect 
            con.ConnectionString = ocsb.ConnectionString;
            con.Open();
            Console.WriteLine();
            Console.WriteLine(">>> Connection Established");

            // Execute a SQL SELECT
            OracleCommand cmd = con.CreateCommand();
                // Breakpoint #1
            cmd.CommandText = "select * from TABLE";
            OracleDataReader reader = cmd.ExecuteReader();

            while (reader.Read())
            {
                //Table table = new Table();
                //table.SetObject(reader);
            }

            // Clean up
                // Breakpoint #2
            reader.Dispose();
            cmd.Dispose();
            con.Dispose();
        }
    } 

From the diagnostic session, the breakpoint #1 occurred at 2.23s and breakpoint #2 occurred at 2,772.89s - meaning that it took 2,770.66 seconds or ~46 minutes between the two breakpoints.

I was wondering what the reason could be? And also, can this be resolved?

I have referenced

  • SQL Query executes slow in C#, but fast in Toad
  • Query runs fast in Query Analyzer but slow in C# application

and answers seem dated/unhelpful for this certain usecase.

来源:https://stackoverflow.com/questions/65176789/sql-query-slow-execution-time-in-c-fast-execution-in-toad

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!