Zend Framework和Mysql-非常慢 [英] Zend Framework and Mysql - very slow

查看:95
本文介绍了Zend Framework和Mysql-非常慢的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用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 )
    {

    }
}

那我有一个简单的模型

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?

谢谢!

推荐答案

提示:

  • 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-非常慢的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

  • 查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆