0

在我的应用程序中,我使用 spring 3.2.4、hibernate 4.2.5、postgres 9.2.2。表通过实体注解映射,并通过 postgres 中的 hibernate.hbm2ddl.auto=update 创建。

应用程序经常更新数据库,如果没有记录,则创建它。Postgres 默认在主键上创建自动索引。

1. Postgres 处理复合主键搜索的速度和单列主键一样吗?

2.在这种方法中,与单列键相比,hibernate的性能是否有所下降?

提前致谢

代码:

复合键:

@Embeddable
public class MyRecordKey implements Serializable{

private static final long serialVersionUID = -4966351432123537568L;
/**
 * In what interwal it was saved
 */
private String dateAndTime;

/**
 * name
 */
private String name;

      ...setters and getters ommited

@Override
public int hashCode() {
    ...code ommited
}

@Override
public boolean equals(Object obj) {
            ...code ommtited
}



}

桌子:

@Entity
public class MyRecord {

@EmbeddedId
private MyRecordKey myRecordKey;

    ...getters, setters and othey attributes ommited
}

道类:

@Transactional
@Repository
public class SaveMyElements {

@Autowired
private SessionFactory sessionFactory;

public void saveElements(List<MyRecord> myRecordList) {
    Session session = sessionFactory.getCurrentSession();
        for (MyRecord chr : myRecordList) {
            session.saveOrUpdate(chr);
                    }

}

数据源.xml:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans 
    http://www.springframework.org/schema/beans/spring-beans-3.2.xsd        
    http://www.springframework.org/schema/tx 
    http://www.springframework.org/schema/tx/spring-tx-3.2.xsd
    http://www.springframework.org/schema/context
    http://www.springframework.org/schema/context/spring-context-3.2.xsd">

<tx:annotation-driven transaction-manager="transactionManager"/>


<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method="close">

    <property name="driverClass" value="${app.driverClassName}" />
    <property name="jdbcUrl" value="${app.url}" />
    <property name="user" value="${app.username}" />
    <property name="password" value="${app.password}" />

    <property name="minPoolSize" value="2" />
    <property name="maxPoolSize" value="20" />
    <property name="maxIdleTime" value="10" />
    <property name="initialPoolSize" value="10" />

    <property name="idleConnectionTestPeriod" value="200" />
    <property name="checkoutTimeout" value="10000" />
    <property name="unreturnedConnectionTimeout" value="600" />
    <property name="automaticTestTable" value="conTest" />
    <property name="testConnectionOnCheckin" value="true" />
</bean>

<context:property-placeholder location="classpath:db.properties" />

<bean id="sessionFactory"
    class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
    <property name="dataSource" ref="dataSource" />
    <property name="packagesToScan" value="org.my.myreader.hibernate.entity" />
    <property name="hibernateProperties">
        <props>
            <prop key="hibernate.dialect">org.hibernate.dialect.PostgreSQLDialect</prop>
            <prop key="hibernate.hbm2ddl.auto">update</prop>
            <prop key="hibernate.show_sql">true</prop>
        </props>
    </property>
</bean>

<bean id="transactionManager"
    class="org.springframework.orm.hibernate4.HibernateTransactionManager">
    <property name="sessionFactory" ref="sessionFactory" />
    <property name="dataSource" ref="dataSource" />
</bean>

<tx:annotation-driven transaction-manager="transactionManager" />

ApplicationServlet-servlet.xml:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
xmlns:context="http://www.springframework.org/schema/context"
xmlns:task="http://www.springframework.org/schema/task"
xsi:schemaLocation="http://www.springframework.org/schema/beans 
    http://www.springframework.org/schema/beans/spring-beans-3.2.xsd        
    http://www.springframework.org/schema/tx 
    http://www.springframework.org/schema/tx/spring-tx-3.2.xsd
    http://www.springframework.org/schema/context
    http://www.springframework.org/schema/context/spring-context-3.2.xsd">


<context:annotation-config />

<context:spring-configured />
<context:load-time-weaver />

<context:component-scan base-package="org.my.myreader"/>
<import resource="classpath:datasource.xml"/> 
    <import resource="classpath:spring-scheduler.xml"/>  
</beans>
4

1 回答 1

0

是的,应该首选单列、纯技术、自动生成的主键。出于性能原因,也出于维护原因:

  1. 将功能属性放入 PK 可防止您修改它们的值
  2. 每个引用 MyRecord 的实体也必须有这两列。
  3. 您在 webapp 中创建的每个链接都需要两个参数,而不仅仅是一个参数来标识应用程序中记录的地址。
  4. 从您的应用程序到数据库的所有内容都需要更多内存/磁盘空间,并且速度会更慢(数字列上的索引比两个单独列上的索引更有效,尤其是 varchar 列)
  5. 在映射和查询此实体以及引用它的所有其他实体时,您会受到影响。

...

于 2013-09-26T20:06:54.497 回答