add index hint of using "where" syntax

git-svn-id: http://xe-core.googlecode.com/svn/branches/1.5.0@9471 201d5d3c-b55e-5fd7-737f-ddc643e51545
This commit is contained in:
lickawtl 2011-10-03 17:28:53 +00:00
parent cebf35f92a
commit 0b8949cfeb
8 changed files with 51 additions and 42 deletions

View file

@ -616,7 +616,8 @@
if ($limit && $limit->isPageHandler()) { if ($limit && $limit->isPageHandler()) {
// Total count // Total count
$count_query = sprintf('select count(*) as "count" %s %s', 'FROM ' . $queryObject->getFromString(), ($queryObject->getWhereString() === '' ? '' : ' WHERE '. $queryObject->getWhereString())); $temp_where = $queryObject->getWhereString(true, false);
$count_query = sprintf('select count(*) as "count" %s %s', 'FROM ' . $queryObject->getFromString(), ($temp_where === '' ? '' : ' WHERE '. $temp_where));
if ($queryObject->getGroupByString() != '') { if ($queryObject->getGroupByString() != '') {
$count_query = sprintf('select count(*) as "count" from (%s) xet', $count_query); $count_query = sprintf('select count(*) as "count" from (%s) xet', $count_query);
} }
@ -683,11 +684,8 @@
$from = ' FROM '.$from; $from = ' FROM '.$from;
$where = $query->getWhereString($with_values); $where = $query->getWhereString($with_values);
if($where != ''){ if($where != '')
$where = ' WHERE ' . $where; $where = ' WHERE ' . $where;
if(strstr($where,'list_order')) $where .= ' and list_order < 2100000000 ';
if(strstr($where,'update_order')) $where .= ' and update_order < 2100000000 ';
}
$groupBy = $query->getGroupByString(); $groupBy = $query->getGroupByString();
if($groupBy != '') $groupBy = ' GROUP BY ' . $groupBy; if($groupBy != '') $groupBy = ' GROUP BY ' . $groupBy;
@ -696,23 +694,7 @@
if($orderBy != '') $orderBy = ' ORDER BY ' . $orderBy; if($orderBy != '') $orderBy = ' ORDER BY ' . $orderBy;
$limit = $query->getLimitString(); $limit = $query->getLimitString();
if ($limit != '') { if ($limit != '') $limit = sprintf (' LIMIT %d, %d', $start_count, $list_count);
if ($query->getLimit()) {
if($orderBy != '')
$limit = sprintf (' for orderby_num() between %d and %d', $start_count + 1, $list_count + $start_count);
else{
if($groupBy != '')
$limit = sprintf (' having groupby_num() between %d and %d', $start_count + 1, $list_count + $start_count);
else{
if ($where != '')
$limit = sprintf (' and inst_num() between %d and %d', $start_count + 1, $list_count + $start_count);
else
$limit = sprintf(' where inst_num() between %d and %d', $start_count + 1, $list_count + $start_count);
}
}
}
}
return $select . ' ' . $from . ' ' . $where . ' ' . $groupBy . ' ' . $orderBy . ' ' . $limit; return $select . ' ' . $from . ' ' . $where . ' ' . $groupBy . ' ' . $orderBy . ' ' . $limit;
} }

View file

@ -732,7 +732,8 @@
$limit = $queryObject->getLimit(); $limit = $queryObject->getLimit();
if ($limit && $limit->isPageHandler()) { if ($limit && $limit->isPageHandler()) {
// Total count // Total count
$count_query = sprintf('select count(*) as "count" %s %s', 'FROM ' . $queryObject->getFromString(), ($queryObject->getWhereString() === '' ? '' : ' WHERE ' . $queryObject->getWhereString())); $temp_where = $queryObject->getWhereString(true, false);
$count_query = sprintf('select count(*) as "count" %s %s', 'FROM ' . $queryObject->getFromString(), ($temp_where === '' ? '' : ' WHERE ' . $temp_where));
if ($queryObject->getGroupByString() != '') { if ($queryObject->getGroupByString() != '') {
$count_query = sprintf('select count(*) as "count" from (%s) xet', $count_query); $count_query = sprintf('select count(*) as "count" from (%s) xet', $count_query);
} }

View file

@ -492,7 +492,8 @@
$limit = $queryObject->getLimit(); $limit = $queryObject->getLimit();
if ($limit && $limit->isPageHandler()) { if ($limit && $limit->isPageHandler()) {
// Total count // Total count
$count_query = sprintf('select count(*) as "count" %s %s', 'FROM ' . $queryObject->getFromString(), ($queryObject->getWhereString() === '' ? '' : ' WHERE '. $queryObject->getWhereString())); $temp_where = $queryObject->getWhereString(true, false);
$count_query = sprintf('select count(*) as "count" %s %s', 'FROM ' . $queryObject->getFromString(), ($temp_where === '' ? '' : ' WHERE '. $temp_where));
if ($queryObject->getGroupByString() != '') { if ($queryObject->getGroupByString() != '') {
$count_query = sprintf('select count(*) as "count" from (%s) xet', $count_query); $count_query = sprintf('select count(*) as "count" from (%s) xet', $count_query);
} }

View file

@ -457,7 +457,8 @@
$limit = $queryObject->getLimit(); $limit = $queryObject->getLimit();
if ($limit && $limit->isPageHandler()) { if ($limit && $limit->isPageHandler()) {
// Total count // Total count
$count_query = sprintf('select count(*) as "count" %s %s', 'FROM ' . $queryObject->getFromString(), ($queryObject->getWhereString() === '' ? '' : ' WHERE '. $queryObject->getWhereString())); $temp_where = $queryObject->getWhereString(true, false);
$count_query = sprintf('select count(*) as "count" %s %s', 'FROM ' . $queryObject->getFromString(), ($temp_where === '' ? '' : ' WHERE '. $temp_where));
if ($queryObject->getGroupByString() != '') { if ($queryObject->getGroupByString() != '') {
$count_query = sprintf('select count(*) as "count" from (%s) xet', $count_query); $count_query = sprintf('select count(*) as "count" from (%s) xet', $count_query);
} }

View file

@ -558,7 +558,8 @@ class DBPostgresql extends DB
$limit = $queryObject->getLimit(); $limit = $queryObject->getLimit();
if ($limit && $limit->isPageHandler()) { if ($limit && $limit->isPageHandler()) {
// Total count // Total count
$count_query = sprintf('select count(*) as "count" %s %s', 'FROM ' . $queryObject->getFromString(), ($queryObject->getWhereString() === '' ? '' : ' WHERE '. $queryObject->getWhereString())); $temp_where = $queryObject->getWhereString(true, false);
$count_query = sprintf('select count(*) as "count" %s %s', 'FROM ' . $queryObject->getFromString(), ($temp_where === '' ? '' : ' WHERE '. $temp_where));
if ($queryObject->getGroupByString() != '') { if ($queryObject->getGroupByString() != '') {
$count_query = sprintf('select count(*) as "count" from (%s) xet', $count_query); $count_query = sprintf('select count(*) as "count" from (%s) xet', $count_query);
} }

View file

@ -478,7 +478,8 @@
function queryPageLimit($queryObject, $data) { function queryPageLimit($queryObject, $data) {
if ($queryObject->getLimit() && $queryObject->getLimit()->isPageHandler()) { if ($queryObject->getLimit() && $queryObject->getLimit()->isPageHandler()) {
// Total count // Total count
$count_query = sprintf('select count(*) as "count" %s %s', 'FROM ' . $queryObject->getFromString(), ($queryObject->getWhereString() === '' ? '' : ' WHERE ' . $queryObject->getWhereString())); $temp_where = $queryObject->getWhereString(true, false);
$count_query = sprintf('select count(*) as "count" %s %s', 'FROM ' . $queryObject->getFromString(), ($temp_where === '' ? '' : ' WHERE ' . $temp_where));
if ($queryObject->getGroupByString() != '') { if ($queryObject->getGroupByString() != '') {
$count_query = sprintf('select count(*) as "count" from (%s) xet', $count_query); $count_query = sprintf('select count(*) as "count" from (%s) xet', $count_query);
} }
@ -556,13 +557,8 @@
$from = ' FROM ' . $from; $from = ' FROM ' . $from;
$where = $query->getWhereString($with_values); $where = $query->getWhereString($with_values);
if ($where != '') { if ($where != '')
$where = ' WHERE ' . $where; $where = ' WHERE ' . $where;
if (strstr($where, 'list_order'))
$where .= ' and list_order < 2100000000 ';
if (strstr($where, 'update_order'))
$where .= ' and update_order < 2100000000 ';
}
$groupBy = $query->getGroupByString(); $groupBy = $query->getGroupByString();
if ($groupBy != '') if ($groupBy != '')

View file

@ -15,6 +15,8 @@
var $columnList = null; var $columnList = null;
var $_orderByString;
function Query($queryID = null function Query($queryID = null
, $action = null , $action = null
, $columns = null , $columns = null
@ -209,9 +211,10 @@
return $from; return $from;
} }
function getWhereString($with_values = true){ function getWhereString($with_values = true, $with_optimization = true){
$where = ''; $where = '';
$condition_count = 0; $condition_count = 0;
foreach($this->conditions as $conditionGroup){ foreach($this->conditions as $conditionGroup){
if($condition_count === 0){ if($condition_count === 0){
$conditionGroup->setPipe(""); $conditionGroup->setPipe("");
@ -220,7 +223,24 @@
$where .= $condition_string; $where .= $condition_string;
$condition_count++; $condition_count++;
} }
return trim($where);
if($with_optimization &&
(strstr($this->getOrderByString(), 'list_order') || strstr($this->getOrderByString(), 'update_order'))){
if($condition_count !== 0) $where = '(' . $where .') ';
foreach($this->orderby as $order){
$colName = $order->getColumnName();
if(strstr($colName, 'list_order') || strstr($colName, 'update_order')){
$opt_condition = new ConditionWithoutArgument($colName, 2100000000, 'less', 'and');
if ($condition_count === 0) $opt_condition->setPipe("");
$where .= $opt_condition->toString($with_values).' ';
$condition_count++;
}
}
}
return trim($where);
} }
function getGroupByString(){ function getGroupByString(){
@ -231,13 +251,16 @@
} }
function getOrderByString(){ function getOrderByString(){
if(count($this->orderby) === 0) return ''; if(!$this->_orderByString){
$orderBy = ''; if(count($this->orderby) === 0) return '';
foreach($this->orderby as $order){ $orderBy = '';
$orderBy .= $order->toString() .', '; foreach($this->orderby as $order){
$orderBy .= $order->toString() .', ';
}
$orderBy = substr($orderBy, 0, -2);
$this->_orderByString = $orderBy;
} }
$orderBy = substr($orderBy, 0, -2); return $this->_orderByString;
return $orderBy;
} }
function getLimit(){ function getLimit(){

View file

@ -9,12 +9,16 @@
} }
function toString(){ function toString(){
$result = is_a($this->column_name, 'Argument') ? $this->column_name->getValue() : $this->column_name; $result = $this->getColumnName();
$result .= ' '; $result .= ' ';
$result .= is_a($this->sort_order, 'Argument') ? $this->sort_order->getValue() : $this->sort_order; $result .= is_a($this->sort_order, 'Argument') ? $this->sort_order->getValue() : $this->sort_order;
return $result; return $result;
} }
function getColumnName(){
return is_a($this->column_name, 'Argument') ? $this->column_name->getValue() : $this->column_name;
}
function getArguments(){ function getArguments(){
$args = array(); $args = array();
if(is_a($this->column_name, 'Argument')) if(is_a($this->column_name, 'Argument'))