Permalink
Browse files

Filter api: systematic use JOIN when building SQL

Do not join tables using the where clause, for better readability and
avoiding risk of issues with operator precedence and 'any condition'
filtering mode.

This commit also removes an unnecessary LEFT JOIN between the bugnote
and bugnote_text tables; since this is a strict 1:1 relationship, an
inner join is sufficient and yields better performance.
  • Loading branch information...
1 parent 44055f8 commit dd66cd8cdcb9e473f0c115e1d0a0a268e175ed8d @dregad dregad committed Mar 18, 2013
Showing with 8 additions and 7 deletions.
  1. +8 −7 core/filter_api.php
View
@@ -1116,7 +1116,6 @@ function filter_get_bug_rows( &$p_page_number, &$p_per_page, &$p_page_count, &$p
$t_project_where_clauses = array(
"$t_project_table.enabled = " . db_param(),
- "$t_project_table.id = $t_bug_table.project_id",
);
$t_where_params = array(
1,
@@ -1125,8 +1124,13 @@ function filter_get_bug_rows( &$p_page_number, &$p_per_page, &$p_page_count, &$p
"$t_bug_table.*",
);
- $t_join_clauses = array();
- $t_from_clauses = array();
+ $t_from_clauses = array(
+ $t_bug_table,
+ );
+
+ $t_join_clauses = array(
+ "JOIN $t_project_table ON $t_project_table.id = $t_bug_table.project_id",
+ );
// normalize the project filtering into an array $t_project_ids
if( 'simple' == $t_view_type ) {
@@ -2022,7 +2026,7 @@ function filter_get_bug_rows( &$p_page_number, &$p_per_page, &$p_page_count, &$p
if ( !$t_first ) {
$t_join_clauses[] = "JOIN $t_bug_text_table ON $t_bug_table.bug_text_id = $t_bug_text_table.id";
$t_join_clauses[] = "LEFT JOIN $t_bugnote_table ON $t_bug_table.id = $t_bugnote_table.bug_id";
- $t_join_clauses[] = "LEFT JOIN $t_bugnote_text_table ON $t_bugnote_table.bugnote_text_id = $t_bugnote_text_table.id";
+ $t_join_clauses[] = "JOIN $t_bugnote_text_table ON $t_bugnote_table.bugnote_text_id = $t_bugnote_text_table.id";
$t_where_clauses[] = $t_textsearch_where_clause;
}
}
@@ -2037,9 +2041,6 @@ function filter_get_bug_rows( &$p_page_number, &$p_per_page, &$p_page_count, &$p
log_event(LOG_FILTERING, 'Join operator : ' . $t_join_operator);
- $t_from_clauses[] = $t_project_table;
- $t_from_clauses[] = $t_bug_table;
-
$t_query_clauses['select'] = $t_select_clauses;
$t_query_clauses['from'] = $t_from_clauses;
$t_query_clauses['join'] = $t_join_clauses;

0 comments on commit dd66cd8

Please sign in to comment.