2016-01-29 27 views
-1

時很慢扭矩4性能我有扭矩非常奇怪的性能問題。看看下面的循環。它需要13秒鐘來運行。但是如果我替換行 c.and(PicturePeer.ID,SimpleKey.keyFor(2072));添加簡單的關鍵標準

c.and(PicturePeer.ID,2072);

同一迴路中0.2秒運行。這是沒有意義的,因爲它是完全相同的操作,這是得到圖片ID = 2072;

我甚至能在我的PostgreSQL數據庫完整的查詢日誌和數據庫的選擇查詢時,我改變我用其中的2種c.add方法不會改變。所以我不明白爲什麼沒有SimpleKey的第二個解決方案比第一個解決方案快50倍。

我找到的原因是, c.and(PicturePeer.ID,SimpleKey.keyFor(2072)) 是retrieveByPK所使用的方法,它殺死了我的表現。

con = Transaction.begin(); 
      long start=System.currentTimeMillis(); 

      for(int i=0;i!=1000;i++) { 
       Criteria c=new Criteria(); 
       c=new Criteria(); 
       c.and(PicturePeer.ID,SimpleKey.keyFor(2072)); 
    //   c.and(PicturePeer.ID,2072); 
       PicturePeer.doSelect(c,con); 
      } 
      long end=System.currentTimeMillis(); 
      long diff=end-start; 
      System.out.println("Load diff=" + diff); 

      con.rollback(); 

從schema.xml中的圖片關係是這樣的:

<table name="picture" idMethod="native"> 
    <column name="id" primaryKey="true" required="true" type="INTEGER"/> 
    <column name="filename" required="true" type="VARCHAR"/> 
    <column name="name" required="true" type="VARCHAR"/> 
    <column name="description" required="true" type="VARCHAR"/> 
    <column name="role" required="true" type="VARCHAR"/> 
</table> 

如果我打電話setLimit(1)的標準,它使用SimpleKey緩慢的方法獲得儘可能快的快速方法,所以它看起來像postgresql使用表掃描,這對我來說沒有意義。特別是因爲慢速版和無限制版的版本提供了完全相同的查詢日誌。

這裏是PostgreSQL的完整日誌。 (選擇2的東西是驗證連接的東西)。

LOG: execute <unnamed>: SET extra_float_digits = 3 
LOG: execute <unnamed>: SET extra_float_digits = 3 
LOG: execute <unnamed>: SELECT 2 
LOG: execute <unnamed>: BEGIN 
LOG: execute <unnamed>: SELECT picture.id, picture.filename, picture.name, picture.description, picture.role FROM picture WHERE picture.id=$1 
DETAIL: parameters: $1 = '2072' 
LOG: execute S_1: ROLLBACK 

而只是要確定:

解釋分析SELECT picture.id,picture.filename,picture.name,picture.description,picture.role從圖片WHERE picture.id = 2072

給我:

Index Scan using picture_pkey on picture (cost=0.29..8.31 rows=1 width=46) (actual time=0.009..0.010 rows=1 loops=1) 
    Index Cond: (id = 2072) 
Planning time: 0.043 ms 
Execution time: 0.022 ms 

與預期的一樣。

回答

0

萬一有人關心,我發現這個問題。

轉矩與該類型的BigDecimal一個SimpleKey.keyFor值結束。這會導致問題,因爲Postgresql最終在執行select時不會使用其索引。 (我猜是類型轉換問題)。