bug fixed for adb Postgres and related the explain function in order to compute the estimated rows of a table. Added a treshold with value 100000. If the rows computed with the explain function is < 100000 the counter function is used otherwise the explain result. DatabaseOperations class modified.
git-svn-id: https://svn.d4science.research-infrastructures.eu/gcube/trunk/data-access/DatabasesResourcesManager@101427 82a268e6-3cf1-43bd-a215-b396298e98cf
This commit is contained in:
parent
8a06b5373b
commit
630d4beadc
|
@ -21,19 +21,24 @@ public class DatabaseOperations {
|
||||||
|
|
||||||
// private static final String QueryPostgres =
|
// private static final String QueryPostgres =
|
||||||
// "select count(*) from \"%1$s\" limit 1";
|
// "select count(*) from \"%1$s\" limit 1";
|
||||||
// private static final String QueryPostgres = "select count(*) from (select * from \"%1$s\" limit 1) as a";
|
// private static final String QueryPostgres =
|
||||||
|
// "select count(*) from (select * from \"%1$s\" limit 1) as a";
|
||||||
private static final String QueryPostgres = "select count(*) from (select * from %1$s limit 1) as a";
|
private static final String QueryPostgres = "select count(*) from (select * from %1$s limit 1) as a";
|
||||||
// private static final String QueryMysql = "select count(*) from (select * from `%1$s` limit 1) as a";
|
// private static final String QueryMysql =
|
||||||
|
// "select count(*) from (select * from `%1$s` limit 1) as a";
|
||||||
private static final String QueryMysql = "select count(*) from (select * from %1$s limit 1) as a";
|
private static final String QueryMysql = "select count(*) from (select * from %1$s limit 1) as a";
|
||||||
// private static final String QueryMysql =
|
// private static final String QueryMysql =
|
||||||
// "select count(*) from `%1$s` limit 1";
|
// "select count(*) from `%1$s` limit 1";
|
||||||
|
private static final String ActualRowsNumberQueryPostgres = "select count(*) from (select * from %1$s) as a";
|
||||||
|
|
||||||
// private static final String Query = "select * from %1$s limit 1";
|
// private static final String Query = "select * from %1$s limit 1";
|
||||||
// private static final String countQuery = "select count(*) from %1$s";
|
// private static final String countQuery = "select count(*) from %1$s";
|
||||||
// private static final String explainQuery = "explain select * from %1$s";
|
// private static final String explainQuery = "explain select * from %1$s";
|
||||||
// private static final String explainQueryPostgres = "explain select * from \"%1$s\"";
|
// private static final String explainQueryPostgres =
|
||||||
|
// "explain select * from \"%1$s\"";
|
||||||
private static final String explainQueryPostgres = "explain select * from %1$s";
|
private static final String explainQueryPostgres = "explain select * from %1$s";
|
||||||
// private static final String explainQueryMysql = "explain select * from `%1$s`";
|
// private static final String explainQueryMysql =
|
||||||
|
// "explain select * from `%1$s`";
|
||||||
private static final String explainQueryMysql = "explain select * from %1$s";
|
private static final String explainQueryMysql = "explain select * from %1$s";
|
||||||
|
|
||||||
private static final String MYSQL = "MySQL";
|
private static final String MYSQL = "MySQL";
|
||||||
|
@ -105,154 +110,185 @@ public class DatabaseOperations {
|
||||||
return DBType;
|
return DBType;
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Method that calculate the estimated number of rows
|
// Method that calculate the estimated number of rows
|
||||||
public long calculateElements(ConnectionManager connection,
|
public long calculateElements(ConnectionManager connection, String dbType,
|
||||||
String dbType, String tablename, String schemaName, SessionFactory session)
|
String tablename, String schemaName, SessionFactory session)
|
||||||
throws Exception {
|
throws Exception {
|
||||||
|
|
||||||
long count = 0;
|
long count = 0;
|
||||||
|
|
||||||
String countingQuery = null;
|
String countingQuery = null;
|
||||||
|
|
||||||
if (dbType.equals(POSTGRES)) {
|
if (dbType.equals(POSTGRES)) {
|
||||||
|
|
||||||
//the full name equal to "schemaname.tablename"
|
|
||||||
tablename=schemaName+"."+ "\"" +tablename+ "\"";
|
|
||||||
|
|
||||||
countingQuery = String.format(QueryPostgres, tablename);
|
// the full name equal to "schemaname.tablename"
|
||||||
|
tablename = schemaName + "." + "\"" + tablename + "\"";
|
||||||
|
|
||||||
}
|
countingQuery = String.format(QueryPostgres, tablename);
|
||||||
if (dbType.equals(MYSQL)) {
|
|
||||||
//the full name equal to "dbname.tablename"
|
|
||||||
tablename=schemaName+"."+tablename;
|
|
||||||
|
|
||||||
countingQuery = String.format(QueryMysql, tablename);
|
}
|
||||||
|
if (dbType.equals(MYSQL)) {
|
||||||
|
// the full name equal to "dbname.tablename"
|
||||||
|
tablename = schemaName + "." + tablename;
|
||||||
|
|
||||||
}
|
countingQuery = String.format(QueryMysql, tablename);
|
||||||
|
|
||||||
AnalysisLogger.getLogger().debug(
|
}
|
||||||
"DatabaseOperations->calculating rows' number with the query: "
|
|
||||||
+ countingQuery);
|
|
||||||
|
|
||||||
List<Object> result;
|
AnalysisLogger.getLogger().debug(
|
||||||
|
"DatabaseOperations->calculating rows' number with the query: "
|
||||||
|
+ countingQuery);
|
||||||
|
|
||||||
// try {
|
List<Object> result;
|
||||||
// result = DatabaseFactory.executeSQLQuery(countingQuery, session);
|
|
||||||
|
|
||||||
result = connection.executeQuery(countingQuery, session);
|
// try {
|
||||||
|
// result = DatabaseFactory.executeSQLQuery(countingQuery, session);
|
||||||
|
|
||||||
// if ((result != null) && (result.size() > 0)) {
|
result = connection.executeQuery(countingQuery, session);
|
||||||
if (result != null) {
|
|
||||||
|
|
||||||
Object element = result.get(0);
|
|
||||||
|
|
||||||
ArrayList<Object> listvalues = new ArrayList<Object>(((LinkedHashMap<String, Object>) element).values());
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
// System.out.println("Dimension: " + result.size());
|
// if ((result != null) && (result.size() > 0)) {
|
||||||
|
if (result != null) {
|
||||||
|
|
||||||
// Integer numElem = Integer.valueOf(result.get(0).toString());
|
Object element = result.get(0);
|
||||||
|
|
||||||
// Long numElemvalue = Long.valueOf(result.get(0).toString());
|
|
||||||
|
|
||||||
Long numElemvalue = Long.valueOf(listvalues.get(0).toString());
|
|
||||||
|
|
||||||
long numElem = numElemvalue.longValue();
|
|
||||||
|
|
||||||
// if (numElem.intValue() == 0){ throw new
|
ArrayList<Object> listvalues = new ArrayList<Object>(
|
||||||
// Exception("The table has not rows");}
|
((LinkedHashMap<String, Object>) element).values());
|
||||||
|
|
||||||
if (numElem > 0) {
|
// System.out.println("Dimension: " + result.size());
|
||||||
|
|
||||||
AnalysisLogger
|
// Integer numElem = Integer.valueOf(result.get(0).toString());
|
||||||
.getLogger()
|
|
||||||
.debug("DatabaseOperations->the database has at least a row.Calculating rows' number through an estimation");
|
|
||||||
|
|
||||||
String explain = null;
|
// Long numElemvalue = Long.valueOf(result.get(0).toString());
|
||||||
|
|
||||||
if (dbType.equals(POSTGRES)) {
|
Long numElemvalue = Long.valueOf(listvalues.get(0).toString());
|
||||||
|
|
||||||
|
|
||||||
explain = String.format(explainQueryPostgres, tablename);
|
long numElem = numElemvalue.longValue();
|
||||||
|
|
||||||
}
|
// if (numElem.intValue() == 0){ throw new
|
||||||
if (dbType.equals(MYSQL)) {
|
// Exception("The table has not rows");}
|
||||||
|
|
||||||
|
|
||||||
explain = String.format(explainQueryMysql, tablename);
|
|
||||||
|
|
||||||
}
|
if (numElem > 0) {
|
||||||
|
|
||||||
// call query with explain function
|
AnalysisLogger
|
||||||
|
.getLogger()
|
||||||
|
.debug("DatabaseOperations->the database has at least a row.Calculating rows' number through an estimation");
|
||||||
|
|
||||||
|
String explain = null;
|
||||||
|
|
||||||
|
if (dbType.equals(POSTGRES)) {
|
||||||
|
|
||||||
|
explain = String.format(explainQueryPostgres, tablename);
|
||||||
|
|
||||||
|
}
|
||||||
|
if (dbType.equals(MYSQL)) {
|
||||||
|
|
||||||
|
explain = String.format(explainQueryMysql, tablename);
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
// call query with explain function
|
||||||
|
|
||||||
|
AnalysisLogger.getLogger().debug(
|
||||||
|
"DatabaseOperations->calculating rows' number with the query: "
|
||||||
|
+ explain);
|
||||||
|
|
||||||
|
List<Object> resultinfo;
|
||||||
|
|
||||||
|
// resultinfo = DatabaseFactory.executeSQLQuery(explain,
|
||||||
|
// session);
|
||||||
|
|
||||||
|
resultinfo = connection.executeQuery(explain, session);
|
||||||
|
|
||||||
|
// recovery result
|
||||||
|
|
||||||
|
if (dbType.equals(MYSQL)) {
|
||||||
|
|
||||||
|
// Object[] resultArray = (Object[]) (resultinfo.get(0));
|
||||||
|
|
||||||
|
Object elem = resultinfo.get(0);
|
||||||
|
|
||||||
|
ArrayList<Object> values = new ArrayList<Object>(
|
||||||
|
((LinkedHashMap<String, Object>) elem).values());
|
||||||
|
|
||||||
|
// //print check
|
||||||
|
// AnalysisLogger.getLogger().debug(
|
||||||
|
// "DatabaseOperations->VALUE: " + values);
|
||||||
|
|
||||||
|
BigInteger value = (BigInteger) values.get(8);
|
||||||
|
|
||||||
|
// BigInteger value = (BigInteger) resultArray[8];
|
||||||
|
|
||||||
|
count = value.longValue();
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
if (dbType.equals(POSTGRES)) {
|
||||||
|
|
||||||
|
String var = resultinfo.get(0).toString();
|
||||||
|
|
||||||
|
int beginindex = var.indexOf("rows");
|
||||||
|
|
||||||
|
int lastindex = var.indexOf("width");
|
||||||
|
|
||||||
|
var = var.substring(beginindex + 5, lastindex - 1);
|
||||||
|
|
||||||
|
Long value = Long.valueOf(var);
|
||||||
|
|
||||||
|
count = value.longValue();
|
||||||
|
|
||||||
AnalysisLogger.getLogger().debug(
|
AnalysisLogger.getLogger().debug(
|
||||||
"DatabaseOperations->calculating rows' number with the query: "
|
"DatabaseOperations-> rows' number with explain function: "
|
||||||
+ explain);
|
+ count);
|
||||||
|
|
||||||
List<Object> resultinfo;
|
// threshold fixed to 100000 value in order to count an
|
||||||
|
// actual rows' number if the estimated rows is < 100000
|
||||||
|
// otherwise because PGAdmin returns a wrong rows number for
|
||||||
|
// a table with a little rows number.
|
||||||
|
|
||||||
// resultinfo = DatabaseFactory.executeSQLQuery(explain,
|
if (count < 100000) {
|
||||||
// session);
|
AnalysisLogger.getLogger().debug(
|
||||||
|
"DatabaseOperations->rows' number with explain function less than the threshold with value 100000");
|
||||||
|
// to count an actual rows number
|
||||||
|
AnalysisLogger.getLogger().debug(
|
||||||
|
"DatabaseOperations->calculating the actual rows' number with the query: "
|
||||||
|
+ String.format(
|
||||||
|
ActualRowsNumberQueryPostgres,
|
||||||
|
tablename));
|
||||||
|
|
||||||
resultinfo = connection.executeQuery(explain, session);
|
List<Object> resultRowsNumber;
|
||||||
|
resultRowsNumber = connection.executeQuery(String
|
||||||
|
.format(ActualRowsNumberQueryPostgres,
|
||||||
|
tablename), session);
|
||||||
|
|
||||||
// recovery result
|
if (resultRowsNumber != null) {
|
||||||
|
Object elem = resultRowsNumber.get(0);
|
||||||
if (dbType.equals(MYSQL)) {
|
ArrayList<Object> listValues = new ArrayList<Object>(
|
||||||
|
((LinkedHashMap<String, Object>) elem)
|
||||||
// Object[] resultArray = (Object[]) (resultinfo.get(0));
|
.values());
|
||||||
|
Long numElemValue = Long.valueOf(listValues.get(0)
|
||||||
Object elem = resultinfo.get(0);
|
.toString());
|
||||||
|
count = numElemValue.longValue();
|
||||||
ArrayList<Object> values = new ArrayList<Object>(
|
|
||||||
((LinkedHashMap<String, Object>) elem).values());
|
|
||||||
|
|
||||||
// //print check
|
|
||||||
// AnalysisLogger.getLogger().debug(
|
|
||||||
// "DatabaseOperations->VALUE: " + values);
|
|
||||||
|
|
||||||
BigInteger value = (BigInteger) values.get(8);
|
|
||||||
|
|
||||||
// BigInteger value = (BigInteger) resultArray[8];
|
|
||||||
|
|
||||||
count = value.longValue();
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
if (dbType.equals(POSTGRES)) {
|
|
||||||
|
|
||||||
String var = resultinfo.get(0).toString();
|
|
||||||
|
|
||||||
int beginindex = var.indexOf("rows");
|
|
||||||
|
|
||||||
int lastindex = var.indexOf("width");
|
|
||||||
|
|
||||||
var = var.substring(beginindex + 5, lastindex - 1);
|
|
||||||
|
|
||||||
Long value = Long.valueOf(var);
|
|
||||||
|
|
||||||
count = value.longValue();
|
|
||||||
|
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// } catch (Exception e) {
|
|
||||||
// TODO Auto-generated catch block
|
|
||||||
// e.printStackTrace();
|
|
||||||
// }
|
|
||||||
|
|
||||||
AnalysisLogger.getLogger().debug(
|
|
||||||
"DatabaseOperations->rows' number calculated: " + count);
|
|
||||||
|
|
||||||
return count;
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// } catch (Exception e) {
|
||||||
|
// TODO Auto-generated catch block
|
||||||
|
// e.printStackTrace();
|
||||||
|
// }
|
||||||
|
|
||||||
|
AnalysisLogger.getLogger().debug(
|
||||||
|
"DatabaseOperations->rows' number calculated: " + count);
|
||||||
|
|
||||||
|
return count;
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue