[英]Performance improvement - Yii2 Eager loading
我們有一個名為 content 的表,表的大小超過 5Gb。 我們的 Yii2 后端應用程序有一個部分可以查看這些內容,並且幾乎沒有過濾器可用於過濾掉內容。 我需要過濾已分配或未分配給類別的內容。
關系
內容可以在許多類別中共享。 因此 Content 和 Category 表之間存在一對多的關系。
映射表是 content_cat_xref。
型號如下。
內容 model
/**
* @return \yii\db\ActiveQuery
*/
public function getContentCatXrefs()
{
return $this->hasMany(ContentCatXref::className(), ['content_id' => 'id']);
}
Content_cat_xref model
/**
* @return \yii\db\ActiveQuery
*/
public function getCategory()
{
return $this->hasOne(Category::className(), ['id' => 'category_id']);
}
/**
* @return \yii\db\ActiveQuery
*/
public function getContent()
{
return $this->hasOne(Content::className(), ['id' => 'content_id']);
}
Controller
public function actionIndex()
{
$searchModel = new \common\models\ContentSearch();
$dataProvider = $searchModel->searchMedia(Yii::$app->request->queryParams);
return $this->render('index', [
'searchModel' => $searchModel,
'dataProvider' => $dataProvider
]);
}
內容搜索 Model
public function searchMedia($params)
{
$query = Content::find()->where(['file_type_id'=>[1,2,3,4,5,6,8,11]]);
$dataProvider = new ActiveDataProvider([
'query' => $query,
]);
$this->load($params);
if (!$this->validate()) {
return $dataProvider;
}
//Conditionally add filters
if(isset($this->is_category_available) && $this->is_category_available === 'yes'){
$query = $query->joinWith(['contentCatXrefs' => function($queryw){
$queryw->andWhere(['is not', 'content_cat_xref.category_id', new \yii\db\Expression('null')]);
}]);
}else if(isset($this->is_category_available) && $this->is_category_available === 'no'){
$query = $query->joinWith(['contentCatXrefs' => function($queryw){
$queryw->andWhere(['is', 'content_cat_xref.category_id', new \yii\db\Expression('null')]);
}]);
}
if(!is_null($this->file_name) && !empty($this->file_name)) {
$query->andWhere("MATCH (file_name) AGAINST (\"".$this->file_name."\" IN NATURAL LANGUAGE MODE)");
}
if(!is_null($this->file_path) && !empty($this->file_path)) {
$query->andWhere("MATCH (file_path) AGAINST (\"".$this->file_path."\" IN NATURAL LANGUAGE MODE)");
}
$query->andFilterWhere(['type_code'=>$this->type_code]);
$query->andFilterWhere(['file_type_id'=>$this->file_type_id]);
$query->andFilterWhere(['content.active_status'=>$this->active_status]);
$query->andFilterWhere(['content.content_status'=>$this->content_status]);
if(is_null($this->file_name)) {
$query->orderBy("id desc");
}
return $dataProvider;
}
我有幾個問題
連接兩個表(content 和 content_cat_xref)以檢索數據的正確有效方法應該是什么。 我需要過濾已分配或未分配給類別的內容。 所以,我為此使用了這個過濾器。
if(isset($this->is_category_available) && $this->is_category_available === 'yes'){ $query = $query->joinWith(['contentCatXrefs' => function($queryw){ $queryw->andWhere(['is not', 'content_cat_xref.category_id', new \yii\db\Expression('null')]); }]); }else if(isset($this->is_category_available) && $this->is_category_available === 'no'){ $query = $query->joinWith(['contentCatXrefs' => function($queryw){ $queryw->andWhere(['is', 'content_cat_xref.category_id', new \yii\db\Expression('null')]); }]); }
我在這里使用急切加載來提高性能。 但是在加入這兩個表時它會給出重復的記錄。 在我使用 distinct 之后,它會降低性能。 執行此 Yii2 的正確方法應該是什么?
在內容搜索 model 中構造查詢時,有沒有辦法將參數傳遞給關系函數?
如果我們按 id 降序排列內容,再次降低性能。
如果有人可以提供這方面的指導,我們將不勝感激。
if($this->is_category_available == 'yes') { return $this->hasMany(ContentCatXref::className(), ['content_id' => 'id']) ->andOnCondition(['a_type' => 1]); } else { return $this->hasMany(ContentCatXref::className(), ['content_id' => 'id']) ->andOnCondition(['a_type' => 0]); }
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.