谓词中的连接
适用于 ❌ 开源版 ✅ Express 版 ✅ 专业版 ✅ 企业版
处理此事件的 SPI 方法是 consecutiveAggregation()
在谓词中使用 CONCAT 通常是潜在的慢速和/或错误查询的标志。
为什么这不好?
普通的索引不能以这种方式使用。即使可以定义基于函数的索引来覆盖这样的谓词,通常最好不要在谓词中使用连接,因为
- 连接列上的普通索引更可能被其他查询重用。
- 除非有一个已知不在实际数据中出现的分隔符,否则这些连接会产生相同的值:
'John Taylor' || 'Doe'
和'John' || 'Taylor Doe'
。 但它们可能是不同的名字和姓氏。
这里给出了一个例子
// A custom DiagnosticsListener SPI implementation class ConcatenationInPredicate implements DiagnosticsListener { @Override public void concatenationInPredicate(DiagnosticsContext ctx) { // The statement that is being executed and which has a concatenation in a predicate. System.out.println("Actual statement: " + ctx.actualStatement()); // The predicate containing the concatenation. System.out.println("Predicate : " + ctx.part()); } }
然后
// Configuration is configured with the target DataSource, SQLDialect, etc. for instance Oracle. try ( Connection c = DSL.using(configuration.derive(new ConcatenationInPredicate())) .diagnosticsConnection(); Statement s = c.createStatement() ) { try (ResultSet a = s.executeQuery("SELECT id FROM author WHERE first_name || last_name = ?")) { while (a.next()) println(a.getInt(1)); } }
反馈
您对此页面有任何反馈吗? 我们很乐意倾听!