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:
Loredana Liccardo 2014-11-04 13:25:13 +00:00
parent 8a06b5373b
commit 630d4beadc
1 changed files with 151 additions and 115 deletions

View File

@ -21,19 +21,24 @@ public class DatabaseOperations {
// private static final String QueryPostgres =
// "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 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 `%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 countQuery = "select count(*) 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 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";
@ -107,8 +112,8 @@ public class DatabaseOperations {
}
// Method that calculate the estimated number of rows
public long calculateElements(ConnectionManager connection,
String dbType, String tablename, String schemaName, SessionFactory session)
public long calculateElements(ConnectionManager connection, String dbType,
String tablename, String schemaName, SessionFactory session)
throws Exception {
long count = 0;
@ -147,9 +152,8 @@ public class DatabaseOperations {
Object element = result.get(0);
ArrayList<Object> listvalues = new ArrayList<Object>(((LinkedHashMap<String, Object>) element).values());
ArrayList<Object> listvalues = new ArrayList<Object>(
((LinkedHashMap<String, Object>) element).values());
// System.out.println("Dimension: " + result.size());
@ -174,13 +178,11 @@ public class DatabaseOperations {
if (dbType.equals(POSTGRES)) {
explain = String.format(explainQueryPostgres, tablename);
}
if (dbType.equals(MYSQL)) {
explain = String.format(explainQueryMysql, tablename);
}
@ -219,8 +221,6 @@ public class DatabaseOperations {
count = value.longValue();
}
if (dbType.equals(POSTGRES)) {
@ -237,6 +237,42 @@ public class DatabaseOperations {
count = value.longValue();
AnalysisLogger.getLogger().debug(
"DatabaseOperations-> rows' number with explain function: "
+ count);
// 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.
if (count < 100000) {
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));
List<Object> resultRowsNumber;
resultRowsNumber = connection.executeQuery(String
.format(ActualRowsNumberQueryPostgres,
tablename), session);
if (resultRowsNumber != null) {
Object elem = resultRowsNumber.get(0);
ArrayList<Object> listValues = new ArrayList<Object>(
((LinkedHashMap<String, Object>) elem)
.values());
Long numElemValue = Long.valueOf(listValues.get(0)
.toString());
count = numElemValue.longValue();
}
}
}
}