Php 缓慢发现
我试图弄明白为什么我的一条教义运行得如此缓慢。我真的不知道从哪里开始,所以请容忍我 我做了一个非常基本的查找来获取用户对象。这项发现耗时约160毫秒。当我通过phpmyadmin运行查询时,需要.7毫秒Php 缓慢发现,php,mysql,doctrine,doctrine-orm,Php,Mysql,Doctrine,Doctrine Orm,我试图弄明白为什么我的一条教义运行得如此缓慢。我真的不知道从哪里开始,所以请容忍我 我做了一个非常基本的查找来获取用户对象。这项发现耗时约160毫秒。当我通过phpmyadmin运行查询时,需要.7毫秒 $this->em->find('Entities\User', $userId) 我已经尝试将跳过名称解析添加到mysql的my.cnf中。用户表中的id字段已编制索引。我真的不知道还能尝试什么。如果我能提供更多信息,请告诉我 以下是实体文件: namespace Entitie
$this->em->find('Entities\User', $userId)
我已经尝试将跳过名称解析添加到mysql的my.cnf中。用户表中的id字段已编制索引。我真的不知道还能尝试什么。如果我能提供更多信息,请告诉我 以下是实体文件:
namespace Entities;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\ORM\EntityRepository;
/** @Entity(repositoryClass = "Entities\UserRepository")
* @Table(name="user")
*/
class User extends \Company_Resource_AbstractEntity
{
/** @Id @Column(type="integer") @GeneratedValue */
protected $id;
/** @Column(type="string") */
protected $name;
/** @Column(type="string") */
protected $password;
/** @Column(type="string") */
protected $email;
/** @Column(type="string") */
protected $first_name;
/** @Column(type="string") */
protected $last_name;
/** @Column(type="integer") */
protected $password_reset;
/** @Column(type="string") */
protected $salt;
/** @Column(type="integer") */
protected $active;
/** @Column(type="string") */
protected $cookie_hash;
/**
* @ManyToOne(targetEntity="Company" , inversedBy="user")
*/
protected $company;
/**
* @ManyToOne(targetEntity="Privilege" , inversedBy="user")
*/
protected $privilege;
/**
* @OneToMany(targetEntity="CompanySubscription" , mappedBy="user")
*/
protected $subscription;
/**
* @OneToMany(targetEntity="EquipmentEvent" , mappedBy="check_in_user")
*/
protected $check_in;
/**
* @OneToMany(targetEntity="EquipmentEvent" , mappedBy="check_out_user")
*/
protected $check_out;
/**
* @OneToMany(targetEntity="GroupEvent" , mappedBy="check_in_user")
*/
protected $check_in_group;
/**
* @OneToMany(targetEntity="GroupEvent" , mappedBy="check_out_user")
*/
protected $check_out_group;
/**
* @OneToMany(targetEntity="Maintenance" , mappedBy="submit_user")
*/
protected $maintenance_submit;
/**
* @OneToMany(targetEntity="Maintenance" , mappedBy="completed_user")
*/
protected $maintenance_complete;
/**
* @OneToMany(targetEntity="UserLogin" , mappedBy="user")
*/
protected $login;
}
抽象实体:
use \Doctrine\Common\Collections\ArrayCollection;
abstract class Company_Resource_AbstractEntity implements ArrayAccess
{
public function offsetExists($offset)
{
return property_exists($this, $offset);
}
// The get/set functions should check to see if an appropriately named function exists before just returning the
// property. This way classes can control how data is returned from the object more completely.
public function offsetGet($offset)
{
$property = new Zend_Filter_Word_UnderscoreToCamelCase();
$method = 'get'. $property->filter($offset);
return $this->{$method}();
}
public function offsetSet($offset, $value)
{
$property = new Zend_Filter_Word_UnderscoreToCamelCase();
$method = 'set'. $property->filter($offset);
return $this->{$method}($value);
}
public function offsetUnset($offset)
{
// can't do this
}
/*==-====-====-====-====-====-====-====-====-====-====-==*/
/*
* Provides magic method access for getFieldName() and setFieldName()
* where field_name is a simple field and not a relation
* A special getData implementation returns all of the current object vars
*/
public function __call($method, $arguments)
{
preg_match('@^([a-z]+)(.*)@', $method, $matches);
$action = $matches[1];
$property = $matches[2];
$underscore = new Zend_Filter_Word_CamelCaseToUnderscore();
$offset = strtolower($underscore->filter($property));
if ($action == 'get')
{
if ($property == 'Data')
return get_object_vars($this);
if ($this->offsetExists($offset))
return $this->{$offset};
else
throw new Zend_Exception(sprintf("'%s' does not have property '%s'", get_class($this), $offset));
}
else if ($action == 'set')
{
if ($this->offsetExists($offset))
return $this->{$offset} = $arguments[0];
else
throw new Zend_Exception(sprintf("'%s' does not have property '%s'", get_class($this), $offset));
}
else
throw new Zend_Exception(sprintf("'%s' does not have method '%s'", get_class($this), $method));
}
}
查找生成的SQL:
SELECT t0.id AS id1,
t0.name AS name2,
t0.password AS password3,
t0.email AS email4,
t0.first_name AS first_name5,
t0.last_name AS last_name6,
t0.password_reset AS password_reset7,
t0.salt AS salt8,
t0.active AS active9,
t0.cookie_hash AS cookie_hash10,
t0.company_id AS company_id11,
t0.privilege_id AS privilege_id12
FROM user t0 WHERE t0.id = ?
有人认为有什么不对劲,或者知道该怎么做吗
使用原则2.2.2
使用phpmyadmin运行该查询时得到的解释:
表模式:我认为我的设置的问题是文件中的实际行数。教条每次都在读这些东西。我为元缓存启用了APC,在第一次加载后,加载时间大大缩短。如果没有查询或结果缓存,该查询实际上只需要大约6毫秒,这正是我一直以来的目标。但愿我能早点尝试。如果我是你,我会启用查询日志记录,通过它,你可以跟踪查询日志文件并查看正在执行的确切查询。也许在幕后发生了一些你不知道的事情。您没有标记RDBMS,但我假设是MySQL?如果是这样,您可以使用此处概述的步骤启用查询日志记录:。找到查询后,我将从MySQL命令行运行它,看看是否存在性能差异,并调整模式以提高性能。基本上,我不认为是代码问题导致了您的速度慢,我认为这与数据库/模式有关。所以我想在我们撕碎你的代码之前先把它作为干扰物去掉。啊,这很有道理。你可以尝试在应用程序级别登录。我使用Symfony/Doctrine构建了一个应用程序,发现它在每个表单保存中运行30个以上的查询。只有在跟踪查询日志之后,我才能优化查询/结构,这样表单保存速度更快,对数据库的影响更小。ORMS很好,但如果使用不当,可能会对性能造成严重破坏。该查询与其他标准查询类似。下一步是表模式,有没有可能发布?如果你不介意的话,你能针对条令给你的查询发布
解释吗?哇,模式很好,除了int(1)
,我通常使用tinyint(1)
。不确定A
对于索引部分的排序意味着什么,但我认为这并不重要。解释也很好。您能发布从模式生成的schema.yml
文件吗?您是说您认为速度慢是由于编译期间文件的大小造成的吗?APC是操作代码缓存,仅在文件不需要在后续请求中编译时才有帮助。我怀疑这是导致您速度缓慢的原因,但如果它对您有效…我真的不知道它可能是什么。如果您有一个开发环境,您可以跟踪查询日志,如我在注释中所述,然后您将看到针对每个请求执行的每个查询。我怀疑你的直觉可能是正确的,这是导致缓慢的关系之一。