将SQL查询转换为JOOQ

问题描述 投票:2回答:1

我有这样的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,任何人都可以放弃任何光线吗?

java mysql sql jooq
1个回答
2
投票

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

EVENT_LOG.useIndex("idx_event_type_timestamp")
© www.soinside.com 2019 - 2024. All rights reserved.