thinkcmf 执行sql优化
\simplewind\thinkphp\library\think\db\Query.php
//优化选项内容 public function beautifyOptions(){ $data_options = $this->getOptions(); //join优化 if(!empty($data_options['join'])){ $join = $data_options['join']; $options = $this->parseExpress(); $this->options = $options; // 生成查询SQL $sql = $this->builder->selectTj($options); // 获取参数绑定 $bind = $this->getBind(); // 获取实际执行的SQL语句 $sql = $this->connection->getRealSql($sql, $bind); if($sql){ $sql = str_replace('`','',$sql); //INNER不处理 if($join){ $is_change = 0; foreach ($join as $k=>$val){ if ($val[1] != 'LEFT') continue; $isneed = 0; if(is_array($val[0])){ foreach ($val[0] as $tmptable=>$tmpalias){ if(strpos($sql,','.$tmpalias.'.')!==false || strpos($sql,' '.$tmpalias.'.')!=false) { $isneed = 1; break; } } if($isneed){ continue; }else{ unset($join[$k]); $is_change = 1; } } } $is_change and $join = array_values(array_filter($join)); } } $this->options['join'] = $join; } }
\simplewind\thinkphp\library\think\db\Builder.php
/** * 生成查询SQL * @access public * @param array $options 表达式 * @return string */ public function selectTj($options = []) { $sql = str_replace( ['%TABLE%', '%DISTINCT%', '%FIELD%', '%WHERE%', '%GROUP%', '%HAVING%', '%ORDER%', '%LIMIT%', '%UNION%', '%LOCK%', '%COMMENT%', '%FORCE%'], [ $this->parseTable($options['table'], $options), $this->parseDistinct($options['distinct']), $this->parseField($options['field'], $options), $this->parseWhere($options['where'], $options), $this->parseGroup($options['group']), $this->parseHaving($options['having']), $this->parseOrder($options['order'], $options), $this->parseLimit($options['limit']), $this->parseUnion($options['union']), $this->parseLock($options['lock']), $this->parseComment($options['comment']), $this->parseForce($options['force']), ], $this->selectSql); return $sql; }