限制Magento非eav表的查询

时间:2016-05-13 11:21:14

标签: php mysql magento

我的数据库中有一个自定义表,大约有900 000行。我正在运行此查询:

$products = Mage::getModel("similar/flipkart")->getCollection()->addFieldToFilter('title', array('like' => '%'.$query.'%')) ->setCurPage(1)
                ->setPageSize(4);

此查询返回结果需要12秒以上。但是当我在mysql表上运行限制查询时,我立即获得结果。我如何加快我的流程。记录查询后,我得到了这个:

`SELECT main_table.* FROM flipkart_furn AS main_table WHERE (title LIKE '%chai%')` 

所以基本上限制没有附加到它。如何限制附加到它

2 个答案:

答案 0 :(得分:1)

我以这种方式运行直接SQL查询(虽然不是一个好的答案,但只是一种解决方法):

$resource = Mage::getSingleton('core/resource');

    /**
     * Retrieve the read connection
    */
    $readConnection = $resource->getConnection('core_read');

    /**
     * Retrieve our table name
    */
    $table = $resource->getTableName('similar/flipkart');

    /**
     * Set the product ID
    */
    //$productId = 44;

    $query = 'SELECT * FROM ' . $table . ' WHERE title like "%'.$query1.'%" LIMIT 4';

    /**
     * Execute the query and store the result in $sku
     */
    $products = $readConnection->fetchAll($query);
    //$sku = $readConnection->fetchOne($query);


    /* $products = Mage::getModel("similar/flipkart")->getCollection()->addFieldToFilter('title', array('like' => '%'.$query.'%')) ->setCurPage(1)
                ->setPageSize(4);
    Mage::log((string)$products->getSelect(),null,"mylogfile1.log",true);*/
    return $products; 

我直接从我的magento模型类中运行了这个。我立刻得到了结果

答案 1 :(得分:0)

简短的回答:你真的不能! 答案很长:你正在与Magento在后台处理一个巨大的EAV模型。根据数据库中的项目数量,它只需要时间来获取数据,很可能是Magento join一些表格会使这一点更长。

一种方法可能是使用like(模糊),而是使用=(锐利)。

$products = Mage::getModel("similar/flipkart")
                ->getCollection()
                ->addFieldToFilter('title', $query)
                ->setCurPage(1)
                ->setPageSize(4);

like搜索确实非常昂贵。

如果您不能使用=代替like,那么您可以提升服务器并优化数据库。这是superb article" Magento on Steroids"如何提高数据库性能。

我还建议使用Nexcess文章中描述的database maintenance

限制只是减少你得到的结果数量 - 通常这也会使查询更快!此外,直接在服务器上运行的查询(使用mysql workbench)总是比需要通过ORM处理的查询更快,而Magento使用自己的ORM。