Zend Framework 和 Mysql - 非常慢
我正在使用 php、mysql 和 zend 框架创建一个网站。 当我尝试运行任何 sql 查询时,页面生成时间会跳至 0.5 秒左右。那太高了。如果我打开sql,页面生成是0.001。 我运行的查询量并没有真正影响页面生成时间(测试了 1-10 个查询)。停留在0.5秒 我不明白,我做错了什么。
我在 bootstrap 中连接到 sql:
protected function _initDatabase ()
{
try
{
$config = new Zend_Config_Ini( APPLICATION_PATH . '/configs/application.ini', APPLICATION_ENV );
$db = Zend_Db::factory( $config -> database);
Zend_DB_Table_Abstract::setDefaultAdapter( $db );
}
catch ( Zend_Db_Exception $e )
{
}
}
然后我有一个简单的模型
class StandardAccessory extends Zend_DB_Table_Abstract
{
/**
* The default table name
*/
protected $_name = 'standard_accessory';
protected $_primary = 'model';
protected $_sequence = false;
}
最后,在我的索引控制器中,我只运行 find 方法。
require_once APPLICATION_PATH . '/models/StandardAccessory.php';
$sa = new StandardAccessory( );
$stndacc = $sa->find( 'abc' );
所有这一切大约需要 0.5 秒,这太长了。有什么建议吗?
谢谢!
I am creating a web site using php, mysql and zend framework.
When I try to run any sql query, page generation jumps to around 0.5 seconds. That's too high. If i turn of sql, page generation is 0.001.
The amount of queries I run, doesn't really affect the page generation time (1-10 queries tested). Stays at 0.5 seconds
I can't figure out, what I am doing wrong.
I connect to sql in bootstrap:
protected function _initDatabase ()
{
try
{
$config = new Zend_Config_Ini( APPLICATION_PATH . '/configs/application.ini', APPLICATION_ENV );
$db = Zend_Db::factory( $config -> database);
Zend_DB_Table_Abstract::setDefaultAdapter( $db );
}
catch ( Zend_Db_Exception $e )
{
}
}
Then I have a simple model
class StandardAccessory extends Zend_DB_Table_Abstract
{
/**
* The default table name
*/
protected $_name = 'standard_accessory';
protected $_primary = 'model';
protected $_sequence = false;
}
And finally, inside my index controller, I just run the find method.
require_once APPLICATION_PATH . '/models/StandardAccessory.php';
$sa = new StandardAccessory( );
$stndacc = $sa->find( 'abc' );
All this takes ~0.5 seconds, which is way too long. Any suggestions?
Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
提示:
缓存表元数据。默认情况下,每次实例化表对象时,
Zend_Db_Table
都会尝试发现有关表的元数据。使用缓存可以减少执行此操作的次数。或者将其硬编码到 Table 类中(注意:数据库表不是模型)。使用
解释
分析MySQL的优化计划。它是否有效地使用了索引?使用
BENCHMARK()
来衡量查询的速度,不使用PHP。子查询必须返回单个列,因此请确保返回非索引列,以便查询必须接触数据,而不仅仅是返回索引条目。
请注意,当您进行第一个查询时,
Zend_Db_Adapter
会延迟加载其数据库连接。因此,如果连接到 MySQL 服务器时出现任何缓慢的情况,就会在实例化 Table 对象时(当它查询元数据时)发生。有什么原因这可能需要很长时间吗? DNS 查找,也许?Tips:
Cache the table metadata. By default,
Zend_Db_Table
tries to discover metadata about the table each time your table object is instantiated. Use a cache to reduce the number of times it has to do this. Or else hard-code it in your Table class (note: db tables are not models).Use
EXPLAIN
to analyze MySQL's optimization plan. Is it using an index effectively?Use
BENCHMARK()
to measure the speed of the query, not using PHP. The subquery must return a single column, so be sure to return a non-indexed column so the query has to touch the data instead of just returning an index entry.Note that
Zend_Db_Adapter
lazy-loads its db connection when you make the first query. So if there's any slowness in connecting to the MySQL server, it'll happen as you instantiate the Table object (when it queries metadata). Any reason this could take a long time? DNS lookups, perhaps?调试此问题的最简单方法是分析您的 sql 查询。您可以使用Firephp(firebug插件)参见http://framework.zend.com/manual/en/zend.db.profiler.html#zend.db.profiler.profilers.firebug
另一种加快速度的方法是缓存表的元数据。
请参阅: http:// Framework.zend.com/manual/en/zend.db.table.html#zend.db.table.metadata.caching
The easiest way to debug this, is to profile your sql queries. you can use Firephp (plugin for firebug) see http://framework.zend.com/manual/en/zend.db.profiler.html#zend.db.profiler.profilers.firebug
another way to speed up things a little is to cache the metadata of your tables.
see: http://framework.zend.com/manual/en/zend.db.table.html#zend.db.table.metadata.caching
除了上述建议之外,我做了一个非常不科学的测试,发现 PDO 适配器在我的应用程序中对我来说更快(我知道 mysqli 应该更快,但也许这是 ZF 抽象)。我在此处显示结果(显示的时间仅用于比较)
Along with the above suggestions I did a very unscientific test and found that the PDO adapter was faster for me in my application (I know mysqli is supposed to be faster but maybe it's the ZF abstraction). I show the results here (the times shown are only good for comparison)