混合喜欢和等于NHibernate查询示例?

时间:2011-10-20 18:55:52

标签: nhibernate query-by-example

我有一个相当大的SOA解决方案,新添加的服务使用NHibernate的Query By Example查询大约980万条记录。到目前为止,性能一直很糟糕,数据库上的配置文件将查询显示为:

exec sp_executesql N'SELECT this_.Id as Id3_0_, this_.ESIID as ESIID3_0_, this_.ADDRESS as ADDRESS3_0_, this_.ADDRESS_OVERFLOW as ADDRESS4_3_0_, this_.CITY as CITY3_0_, this_.STATE as STATE3_0_, this_.ZIP5 as ZIP7_3_0_, this_.ZIP4 as ZIP8_3_0_, this_.DUNS as DUNS3_0_, this_.PREMISE_TYPE as PREMISE10_3_0_, this_.STATUS as STATUS3_0_, this_.METER_READ_CYCLE as METER12_3_0_, this_.STATIONCODE as STATION13_3_0_ FROM vw_TDSP_ESIID this_ WHERE (lower(this_.ADDRESS) like lower(@p0) and lower(this_.CITY) like lower(@p1) and lower(this_.STATE) like lower(@p2) and lower(this_.ZIP5) like lower(@p3) and lower(this_.ZIP4) like lower(@p4))',N'@p0 nvarchar(10),@p1 nvarchar(2),@p2 nvarchar(4),@p3 nvarchar(7),@p4 nvarchar(2)',@p0=N'%110 Main%',@p1=N'%%',@p2=N'%TX%',@p3=N'%77002%',@p4=N'%%'

所以,主要是因为我的代码看起来像:

var queryEx = Example.Create(esiIdEntityProto)
            .EnableLike(MatchMode.Anywhere)
            .ExcludeNulls()
            .ExcludeZeroes()
            .IgnoreCase();

NHib在任何地方都使用like运算符,我得到了因为这就是我设置它的方式。但是有可能设置一些字段,有些字段是平等的吗?我需要Zip5为EQUAL并声明为EQUAL ......但其余部分可以是LIKE。

或者我是否只是破坏了QBE,所以我不妨使用常规的旧标准?

1 个答案:

答案 0 :(得分:2)

你可以混合

.ExcludeProperty("Zip5")


.Add(example).Add(Restrictions.Eq("Zip5", esiIdEntityProto.Zip5))

.ExcludeProperty("ADDRESS")


.Add(example).Add(Restrictions.Like("ADDRESS", esiIdEntityProto.Address))