killbill-memoizeit

util: optimize pagination query Force the offset on the index

1/23/2017 8:14:47 AM

Details

diff --git a/util/src/main/resources/org/killbill/billing/util/entity/dao/EntitySqlDao.sql.stg b/util/src/main/resources/org/killbill/billing/util/entity/dao/EntitySqlDao.sql.stg
index d05e54e..6f35450 100644
--- a/util/src/main/resources/org/killbill/billing/util/entity/dao/EntitySqlDao.sql.stg
+++ b/util/src/main/resources/org/killbill/billing/util/entity/dao/EntitySqlDao.sql.stg
@@ -151,10 +151,15 @@ get(offset, rowCount, orderBy) ::= <<
 select
 <allTableFields("t.")>
 from <tableName()> t
-where <CHECK_TENANT("t.")>
-<andCheckSoftDeletionWithComma("t.")>
+join (
+  select <recordIdField()>
+  from <tableName()>
+  where <CHECK_TENANT()>
+  <andCheckSoftDeletionWithComma()>
+  order by <orderBy>
+  limit :rowCount offset :offset
+) optimization on <recordIdField("optimization.")> = <recordIdField("t.")>
 order by t.<orderBy>
-limit :rowCount offset :offset
 ;
 >>
 
diff --git a/util/src/test/java/org/killbill/billing/util/dao/TestStringTemplateInheritance.java b/util/src/test/java/org/killbill/billing/util/dao/TestStringTemplateInheritance.java
index 7f1627c..cc50c48 100644
--- a/util/src/test/java/org/killbill/billing/util/dao/TestStringTemplateInheritance.java
+++ b/util/src/test/java/org/killbill/billing/util/dao/TestStringTemplateInheritance.java
@@ -123,9 +123,14 @@ public class TestStringTemplateInheritance extends UtilTestSuiteNoDB {
                                                                                                                                                           ", t.account_record_id\r?\n" +
                                                                                                                                                           ", t.tenant_record_id\r?\n" +
                                                                                                                                                           "from kombucha t\r?\n" +
-                                                                                                                                                          "where t.tenant_record_id = :tenantRecordId\r?\n" +
+                                                                                                                                                          "join \\(\r?\n" +
+                                                                                                                                                          "  select record_id\r?\n" +
+                                                                                                                                                          "  from kombucha\r?\n" +
+                                                                                                                                                          "  where tenant_record_id = :tenantRecordId\r?\n" +
+                                                                                                                                                          "  order by record_id\r?\n" +
+                                                                                                                                                          "  limit :rowCount offset :offset\r?\n" +
+                                                                                                                                                          "\\) optimization on optimization.record_id = t.record_id\r?\n" +
                                                                                                                                                           "order by t.record_id\r?\n" +
-                                                                                                                                                          "limit :rowCount offset :offset\r?\n" +
                                                                                                                                                           ";");
         assertPattern(kombucha.getInstanceOf("test").toString(), "select\r?\n" +
                                                                  "  t.record_id\r?\n" +