Search code examples
c#oracleperformanceoracle.manageddataaccess

Oracle.ManagedDataAccess performance in C#


I am using Oracle database as my backend. For my workflow, I am building a dynamic query based on what the user has selected. This dynamic query fetches from the 5 different tables. I am fetching and reading all the columns from all 5 tables so then I can use the same logic from elsewhere as well.

Oracle.ManagedDataAccess.dll is used for the database access.

Code:

    public class ClassEx
{
    public ClassA ClassA { get; set; }
    public ClassB ClassB { get; set; }
    public ClassC ClassC { get; set; }
    public ClassD ClassD { get; set; }
    public ClassE ClassE { get; set; }

    public List<ClassEx> Select()
    {
        List<ClassEx> ex = new List<ClassEx>();
        string sql = "Select "  + (new ClaasA()).GetFullString("a") + ", "
                    + (new ClaasB()).GetFullString("b") + ", "
                    + (new ClaasC()).GetFullString("c") + ", "
                    + (new ClaasD()).GetFullString("d") + ", "
                    + (new ClaasE()).GetFullString("e") +
                " From Sysadm.TableA a, Sysadm.TableB b, Sysadm.TableC c, Sysadm.TableD d, Sysadm.TableE e" +
                " Where a.Col1 = b.Col2" +
                " And a.Col5 = c.Col2" +
                " And a.Col6 = d.Col2" + 
                " And a.Col10 = e.Col2";

        GenericDataReader dr = new GenericDataReader(sql);

        while(dr.Read())
        {
            ClassEx dummy = new ClassEx();

            dummy.ClassA = new ClassA(dr);
            dummy.ClassB = new ClassB(dr);
            dummy.ClassC = new ClassC(dr);
            dummy.ClassD = new ClassD(dr);
            dummy.ClassE = new ClassE(dr);

            ex.Add(dummy);
        }

        return ex;
    }
}



public ClassA
{
    public int Col1 {get;set;}
    ...
    public DateTime? Col30 {get;set;}

    public ClassA(GenericDataReader dr)
    {
        Col1 = dr.GetInt("A_Col1")
        Col2 = dr.GetString("A_Col2")
        ....
        Col30 = dr.GetDateTime("A_Col30")
    }

    public string GetFullString(string alias)
    {
        if (!String.IsNullOrEmpty(alias))
                    alias = alias + ".";

        return  alias + "A_Col1, " + 
            alias + "A_Col2, " +
            ...
            alias + "A_Col30"
    }
}

public ClassB
{
    public int Col1 {get;set;}
    ...
    public DateTime? Col30 {get;set;}

    public ClassB(GenericDataReader dr)
    {
        Col1 = dr.GetInt("B_Col1")
        Col2 = dr.GetString("B_Col2")
        ....
        Col30 = dr.GetDateTime("B_Col30")
    }

    public string GetFullString(string alias)
    {
        if (!String.IsNullOrEmpty(alias))
                    alias = alias + ".";

        return  alias + "B_Col1, " + 
            alias + "B_Col2, " +
            ...
            alias + "B_Col30"
    }
}

public ClassC
{
    public int Col1 {get;set;}
    ...
    public DateTime? Col25 {get;set;}

    public ClassC(GenericDataReader dr)
    {
        Col1 = dr.GetInt("C_Col1")
        Col2 = dr.GetString("C_Col2")
        ....
        Col25 = dr.GetDateTime("C_Col25")
    }

    public string GetFullString(string alias)
    {
        if (!String.IsNullOrEmpty(alias))
                    alias = alias + ".";

        return  alias + "C_Col1, " + 
            alias + "C_Col2, " +
            ...
            alias + "C_Col25"
    }
}

public ClassD
{
    public int Col1 {get;set;}
    ...
    public DateTime? Col10 {get;set;}

    public ClassD(GenericDataReader dr)
    {
        Col1 = dr.GetInt("D_Col1")
        Col2 = dr.GetString("D_Col2")
        ....
        Col10 = dr.GetDateTime("D_Col10")
    }

    public string GetFullString(string alias)
    {
        if (!String.IsNullOrEmpty(alias))
                    alias = alias + ".";

        return  alias + "D_Col1, " + 
            alias + "D_Col2, " +
            ...
            alias + "D_Col10"
    }
}

public ClassE
{
    public int Col1 {get;set;}
    ...
    public DateTime? Col35 {get;set;}

    public ClassE(GenericDataReader dr)
    {
        Col1 = dr.GetInt("E_Col1")
        Col2 = dr.GetString("E_Col2")
        ....
        Col35 = dr.GetDateTime("E_Col35")
    }

    public string GetFullString(string alias)
    {
        if (!String.IsNullOrEmpty(alias))
                    alias = alias + ".";

        return  alias + "E_Col1, " + 
            alias + "E_Col2, " +
            ...
            alias + "E_Col35"
    }
}

Everything work well. Query execution does not take even a second to execute. However, while loop takes 40 seconds to read 750 records. Which is unacceptable. Can you please help me in improving the while loop. I am not sure how best it can read all the record in fastest way.


Solution

  • Thank you so much friends for your response.

    I have found a bug in my data reader itself when commented out one after another to see the issue. I have found that in one case, it always going in the exception and so it slows down my data reader.

    After solving the exception, now I do receive my result in 2 seconds. Solved!

    Thank you once again :)