0

我使用此代码对包含搜索字符串的数组进行了自定义目录搜索:

$collection =    Mage::getModel('catalog/product')->getCollection()
            ->addAttributeToSelect('*')
        ->addAttributeToFilter('visibility', $visibility);

        foreach($searchNames as $searchName){
            $collection->addAttributeToFilter(array(
                    array('attribute'=> 'name','like' => '%'.$searchName.'%'),
                    array('attribute'=> 'search_field','like' => '%'.$searchName.'%'),
                    array('attribute'=> 'sku','like' => '%'.$searchName.'%')));
        }

当我得到 ~6k 结果时,搜索大约需要 5 秒。有没有办法加快这个速度?也许有一些我还没有找到的性能调整。

我已经为表 catalog_product_flat 中的这些字段设置了索引,但没有任何更改。

4

1 回答 1

0

Indexes will not work in your query for name, search_field and sku attributes! Because your LIKE value begins with a wildcard (%), and indexes can only work if it starts with a constant. Your query will speed up if you remove that leading % from LIKE values.

I don't understand why you are using foreach?

于 2013-02-22T21:11:35.450 回答