`

Restrictions.in查询,及property lazy

阅读更多
最近使用Restrictions.in查询发现性能很差.

发现原因有两:
1.sql中,in,not in的运行效率就不是很高.一般使用exits替代.
但是in比exists的效率差也不是绝对的.
http://www.cnblogs.com/zwl12549/archive/2007/04/19/720028.html
http://blog.csdn.net/qustwmt/archive/2008/03/17/2190811.aspx
引用
IN适合于外表大而内表小的情况;EXISTS适合于外表小而内表大的情况。

引用
查询效率分析:
子查询为确保消除重复值,必须为外部查询的每个结果都处理嵌套查询。在这种情况下可以考虑用联接查询来取代。

如果要用子查询,那就用EXISTS替代IN、用NOT EXISTS替代NOT IN。因为EXISTS引入的子查询只是测试是否存在符合子查询中指定条件的行,效率较高。无论在哪种情况下,NOT IN都是最低效的。因为它对子查询中的表执行了一个全表遍历。

建立合理的索引,避免扫描多余数据,避免表扫描!
几百万条数据,照样几十毫秒完成查询


2.发现使用Restrictions.in,不进行二级缓存.
用SQL方式替代此HQL,缓存正常.

----------------------------------------------------------------------

在Hibernate3中添加了property的lazy功能.
一直以为只要设置了lazy=true就能延迟加载.但事实也不是.

在Hibernate Reference中也写到了
引用
To enable lazy property loading, set the lazy attribute on your particular property mappings:

<class name="Document">
       <id name="id">
        <generator class="native"/>
    </id>
    <property name="name" not-null="true" length="50"/>
    <property name="summary" not-null="true" length="200" lazy="true"/>
    <property name="text" not-null="true" length="2000" lazy="true"/>
</class>
Lazy property loading requires buildtime bytecode instrumentation! If your persistent classes are not enhanced, Hibernate will silently ignore lazy property settings and fall back to immediate fetching.

For bytecode instrumentation, use the following Ant task:

<target name="instrument" depends="compile">
    <taskdef name="instrument" classname="org.hibernate.tool.instrument.InstrumentTask">
        <classpath path="${jar.path}"/>
        <classpath path="${classes.dir}"/>
        <classpath refid="lib.class.path"/>
    </taskdef>

    <instrument verbose="true">
        <fileset dir="${testclasses.dir}/org/hibernate/auction/model">
            <include name="*.class"/>
        </fileset>
    </instrument>
</target>
A different (better?) way to avoid unnecessary column reads, at least for read-only transactions is to use the projection features of HQL or Criteria queries. This avoids the need for buildtime bytecode processing and is certainly a prefered solution.


不是很明白bytecode instrumentation是什么意思.
只能按照<深入浅出Hibernate>中的方法,显示多态的方法实现...
1
0
分享到:
评论

相关推荐

    Restrictions用法.docx

    例如,`Restrictions.in("name", Arrays.asList("AAA", "BBB", "CCC"))`会匹配名字为"AAA"、"BBB"或"CCC"的学生。 7. `Restrictions.and`, `Restrictions.or`: 用于组合多个条件,分别对应逻辑与(AND)和逻辑或...

    java QBC常用方法

    - **语法**:`Restrictions.not(Restrictions.in(String propertyName, Collection values))` - **示例**: ```java Criteria criteria = session.createCriteria(User.class); List&lt;String&gt; usernames = Arrays....

    NHibernate HQL 条件查询

    - `Restrictions.in(property, values)`:表示属性值位于给定集合中的条件。 - `Restrictions.between(property, fromValue, toValue)`:表示属性值位于两个值之间的条件。 #### 复合条件表达式的构建 - `...

    Restrictions用法.pdf

    7. `Restrictions.in`: 对应SQL的`IN`子句,用于指定一个值列表,如`in("id", ids)`会找到id在指定数组ids中的学生对象。 8. `Restrictions.and`和`Restrictions.or`: 分别表示逻辑与(AND)和逻辑或(OR)。例如,...

    Hibernate Criteria用法大全.docx

    Criterion statusCrit = Restrictions.in("status", new String[]{"active", "pending"}); Criterion dateCrit = Restrictions.ge("date", startDate); criteria.add(Restrictions.and(statusCrit, dateCrit)); ...

    DetachedCriteria使用介绍

    searDc.add(Property.forName("unid").in(sub)); ``` 这样,你就可以构建类似于 SQL 的子查询结构:`Select * from Person a where a.unid in (select usernid from PositionUserLink b where b.positionunid=..)`...

    criterion简介

    * Restrictions.in():在某个集合内 * Restrictions.and():AND 关系 * Restrictions.or():OR 关系 * Restrictions.isNull():判断属性是否为空 * Restrictions.isNotNull():判断属性是否不为空 * Order.asc():...

    Hibernate学习笔记

    6. `Restrictions.in()`: 对应SQL的"in"表达式,例如`Restrictions.in("id", new Integer[]{1, 2, 3})`,用于设定某个字段的值在给定的列表中。 7. `Restrictions.eqProperty()`, `Restrictions.gtProperty()`, `...

    Oracle中(column1,column2..)in(集合/子查询)的hibernate实现

    criteria.add(Restrictions.in("column1", valueList.stream().map(arr -&gt; arr[0]).collect(Collectors.toList()))); criteria.add(Restrictions.in("column2", valueList.stream().map(arr -&gt; arr[1]).collect...

    Hibernate实体查询(ceriteria)精解

    .add(Restrictions.in("name", new String[]{"Fritz", "Izi", "Pk"})) .add(Restrictions.disjunction() .add(Restrictions.isNull("age")) .add(Restrictions.eq("age", new Integer(0))) .add(Restrictions....

    qbc表达式

    - **示例**: 如果要查询年龄不在[25, 30, 35]中的用户,可以使用`Restrictions.not(Restrictions.in("age", new Integer[]{25, 30, 35}))`。 ##### 15. **闭区间xy中的任意值** `between x and y` - **HQL运算符**...

    DetachedCriteria查询

    可以使用 `Restrictions.like()` 方法添加 LIKE 查询条件,例如: `Criterion fullName = Restrictions.like("fullName", findKey);` `Criterion shortName = Restrictions.like("shortName", findKey);` `...

    Hibernate三大类查询总结

    `Restrictions.in()`方法用于指定一个值列表,筛选出符合列表内任一值的记录。`Disjunction()`可以组合多个条件,实现“或”的效果,如下所示: ```java List&lt;Cat&gt; cats = sess.createCriteria(Cat.class) .add...

    7-Zip压缩软件 7-Zip is open source software. Most of the source code is under the GNU LGPL license. The unRAR code is under a mixed license: GNU LGPL + unRAR restrictions. Check license information here: 7-Zip license.

    7-Zip压缩软件7-Zip is open source software. Most of the source code is under the ... The unRAR code is under a mixed license: GNU LGPL + unRAR restrictions. Check license information here: 7-Zip license.

    Hibernate中Criteria的完整用法

    `in`和`not in`操作可以使用`Restrictions.in()`和`Restrictions.not(Restrictions.in())`实现,用于查找值在给定列表中的记录或不在列表中的记录。`between`和`not between`操作则通过`Restrictions.between()`和`...

    DetachedCriteria Criteria 使用方法

    | not in | Restrictions.not(Restrictions.in()) | 不等于列表中任意一个值 | | between x and y | Restrictions.between() | 闭区间 x 和 y 中的任意值 | | not between x and y | Restrictions.not(Restrictions....

    Hibernate Criteria 完全使用

    - `Expression`:用于构建查询条件,如 ` Restrictions.eq("propertyName", value)` 表示“属性等于值”。 ### 2. 添加查询条件 Criteria API 支持多种条件表达式,例如: - `eq(String propertyName, Object ...

    hibernate creteria

    .add(Property.forName("name").in(new String[]{"Fritz", "Izi", "Pk"})) .list(); ``` 通过以上解析,我们可以看到 Hibernate Criteria API 提供了一个强大且灵活的查询框架,能够满足复杂的数据检索需求,同时...

    hibernate criteria的使用方法

    crit.add(Restrictions.not(Restrictions.in("age", new Integer[]{2, 3}))); ``` - **在集合中**: ```java crit.add(Restrictions.in("age", new Integer[]{2, 3})); ``` - **在某个区间内**: ```java ...

    criteria介绍与使用

    .add(Restrictions.in("name", new String[]{"Fritz", "Izi", "Pk"})) .add(Restrictions.disjunction() .add(Restrictions.isNull("age")) .add(Restrictions.eq("age", new Integer(0))) .add(Restrictions....

Global site tag (gtag.js) - Google Analytics