我正在使用zend框架2进行php项目。
我有一个函数,可以运行近300个查询,我使用zf2 tablegateway进行查询。 我的主要问题是完成此操作需要大约120秒。 在使用Byprofiler和Zenddeveloper工具进行分析并提取所有查询之后,我将查询作为独立测试,所有这些查询都需要0.5秒来完成,因此我的结论是tablegateway或zf2增加了119秒的重载。
发生了什么,我该怎样避免这种情况?有关使用zf2的任何建议吗?
修改 以下是查询集:
SELECT `id` FROM `custom_field` WHERE `name` = 'color_id' AND `container_class` = 'product';
SELECT `id` FROM `custom_field` WHERE `name` = 'size_label1' AND `container_class` = 'product';
SELECT `link_id` FROM `container_link` WHERE `container_id` = 70127 AND `reason` = 'product_main_text';
SELECT `value` FROM `custom_field_int` WHERE `container_id` = 70128 AND `custom_field_id` = 334;
SELECT `value` FROM `custom_field_string` WHERE `container_id` = 70128 AND `custom_field_id` = 397;
SELECT `id` FROM `custom_field` WHERE `name` = 'color_id' AND `container_class` = 'product';
SELECT `id` FROM `custom_field` WHERE `name` = 'size_label1' AND `container_class` = 'product';
SELECT `link_id` FROM `container_link` WHERE `container_id` = 69917 AND `reason` = 'product_main_text';
SELECT `value` FROM `custom_field_int` WHERE `container_id` = 69918 AND `custom_field_id` = 334;
SELECT `value` FROM `custom_field_string` WHERE `container_id` = 69918 AND `custom_field_id` = 397;
SELECT `link_id` FROM `container_link` WHERE `container_id` = 69939 AND `reason` = 'product_main_text';
SELECT `value` FROM `custom_field_int` WHERE `container_id` = 69940 AND `custom_field_id` = 334;
SELECT `value` FROM `custom_field_string` WHERE `container_id` = 69940 AND `custom_field_id` = 397;
SELECT `link_id` FROM `container_link` WHERE `container_id` = 69925 AND `reason` = 'product_main_text';
SELECT `value` FROM `custom_field_int` WHERE `container_id` = 69926 AND `custom_field_id` = 334;
SELECT `value` FROM `custom_field_string` WHERE `container_id` = 69926 AND `custom_field_id` = 397;
SELECT `link_id` FROM `container_link` WHERE `container_id` = 69947 AND `reason` = 'product_main_text';
SELECT `value` FROM `custom_field_int` WHERE `container_id` = 69948 AND `custom_field_id` = 334;
SELECT `value` FROM `custom_field_string` WHERE `container_id` = 69948 AND `custom_field_id` = 397;
SELECT `link_id` FROM `container_link` WHERE `container_id` = 69955 AND `reason` = 'product_main_text';
SELECT `value` FROM `custom_field_int` WHERE `container_id` = 69956 AND `custom_field_id` = 334;
SELECT `value` FROM `custom_field_string` WHERE `container_id` = 69956 AND `custom_field_id` = 397;
SELECT `link_id` FROM `container_link` WHERE `container_id` = 69933 AND `reason` = 'product_main_text';
SELECT `value` FROM `custom_field_int` WHERE `container_id` = 69934 AND `custom_field_id` = 334;
SELECT `value` FROM `custom_field_string` WHERE `container_id` = 69934 AND `custom_field_id` = 397;
SELECT `link_id` FROM `container_link` WHERE `container_id` = 69923 AND `reason` = 'product_main_text';
SELECT `value` FROM `custom_field_int` WHERE `container_id` = 69924 AND `custom_field_id` = 334;
SELECT `value` FROM `custom_field_string` WHERE `container_id` = 69924 AND `custom_field_id` = 397;
SELECT `link_id` FROM `container_link` WHERE `container_id` = 69945 AND `reason` = 'product_main_text';
SELECT `value` FROM `custom_field_int` WHERE `container_id` = 69946 AND `custom_field_id` = 334;
SELECT `value` FROM `custom_field_string` WHERE `container_id` = 69946 AND `custom_field_id` = 397;
SELECT `link_id` FROM `container_link` WHERE `container_id` = 69931 AND `reason` = 'product_main_text';
SELECT `value` FROM `custom_field_int` WHERE `container_id` = 69932 AND `custom_field_id` = 334;
SELECT `value` FROM `custom_field_string` WHERE `container_id` = 69932 AND `custom_field_id` = 397;
SELECT `link_id` FROM `container_link` WHERE `container_id` = 69953 AND `reason` = 'product_main_text';
SELECT `value` FROM `custom_field_int` WHERE `container_id` = 69954 AND `custom_field_id` = 334;
SELECT `value` FROM `custom_field_string` WHERE `container_id` = 69954 AND `custom_field_id` = 397;
SELECT `link_id` FROM `container_link` WHERE `container_id` = 69921 AND `reason` = 'product_main_text';
SELECT `value` FROM `custom_field_int` WHERE `container_id` = 69922 AND `custom_field_id` = 334;
SELECT `value` FROM `custom_field_string` WHERE `container_id` = 69922 AND `custom_field_id` = 397;
SELECT `link_id` FROM `container_link` WHERE `container_id` = 69943 AND `reason` = 'product_main_text';
SELECT `value` FROM `custom_field_int` WHERE `container_id` = 69944 AND `custom_field_id` = 334;
SELECT `value` FROM `custom_field_string` WHERE `container_id` = 69944 AND `custom_field_id` = 397;
SELECT `link_id` FROM `container_link` WHERE `container_id` = 69929 AND `reason` = 'product_main_text';
SELECT `value` FROM `custom_field_int` WHERE `container_id` = 69930 AND `custom_field_id` = 334;
SELECT `value` FROM `custom_field_string` WHERE `container_id` = 69930 AND `custom_field_id` = 397;
SELECT `link_id` FROM `container_link` WHERE `container_id` = 69951 AND `reason` = 'product_main_text';
SELECT `value` FROM `custom_field_int` WHERE `container_id` = 69952 AND `custom_field_id` = 334;
SELECT `value` FROM `custom_field_string` WHERE `container_id` = 69952 AND `custom_field_id` = 397;
SELECT `link_id` FROM `container_link` WHERE `container_id` = 69937 AND `reason` = 'product_main_text';
SELECT `value` FROM `custom_field_int` WHERE `container_id` = 69938 AND `custom_field_id` = 334;
SELECT `value` FROM `custom_field_string` WHERE `container_id` = 69938 AND `custom_field_id` = 397;
SELECT `link_id` FROM `container_link` WHERE `container_id` = 69959 AND `reason` = 'product_main_text';
SELECT `value` FROM `custom_field_int` WHERE `container_id` = 69960 AND `custom_field_id` = 334;
SELECT `value` FROM `custom_field_string` WHERE `container_id` = 69960 AND `custom_field_id` = 397;
SELECT `link_id` FROM `container_link` WHERE `container_id` = 69915 AND `reason` = 'product_main_text';
SELECT `value` FROM `custom_field_int` WHERE `container_id` = 69916 AND `custom_field_id` = 334;
SELECT `value` FROM `custom_field_string` WHERE `container_id` = 69916 AND `custom_field_id` = 397;
SELECT `link_id` FROM `container_link` WHERE `container_id` = 69919 AND `reason` = 'product_main_text';
SELECT `value` FROM `custom_field_int` WHERE `container_id` = 69920 AND `custom_field_id` = 334;
SELECT `value` FROM `custom_field_string` WHERE `container_id` = 69920 AND `custom_field_id` = 397;
SELECT `link_id` FROM `container_link` WHERE `container_id` = 69941 AND `reason` = 'product_main_text';
SELECT `value` FROM `custom_field_int` WHERE `container_id` = 69942 AND `custom_field_id` = 334;
SELECT `value` FROM `custom_field_string` WHERE `container_id` = 69942 AND `custom_field_id` = 397;
SELECT `link_id` FROM `container_link` WHERE `container_id` = 69927 AND `reason` = 'product_main_text';
SELECT `value` FROM `custom_field_int` WHERE `container_id` = 69928 AND `custom_field_id` = 334;
SELECT `value` FROM `custom_field_string` WHERE `container_id` = 69928 AND `custom_field_id` = 397;
SELECT `link_id` FROM `container_link` WHERE `container_id` = 69949 AND `reason` = 'product_main_text';
SELECT `value` FROM `custom_field_int` WHERE `container_id` = 69950 AND `custom_field_id` = 334;
SELECT `value` FROM `custom_field_string` WHERE `container_id` = 69950 AND `custom_field_id` = 397;
SELECT `link_id` FROM `container_link` WHERE `container_id` = 69957 AND `reason` = 'product_main_text';
SELECT `value` FROM `custom_field_int` WHERE `container_id` = 69958 AND `custom_field_id` = 334;
SELECT `value` FROM `custom_field_string` WHERE `container_id` = 69958 AND `custom_field_id` = 397;
SELECT `link_id` FROM `container_link` WHERE `container_id` = 69913 AND `reason` = 'product_main_text';
SELECT `value` FROM `custom_field_int` WHERE `container_id` = 69914 AND `custom_field_id` = 334;
SELECT `value` FROM `custom_field_string` WHERE `container_id` = 69914 AND `custom_field_id` = 397;
SELECT `link_id` FROM `container_link` WHERE `container_id` = 69935 AND `reason` = 'product_main_text';
SELECT `value` FROM `custom_field_int` WHERE `container_id` = 69936 AND `custom_field_id` = 334;
SELECT `value` FROM `custom_field_string` WHERE `container_id` = 69936 AND `custom_field_id` = 397;
SELECT `id` FROM `custom_field` WHERE `name` = 'color_id' AND `container_class` = 'product';
SELECT `id` FROM `custom_field` WHERE `name` = 'size_label1' AND `container_class` = 'product';
SELECT `link_id` FROM `container_link` WHERE `container_id` = 70127 AND `reason` = 'product_main_text';
SELECT `value` FROM `custom_field_int` WHERE `container_id` = 70128 AND `custom_field_id` = 334;
SELECT `value` FROM `custom_field_string` WHERE `container_id` = 70128 AND `custom_field_id` = 397;
SELECT `id` FROM `custom_field` WHERE `name` = 'color_id' AND `container_class` = 'product';
SELECT `id` FROM `custom_field` WHERE `name` = 'size_label1' AND `container_class` = 'product';
SELECT `link_id` FROM `container_link` WHERE `container_id` = 70141 AND `reason` = 'product_main_text';
SELECT `value` FROM `custom_field_int` WHERE `container_id` = 70142 AND `custom_field_id` = 334;
SELECT `value` FROM `custom_field_string` WHERE `container_id` = 70142 AND `custom_field_id` = 397;
SELECT `link_id` FROM `container_link` WHERE `container_id` = 70147 AND `reason` = 'product_main_text';
SELECT `value` FROM `custom_field_int` WHERE `container_id` = 70148 AND `custom_field_id` = 334;
SELECT `value` FROM `custom_field_string` WHERE `container_id` = 70148 AND `custom_field_id` = 397;
SELECT `link_id` FROM `container_link` WHERE `container_id` = 70169 AND `reason` = 'product_main_text';
SELECT `value` FROM `custom_field_int` WHERE `container_id` = 70170 AND `custom_field_id` = 334;
SELECT `value` FROM `custom_field_string` WHERE `container_id` = 70170 AND `custom_field_id` = 397;
SELECT `link_id` FROM `container_link` WHERE `container_id` = 70155 AND `reason` = 'product_main_text';
SELECT `value` FROM `custom_field_int` WHERE `container_id` = 70156 AND `custom_field_id` = 334;
SELECT `value` FROM `custom_field_string` WHERE `container_id` = 70156 AND `custom_field_id` = 397;
SELECT `link_id` FROM `container_link` WHERE `container_id` = 70163 AND `reason` = 'product_main_text';
SELECT `value` FROM `custom_field_int` WHERE `container_id` = 70164 AND `custom_field_id` = 334;
SELECT `value` FROM `custom_field_string` WHERE `container_id` = 70164 AND `custom_field_id` = 397;
SELECT `link_id` FROM `container_link` WHERE `container_id` = 70153 AND `reason` = 'product_main_text';
SELECT `value` FROM `custom_field_int` WHERE `container_id` = 70154 AND `custom_field_id` = 334;
SELECT `value` FROM `custom_field_string` WHERE `container_id` = 70154 AND `custom_field_id` = 397;
SELECT `link_id` FROM `container_link` WHERE `container_id` = 70161 AND `reason` = 'product_main_text';
SELECT `value` FROM `custom_field_int` WHERE `container_id` = 70162 AND `custom_field_id` = 334;
SELECT `value` FROM `custom_field_string` WHERE `container_id` = 70162 AND `custom_field_id` = 397;
SELECT `link_id` FROM `container_link` WHERE `container_id` = 70145 AND `reason` = 'product_main_text';
SELECT `value` FROM `custom_field_int` WHERE `container_id` = 70146 AND `custom_field_id` = 334;
SELECT `value` FROM `custom_field_string` WHERE `container_id` = 70146 AND `custom_field_id` = 397;
SELECT `link_id` FROM `container_link` WHERE `container_id` = 70151 AND `reason` = 'product_main_text';
SELECT `value` FROM `custom_field_int` WHERE `container_id` = 70152 AND `custom_field_id` = 334;
SELECT `value` FROM `custom_field_string` WHERE `container_id` = 70152 AND `custom_field_id` = 397;
SELECT `link_id` FROM `container_link` WHERE `container_id` = 70159 AND `reason` = 'product_main_text';
SELECT `value` FROM `custom_field_int` WHERE `container_id` = 70160 AND `custom_field_id` = 334;
SELECT `value` FROM `custom_field_string` WHERE `container_id` = 70160 AND `custom_field_id` = 397;
SELECT `link_id` FROM `container_link` WHERE `container_id` = 70167 AND `reason` = 'product_main_text';
SELECT `value` FROM `custom_field_int` WHERE `container_id` = 70168 AND `custom_field_id` = 334;
SELECT `value` FROM `custom_field_string` WHERE `container_id` = 70168 AND `custom_field_id` = 397;
SELECT `link_id` FROM `container_link` WHERE `container_id` = 70143 AND `reason` = 'product_main_text';
SELECT `value` FROM `custom_field_int` WHERE `container_id` = 70144 AND `custom_field_id` = 334;
SELECT `value` FROM `custom_field_string` WHERE `container_id` = 70144 AND `custom_field_id` = 397;
SELECT `link_id` FROM `container_link` WHERE `container_id` = 70149 AND `reason` = 'product_main_text';
SELECT `value` FROM `custom_field_int` WHERE `container_id` = 70150 AND `custom_field_id` = 334;
SELECT `value` FROM `custom_field_string` WHERE `container_id` = 70150 AND `custom_field_id` = 397;
SELECT `link_id` FROM `container_link` WHERE `container_id` = 70157 AND `reason` = 'product_main_text';
SELECT `value` FROM `custom_field_int` WHERE `container_id` = 70158 AND `custom_field_id` = 334;
SELECT `value` FROM `custom_field_string` WHERE `container_id` = 70158 AND `custom_field_id` = 397;
SELECT `link_id` FROM `container_link` WHERE `container_id` = 70165 AND `reason` = 'product_main_text';
SELECT `value` FROM `custom_field_int` WHERE `container_id` = 70166 AND `custom_field_id` = 334;
SELECT `value` FROM `custom_field_string` WHERE `container_id` = 70166 AND `custom_field_id` = 397;
SELECT `id` FROM `custom_field` WHERE `name` = 'color_id' AND `container_class` = 'product';
SELECT `id` FROM `custom_field` WHERE `name` = 'size_label1' AND `container_class` = 'product';
SELECT `link_id` FROM `container_link` WHERE `container_id` = 69969 AND `reason` = 'product_main_text';
SELECT `value` FROM `custom_field_int` WHERE `container_id` = 69970 AND `custom_field_id` = 334;
SELECT `value` FROM `custom_field_string` WHERE `container_id` = 69970 AND `custom_field_id` = 397;
SELECT `link_id` FROM `container_link` WHERE `container_id` = 69963 AND `reason` = 'product_main_text';
SELECT `value` FROM `custom_field_int` WHERE `container_id` = 69964 AND `custom_field_id` = 334;
SELECT `value` FROM `custom_field_string` WHERE `container_id` = 69964 AND `custom_field_id` = 397;
SELECT `link_id` FROM `container_link` WHERE `container_id` = 69961 AND `reason` = 'product_main_text';
SELECT `value` FROM `custom_field_int` WHERE `container_id` = 69962 AND `custom_field_id` = 334;
SELECT `value` FROM `custom_field_string` WHERE `container_id` = 69962 AND `custom_field_id` = 397;
SELECT `link_id` FROM `container_link` WHERE `container_id` = 69967 AND `reason` = 'product_main_text';
SELECT `value` FROM `custom_field_int` WHERE `container_id` = 69968 AND `custom_field_id` = 334;
SELECT `value` FROM `custom_field_string` WHERE `container_id` = 69968 AND `custom_field_id` = 397;
SELECT `link_id` FROM `container_link` WHERE `container_id` = 69971 AND `reason` = 'product_main_text';
SELECT `value` FROM `custom_field_int` WHERE `container_id` = 69972 AND `custom_field_id` = 334;
SELECT `value` FROM `custom_field_string` WHERE `container_id` = 69972 AND `custom_field_id` = 397;
SELECT `link_id` FROM `container_link` WHERE `container_id` = 69965 AND `reason` = 'product_main_text';
SELECT `value` FROM `custom_field_int` WHERE `container_id` = 69966 AND `custom_field_id` = 334;
SELECT `value` FROM `custom_field_string` WHERE `container_id` = 69966 AND `custom_field_id` = 397;
SELECT `id` FROM `custom_field` WHERE `name` = 'color_id' AND `container_class` = 'product';
SELECT `id` FROM `custom_field` WHERE `name` = 'size_label1' AND `container_class` = 'product';
SELECT `link_id` FROM `container_link` WHERE `container_id` = 70135 AND `reason` = 'product_main_text';
SELECT `value` FROM `custom_field_int` WHERE `container_id` = 70136 AND `custom_field_id` = 334;
SELECT `value` FROM `custom_field_string` WHERE `container_id` = 70136 AND `custom_field_id` = 397;
SELECT `link_id` FROM `container_link` WHERE `container_id` = 70129 AND `reason` = 'product_main_text';
SELECT `value` FROM `custom_field_int` WHERE `container_id` = 70130 AND `custom_field_id` = 334;
SELECT `value` FROM `custom_field_string` WHERE `container_id` = 70130 AND `custom_field_id` = 397;
SELECT `link_id` FROM `container_link` WHERE `container_id` = 70139 AND `reason` = 'product_main_text';
SELECT `value` FROM `custom_field_int` WHERE `container_id` = 70140 AND `custom_field_id` = 334;
SELECT `value` FROM `custom_field_string` WHERE `container_id` = 70140 AND `custom_field_id` = 397;
SELECT `link_id` FROM `container_link` WHERE `container_id` = 70133 AND `reason` = 'product_main_text';
SELECT `value` FROM `custom_field_int` WHERE `container_id` = 70134 AND `custom_field_id` = 334;
SELECT `value` FROM `custom_field_string` WHERE `container_id` = 70134 AND `custom_field_id` = 397;
SELECT `link_id` FROM `container_link` WHERE `container_id` = 70137 AND `reason` = 'product_main_text';
SELECT `value` FROM `custom_field_int` WHERE `container_id` = 70138 AND `custom_field_id` = 334;
SELECT `value` FROM `custom_field_string` WHERE `container_id` = 70138 AND `custom_field_id` = 397;
SELECT `link_id` FROM `container_link` WHERE `container_id` = 70131 AND `reason` = 'product_main_text';
SELECT `value` FROM `custom_field_int` WHERE `container_id` = 70132 AND `custom_field_id` = 334;
SELECT `value` FROM `custom_field_string` WHERE `container_id` = 70132 AND `custom_field_id` = 397;
请注意,由于数据模型的复杂性,这些查询无法以任何方式加入或优化(如果是这样,我会破坏61个加入限制)。
EDIT2 以下是调用retrive值的主要方法:
public function findTreeValues($table, $container_id){
$ret = array();
$sql = new Sql($this->tableGateway->adapter);
$select = new \Zend\Db\Sql\Select;
$select->columns( array('value') );
$select->from(array('a'=> $table));
$select->join(array('b'=>'field_set_value'), new Expression("a.value = b.id "), array('label', 'custom_field_id'));
$select->where( array('a.container_id' => $container_id) );
$select->order('b.label');
$statement = $sql->prepareStatementForSqlObject($select);
//echo $sql->getSqlStringForSqlObject($select);
$values = $statement->execute();
foreach ($values as $v){
$ret[$v['custom_field_id']][$v['value']] = $v['label'];
}
return $ret;
}
public function getValue($custom_field_id, $container_id){
$obj = $this->find($custom_field_id, $container_id);
return $obj ? $obj->value : null;
}
答案 0 :(得分:2)
经过调试和调查,我发现ZF2 tablegateway对从db中恢复的数据进行类型解析。对于每个数据,例如对于INT类型的ID或对于字符串类型的字段,它将进行类型转换为正确的php类型。
通常,当你使用mysql从db中选择时,返回数据总是字符串。 这似乎是缓慢的部分:
protected function executeSelect(Select $select)
{
$selectState = $select->getRawState();
if ($selectState['table'] != $this->table) {
throw new Exception\RuntimeException('The table name of the provided select object must match that of the table');
}
if ($selectState['columns'] == array(Select::SQL_STAR)
&& $this->columns !== array()) {
$select->columns($this->columns);
}
// apply preSelect features
$this->featureSet->apply('preSelect', array($select));
// prepare and execute
$statement = $this->sql->prepareStatementForSqlObject($select);
$result = $statement->execute();
// build result set
$resultSet = clone $this->resultSetPrototype;
$resultSet->initialize($result);
// apply postSelect features
$this->featureSet->apply('postSelect', array($statement, $result, $resultSet));
return $resultSet;
}
通过简单扩展TableGateway类并删除以下内容来覆盖此方法:
//构建结果集 $ resultSet = clone $ this-> resultSetPrototype; $ resultSet->初始化($结果); $ this-> featureSet-> apply(' postSelect',array($ statement,$ result,$ resultSet));
我的应用程序得到了令人难以置信的提升。现在即使复杂的查询也非常快。希望这会对某人有所帮助。