问题描述
我正在使用 php、mysql 和 zend 框架创建一个网站.当我尝试运行任何 sql 查询时,页面生成会跳转到 0.5 秒左右.那太高了.如果我关闭 sql,页面生成为 0.001.我运行的查询量并没有真正影响页面生成时间(测试了 1-10 个查询).停留在 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.
我在引导程序中连接到 sql:
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;
}
最后,在我的索引控制器中,我只运行了 find 方法.
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' );
所有这一切都需要大约 0.5 秒,这太长了.有什么建议?
All this takes ~0.5 seconds, which is way too long. Any suggestions?
谢谢!
推荐答案
提示:
缓存表元数据.默认情况下,每次实例化表对象时,
Zend_Db_Table
都会尝试发现有关该表的元数据.使用缓存来减少它必须执行此操作的次数.或者在您的 Table 类中对其进行硬编码(注意:db 表不是模型).
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).
使用EXPLAIN
分析MySQL的优化方案.它是否有效地使用了索引?
Use EXPLAIN
to analyze MySQL's optimization plan. Is it using an index effectively?
mysql> EXPLAIN SELECT * FROM standard_accessory WHERE model = 'abc';
使用BENCHMARK()
来衡量查询的速度,不使用PHP.子查询必须返回单列,所以一定要返回一个非索引列,这样查询必须接触数据,而不是只返回一个索引条目.
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.
mysql> SELECT BENCHMARK(1000,
(SELECT nonindexed_column FROM standard_accessory WHERE model = 'abc'));
请注意,当您进行第一次查询时,Zend_Db_Adapter
会延迟加载其数据库连接.因此,如果连接到 MySQL 服务器时出现任何缓慢,当您实例化 Table 对象时(当它查询元数据时)就会发生这种情况.任何原因这可能需要很长时间?DNS 查找,也许?
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?
这篇关于Zend Framework 和 Mysql - 非常慢的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!