将SQL查询转换成JOOQ

时间:2018-12-07 19:50:11

标签: java mysql sql jooq

我有这样的SQL查询:

select 
  e.event_type as eventType, 
  e.app_name as appName, 
  e.from_number as appNumber, 
  e.timestamp as messageTime, 
  b.created_at as blockTime 
from event_log e use index (idx_event_type_timestamp) 
  join blocked_phone_numbers b 
    on b.app_name = e.app_name
    and b.number = e.to_number 
where e.event_type = 1 
  and e.timestamp > 2018-09-08
  and b.created_at < e.timestamp 
limit 10;

我需要将其重构为一个JOOQ查询,这是我到达的地方:

SelectLimitPercentStep<Record> selectLimitPercentStep = context
                .select(EVENT_LOG.EVENT_TYPE, EVENT_LOG.APP_NAME, EVENT_LOG.FROM_NUMBER, EVENT_LOG.TIMESTAMP)
                .select(BLOCKED_PHONE_NUMBERS.CREATED_AT)
                //.use_index//todo: use index
                .from(EVENT_LOG)
                .join(BLOCKED_PHONE_NUMBERS)
                .on(EVENT_LOG.APP_NAME.eq(EVENT_LOG.APP_NAME).and(EVENT_LOG.TO_NUMBER.eq(BLOCKED_PHONE_NUMBERS.NUMBER)))
                .where(EVENT_LOG.EVENT_TYPE.eq((byte) EventType.OUTBOUND_SENT.ordinal())
                    .and(EVENT_LOG.TIMESTAMP.gt(new Timestamp(earliestTime.getMillis())))
                    .and(BLOCKED_PHONE_NUMBERS.CREATED_AT.lt(EVENT_LOG.TIMESTAMP)))
                .limit(1000);

但是我没有找到任何将use index (idx_event_type_timestamp)的部分翻译成JOOQ的方法,请问有人可以阐明吗?

1 个答案:

答案 0 :(得分:2)

jOOQ有一个Table.useIndex()方法,只需使用它即可。

EVENT_LOG.useIndex("idx_event_type_timestamp")