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";
|
||||||
|
@ -107,8 +112,8 @@ public class DatabaseOperations {
|
||||||
}
|
}
|
||||||
|
|
||||||
// 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;
|
||||||
|
@ -117,15 +122,15 @@ public class DatabaseOperations {
|
||||||
|
|
||||||
if (dbType.equals(POSTGRES)) {
|
if (dbType.equals(POSTGRES)) {
|
||||||
|
|
||||||
//the full name equal to "schemaname.tablename"
|
// the full name equal to "schemaname.tablename"
|
||||||
tablename=schemaName+"."+ "\"" +tablename+ "\"";
|
tablename = schemaName + "." + "\"" + tablename + "\"";
|
||||||
|
|
||||||
countingQuery = String.format(QueryPostgres, tablename);
|
countingQuery = String.format(QueryPostgres, tablename);
|
||||||
|
|
||||||
}
|
}
|
||||||
if (dbType.equals(MYSQL)) {
|
if (dbType.equals(MYSQL)) {
|
||||||
//the full name equal to "dbname.tablename"
|
// the full name equal to "dbname.tablename"
|
||||||
tablename=schemaName+"."+tablename;
|
tablename = schemaName + "." + tablename;
|
||||||
|
|
||||||
countingQuery = String.format(QueryMysql, tablename);
|
countingQuery = String.format(QueryMysql, tablename);
|
||||||
|
|
||||||
|
@ -147,15 +152,14 @@ public class DatabaseOperations {
|
||||||
|
|
||||||
Object element = result.get(0);
|
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());
|
// System.out.println("Dimension: " + result.size());
|
||||||
|
|
||||||
// Integer numElem = Integer.valueOf(result.get(0).toString());
|
// Integer numElem = Integer.valueOf(result.get(0).toString());
|
||||||
|
|
||||||
// Long numElemvalue = Long.valueOf(result.get(0).toString());
|
// Long numElemvalue = Long.valueOf(result.get(0).toString());
|
||||||
|
|
||||||
Long numElemvalue = Long.valueOf(listvalues.get(0).toString());
|
Long numElemvalue = Long.valueOf(listvalues.get(0).toString());
|
||||||
|
|
||||||
|
@ -174,13 +178,11 @@ public class DatabaseOperations {
|
||||||
|
|
||||||
if (dbType.equals(POSTGRES)) {
|
if (dbType.equals(POSTGRES)) {
|
||||||
|
|
||||||
|
|
||||||
explain = String.format(explainQueryPostgres, tablename);
|
explain = String.format(explainQueryPostgres, tablename);
|
||||||
|
|
||||||
}
|
}
|
||||||
if (dbType.equals(MYSQL)) {
|
if (dbType.equals(MYSQL)) {
|
||||||
|
|
||||||
|
|
||||||
explain = String.format(explainQueryMysql, tablename);
|
explain = String.format(explainQueryMysql, tablename);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -202,25 +204,23 @@ public class DatabaseOperations {
|
||||||
|
|
||||||
if (dbType.equals(MYSQL)) {
|
if (dbType.equals(MYSQL)) {
|
||||||
|
|
||||||
// Object[] resultArray = (Object[]) (resultinfo.get(0));
|
// Object[] resultArray = (Object[]) (resultinfo.get(0));
|
||||||
|
|
||||||
Object elem = resultinfo.get(0);
|
Object elem = resultinfo.get(0);
|
||||||
|
|
||||||
ArrayList<Object> values = new ArrayList<Object>(
|
ArrayList<Object> values = new ArrayList<Object>(
|
||||||
((LinkedHashMap<String, Object>) elem).values());
|
((LinkedHashMap<String, Object>) elem).values());
|
||||||
|
|
||||||
// //print check
|
// //print check
|
||||||
// AnalysisLogger.getLogger().debug(
|
// AnalysisLogger.getLogger().debug(
|
||||||
// "DatabaseOperations->VALUE: " + values);
|
// "DatabaseOperations->VALUE: " + values);
|
||||||
|
|
||||||
BigInteger value = (BigInteger) values.get(8);
|
BigInteger value = (BigInteger) values.get(8);
|
||||||
|
|
||||||
// BigInteger value = (BigInteger) resultArray[8];
|
// BigInteger value = (BigInteger) resultArray[8];
|
||||||
|
|
||||||
count = value.longValue();
|
count = value.longValue();
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if (dbType.equals(POSTGRES)) {
|
if (dbType.equals(POSTGRES)) {
|
||||||
|
@ -237,6 +237,42 @@ public class DatabaseOperations {
|
||||||
|
|
||||||
count = value.longValue();
|
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();
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue