Skip to content

Commit

Permalink
fixed view's select SQLs - moved UUID filtering closer to T_WEBUI_Vie…
Browse files Browse the repository at this point in the history
…wSelection table

because it seems that PostgreSQL it's building a fucked up plan in case
some columns are using window functions.

#437
  • Loading branch information
teosarca committed Jun 9, 2017
1 parent 76a3c3a commit 67ca5e3
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -97,22 +97,22 @@ private SqlViewBinding(final Builder builder)
sqlWhereClause = builder.getSqlWhereClause();
sqlSelectByPage = sqlSelect.toComposer()
.append("\n WHERE ")
.append("\n " + SqlViewSelectionQueryBuilder.COLUMNNAME_Paging_UUID + "=?")
.append("\n AND " + SqlViewSelectionQueryBuilder.COLUMNNAME_Paging_SeqNo + " BETWEEN ? AND ?")
// .append("\n " + SqlViewSelectionQueryBuilder.COLUMNNAME_Paging_UUID + "=?") // already filtered above
.append("\n " + SqlViewSelectionQueryBuilder.COLUMNNAME_Paging_SeqNo + " BETWEEN ? AND ?")
.append("\n ORDER BY " + SqlViewSelectionQueryBuilder.COLUMNNAME_Paging_SeqNo)
.build();

sqlSelectById = sqlSelect.toComposer()
.append("\n WHERE ")
.append("\n " + SqlViewSelectionQueryBuilder.COLUMNNAME_Paging_UUID + "=?")
.append("\n AND " + SqlViewSelectionQueryBuilder.COLUMNNAME_Paging_Record_ID + "=?")
// .append("\n " + SqlViewSelectionQueryBuilder.COLUMNNAME_Paging_UUID + "=?") // already filtered above
.append("\n " + SqlViewSelectionQueryBuilder.COLUMNNAME_Paging_Record_ID + "=?")
.build();

sqlSelectLinesByRowIds = SqlViewSelectionQueryBuilder.buildSqlSelectLines(_tableName, _tableAlias, _keyField.getColumnName(), displayFieldNames, allFields)
.toComposer()
.append("\n WHERE ")
.append("\n " + SqlViewSelectionQueryBuilder.COLUMNNAME_Paging_UUID + "=?")
.append("\n AND " + SqlViewSelectionQueryBuilder.COLUMNNAME_Paging_Record_ID + " IN ").append(SqlViewSelectionQueryBuilder.Paging_Record_IDsPlaceholder)
// .append("\n " + SqlViewSelectionQueryBuilder.COLUMNNAME_Paging_UUID + "=?") // already filtered above
.append("\n " + SqlViewSelectionQueryBuilder.COLUMNNAME_Paging_Record_ID + " IN ").append(SqlViewSelectionQueryBuilder.Paging_Record_IDsPlaceholder)
.build();

final List<SqlViewRowFieldLoader> rowFieldLoaders = new ArrayList<>(allFields.size());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -682,6 +682,9 @@ public <T> IQueryFilter<T> buildInSelectionQueryFilter(final String selectionId)
return TypedSqlQueryFilter.of(sql, sqlParams);
}

/**
* SQL Parameters required: 1=UUID
*/
public static IStringExpression buildSqlSelect(
final String sqlTableName,
final String sqlTableAlias,
Expand Down Expand Up @@ -744,6 +747,8 @@ private static IStringExpression buildSqlSelect_WithoutGrouping(
.append("\n , sel." + I_T_WEBUI_ViewSelection.COLUMNNAME_Record_ID + " AS " + COLUMNNAME_Paging_Record_ID)
.append("\n FROM " + I_T_WEBUI_ViewSelection.Table_Name + " sel")
.append("\n LEFT OUTER JOIN " + sqlTableName + " ON (" + sqlTableName + "." + sqlKeyColumnName + " = sel." + I_T_WEBUI_ViewSelection.COLUMNNAME_Record_ID + ")")
// Filter by UUID. Keep this closer to the source table, see https://github.com/metasfresh/metasfresh-webui-api/issues/437
.append("\n WHERE sel." + I_T_WEBUI_ViewSelection.COLUMNNAME_UUID + "=?")
.append("\n ) " + sqlTableAlias); // FROM

return sql.build().caching();
Expand Down Expand Up @@ -827,6 +832,9 @@ else if (groupingBinding.isGroupBy(fieldName))
.append("\n INNER JOIN " + I_T_WEBUI_ViewSelectionLine.Table_Name + " sl on (sl.UUID=sel.UUID and sl.Record_ID=sel.Record_ID)")
.append("\n LEFT OUTER JOIN " + sqlTableName + " ON (" + sqlTableName + "." + sqlKeyColumnName + " = sl." + I_T_WEBUI_ViewSelectionLine.COLUMNNAME_Line_ID + ")")
//
// Filter by UUID. Keep this closer to the source table, see https://github.com/metasfresh/metasfresh-webui-api/issues/437
.append("\n WHERE sel." + I_T_WEBUI_ViewSelection.COLUMNNAME_UUID + "=?")
//
.append("\n GROUP BY ")
.append("\n sel." + I_T_WEBUI_ViewSelection.COLUMNNAME_Line)
.append("\n , sel." + I_T_WEBUI_ViewSelection.COLUMNNAME_UUID)
Expand Down Expand Up @@ -880,6 +888,8 @@ public static IStringExpression buildSqlSelectLines(
.append("\n , sl." + I_T_WEBUI_ViewSelectionLine.COLUMNNAME_Record_ID + " AS " + COLUMNNAME_Paging_Record_ID)
.append("\n FROM " + I_T_WEBUI_ViewSelectionLine.Table_Name + " sl")
.append("\n LEFT OUTER JOIN " + sqlTableName + " ON (" + sqlTableName + "." + sqlKeyColumnName + " = sl." + I_T_WEBUI_ViewSelectionLine.COLUMNNAME_Line_ID + ")")
// Filter by UUID. Keep this closer to the source table, see https://github.com/metasfresh/metasfresh-webui-api/issues/437
.append("\n WHERE sl." + I_T_WEBUI_ViewSelectionLine.COLUMNNAME_UUID + "=?")
.append("\n ) " + sqlTableAlias); // FROM

return sql.build().caching();
Expand Down

0 comments on commit 67ca5e3

Please sign in to comment.