SQL Queries sometimes become a heck to handle. So I have a SQL Query consisting multiple OR operations and REGEX. While running through JAVA code execution time is more than the expected time on the huge entries of data.
Below is the query:
SELECT * FROM tableName WHERE (col1 REGEXP ('xyz'|'abc') OR (col2 = 15 AND (col1 REGEXP ('xyz'|'abc')))) AND col3>='2017-08-28' AND col3<='2017-08-30';
Hibernate Code:
public List<MyModel> getAllMatchedEntries() {
long lStartTime = new Date().getTime();
Query query = ((SQLQuery) getSession().createSQLQuery( "SELECT * FROM tableName WHERE (col1 REGEXP :list OR (col2 = 15 AND (col1 REGEXP :list))) AND col3>=:sd AND col3<=:ed"). setResultTransformer(Transformers.aliasToBean(LeadGenViewTable.class)).setParameter("list", regexlist).setParameter("mid", merchant_id).setParameter("todayDate", sd).setParameter("ed", ed));
List<MyModel> list = query.list();
long lEndTime = new Date().getTime();
long output = lEndTime - lStartTime;
list= list.stream().filter(distinctByKey(t -> t.getSomeCol())).collect(Collectors.toList());
System.out.println("Time Taken "+output);
return list;
}
And Surprisingly Output in logs:
Time Taken 45616
I'm facing a hard time figuring out the time lag happening either because of REGEXP / OR operations. Help would be appreciated.