import java.sql.*;
public class DBTest {
public static void main(String[] args) throws Exception {
test();
}
public static void test(){
String URL = "jdbc:informix-sqli://xxx:xxx/xxx:INFORMIXSERVER=xxx;DB_LOCALE=zh_CN.utf8;CLIENT_LOCALE=zh_CN.utf8;NEWCODESET=UTF8,utf8,57372;IFX_LOCK_MODE_WAIT=100;";
String USER = "xxx";
String PASSWORD ="xxxxx";
String sql1 = "select count(1) from checkresult where qhdm like '4101%'";
String sql2 = "select {+ index(checkresult idx_checkresult_qhdm)} count(*) from checkresult where qhdm like concat(?,'%')";
String sql3 = "select count(*) from checkresult where qhdm like concat(?,'%')";
String sql4 = "select {+ index(checkresult idx_checkresult_qhdm)} count(*) from checkresult where qhdm like ?||'%'";
try { Class.forName("com.informix.jdbc.IfxDriver"); Connection conn = DriverManager.getConnection(URL, USER, PASSWORD); Statement stmt = conn.createStatement(); //1.Load the driver System.out.println("Start:");
double b = System.currentTimeMillis();
ResultSet rs = stmt.executeQuery(sql1);
double b1 = System.currentTimeMillis();
System.out.println(" 结束1 like '4101%':"+(b1-b));
PreparedStatement pst = conn.prepareStatement(sql2);
double b2 = System.currentTimeMillis();
System.out.println(" get pst:"+(b2-b1));
pst.setString(1,"4101");
double b3 = System.currentTimeMillis();
System.out.println(" setString:"+(b3-b2));
double c = System.currentTimeMillis();
pst.executeQuery();
double c1 = System.currentTimeMillis();
System.out.println(" 结束2 + index like concat(?,'%'):"+(c1-c));
pst = conn.prepareStatement(sql3);
double c2 = System.currentTimeMillis();
System.out.println(" get pst:"+(c2-c1));
pst.setString(1,"4101");
double c3 = System.currentTimeMillis();
System.out.println(" setString:"+(c3-c2));
double d = System.currentTimeMillis();
pst.executeQuery();
double d1 = System.currentTimeMillis();
System.out.println("End 3 no + index like concat(?,'%'): "+(d1-d));
pst = conn.prepareStatement(sql4);
double d2 = System.currentTimeMillis();
System.out.println(" get pst:"+(d2-d1));
pst.setString(1,"4101");
double d3 = System.currentTimeMillis();
System.out.println(" setString:"+(d3-d2));
double e = System.currentTimeMillis();
pst.executeQuery();
System.out.println(" 结束4 + index like ?||%:"+(System.currentTimeMillis()-e));
}catch (Exception e){
e.printStackTrace();
}
}
}
========================Conclusion================================================================================================================================================================================================================================================================================== =====================
It can be seen that like? "%" is still a great test for the optimizer, so use it with caution.