Skip to content
This repository has been archived by the owner on Sep 10, 2021. It is now read-only.

Commit

Permalink
ENH: Improved search speed
Browse files Browse the repository at this point in the history
  • Loading branch information
Julien Jomier committed Mar 11, 2011
1 parent 1b1167c commit fff8409
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 4 deletions.
5 changes: 5 additions & 0 deletions application/controllers/SearchController.php
Expand Up @@ -32,10 +32,15 @@ public function indexAction()
/** search live Action */
public function liveAction()
{
// This is necessary in order to avoid session lock and being able to run two
// ajax requests simultaneously
session_write_close();

$search = $this->getRequest()->getParam('term');

// Search for the items
$ItemsDao = $this->ItemKeyword->getItemsFromSearch($search,$this->userSession->Dao);

// Search for the folders
$FoldersDao = $this->Folder->getFoldersFromSearch($search,$this->userSession->Dao);

Expand Down
29 changes: 25 additions & 4 deletions application/models/pdo/ItemKeywordModel.php
Expand Up @@ -25,13 +25,35 @@ function getItemsFromSearch($searchterm,$userDao)
}
else if(!$userDao instanceof UserDao)
{
throw new Zend_Exception("Should be an user.");
throw new Zend_Exception("Should be a user.");
}
else
{
$userId = $userDao->getUserId();
}

// Apparently it's slow to do a like in a subquery so we run it first
$sql = $this->select()->from(array('itemkeyword'),array('keyword_id'))
->where('value LIKE ?','%'.$searchterm.'%');
$ids = '(';
$rowset = $this->fetchAll($sql);
$return = array();
foreach($rowset as $row)
{
if($ids != '(')
{
$ids .= ',';
}
$ids .= $row->keyword_id;
}
$ids .= ')';

// If we don't have any data we return
if(count($rowset) == 0)
{
return $return;
}

$subqueryUser= $this->select()
->setIntegrityCheck(false)
->from(array('p' => 'itempolicyuser'),
Expand All @@ -56,16 +78,15 @@ function getItemsFromSearch($searchterm,$userDao)

$sql = $this->select()->from(array('i' => 'item'),array('item_id','name','count(*)'))
->join(array('i2k' => 'item2keyword'),'i.item_id=i2k.item_id')
->join(array('k' => 'itemkeyword'),'k.keyword_id=i2k.keyword_id')
->where('k.value LIKE ?','%'.$searchterm.'%')
->where('i2k.keyword_id IN '.$ids)
->where('( i.item_id IN ('.$subqueryUser.') OR
i.item_id IN ('.$subqueryGroup.'))' )
->group('i.name')
->setIntegrityCheck(false)
->limit(14);


$rowset = $this->fetchAll($sql);
$return = array();
foreach($rowset as $row)
{
$tmpDao= new ItemDao();
Expand Down

0 comments on commit fff8409

Please sign in to comment.