List <int>需要很长时间来实例化Nhibernate Criteria </int>

时间:2010-11-26 15:20:52

标签: c# nhibernate list criteria

我有这个查询,返回7行,在数据库中需要4ms,但实例化需要1075ms。这只是时代的一个例子,它在物体上有所不同,但它似乎没有意义,它花了这么长时间。有关如何提高速度的任何指示?

var criteria =
                GetSession().CreateSQLQuery(
                    @"
                    select circt_id as CircuitId from normal_upstream
                        where dni_equip_type = 'A'

                        start with up_equip_stn_no in (:UpStationNo1)
                        connect by prior equip_stn_no = up_equip_stn_no
                        union
                        select circt_id as CircuitId
                        from normal_upstream 
                        where up_equip_stn_no in (:UpStationNo1)")
                    .AddScalar("CircuitId", NHibernateUtil.Int32).SetParameterList("UpStationNo1",upstreamStations)
                    .List<int>()

生成SQL查询

   select
     circt_id as CircuitId 
from normal_upstream

where dni_equip_type = 'A'

                            start with up_equip_stn_no in (
    'B' /* :p0 */)
                        connect by prior equip_stn_no = up_equip_stn_no
                        union
                        select
 circt_id as CircuitId
                        from normal_upstream       
where up_equip_stn_no in (
    'B' /* :p1 */)

感谢任何帮助。感谢

1 个答案:

答案 0 :(得分:5)

显然减慢它的速度是SetParameterList调用。当我在获得8ms数据库和485ms实例化之前的格式化SQL时,这是可以接受的。我想有更快的东西,但现在就可以了。

以下是我目前正在使用的代码:

var sql =
            String.Format(
                @"select circt_id as CircuitId from normal_upstream
                where dni_equip_type = 'FDR_OCR'
                        start with up_equip_stn_no in ({0})
                        connect by prior equip_stn_no = up_equip_stn_no
                        union
                        select circt_id as CircuitId
                        from normal_upstream 
                        where up_equip_stn_no in ({0})",
                String.Join(",",upstreamStations.Select(x=>"'"+x+"'").ToArray()));
        var criteria =
            GetSession().CreateSQLQuery(sql)
                .AddScalar("CircuitId", NHibernateUtil.Int32)
                .List();
            return criteria;