0

我使用 Oracle 数据库作为我的后端。对于我的工作流程,我正在根据用户选择的内容构建动态查询。此动态查询从 5 个不同的表中获取。我正在从所有 5 个表中获取并读取所有列,这样我也可以在其他地方使用相同的逻辑。

Oracle.ManagedDataAccess.dll用于数据库访问。

代码:

    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"
    }
}

一切正常。查询执行甚至不需要一秒钟。然而,while 循环读取 750 条记录需要 40 秒。这是不可接受的。你能帮我改进while循环吗?我不确定它如何以最快的方式读取所有记录。

4

1 回答 1

0

非常感谢各位朋友的回复。

当一个接一个地注释掉以查看问题时,我发现我的数据阅读器本身存在错误。我发现在一种情况下,它总是出现异常,因此会减慢我的数据阅读器的速度。

解决异常后,现在我确实在 2 秒内收到了结果。解决了!

再一次感谢你 :)

于 2018-12-27T22:21:21.583 回答