gpt4 book ai didi

mysql - 10 月 cms 查询加载时间较长

转载 作者:行者123 更新时间:2023-11-29 10:26:16 25 4
gpt4 key购买 nike

我正在尝试优化这个需要很长时间才能加载的嵌套查询。

基本思想是获取属于一个部门的所有系列,中间是属于多个部门、多个系列的产品。表结构

部门*<-->*产品*<->*系​​列

在模型中,关系定义如下部门模型

public $belongsToMany  = [
'products' => [
'\depcore\parts\Models\Product',
'table' => 'depcore_parts_products_departments',
],
// 'series' => [
// '\depcore\parts\Models\Series',
// 'table' => 'depcore_parts_products_series',
// ]
];

系列型号

public $hasMany = [
'products' => [
'\depcore\parts\Models\Product',
'table' => 'depcore_parts_products_departments',
]
];

产品型号

public $belongsToMany = [
'series' => [
'depcore\parts\Models\Series',
'table' => 'depcore_parts_products_series',
'order' => 'name',
],
'departments' => [
'depcore\parts\Models\Department',
'table' => 'depcore_parts_products_departments',
// 'order' => 'name'
]
];

在部门模型中,我创建了一种方法来检索属于该部门的所有系列,这些系列在分析后会导致一些严重的性能问题

public function series (){

$seriesArray = array( );
$products = $this->products()->remember(100)->get();
foreach ($products as $product) {
$productSeries = $product->series()->remember(100)->get();
foreach ($productSeries as $series) {
if (!isset($seriesArray[$series->id]) and $series->published )
$seriesArray[$series->id] = $series;
}
}

return \Illuminate\Database\Eloquent\Collection::make($seriesArray);

}

我猜想这可以通过原始 SQL 或在事件记录中更好的实现来完成,但我坚持两者。

我稍后添加了 remember() 方法,但没有结果。目前网页加载时间约为20秒。立即删除此代码。

如有任何建议,我们将不胜感激。

从表结构来看,我猜这将是运行适当的 SQL 命令以获得所需的结果

SELECT DISTINCT series_id FROM depcore_parts_products_series WHERE product_id IN (SELECT DISTINCT product_id FROM depcore_parts_products_departments WHERE department_id = 3);

此查询仅在中间表上运行,并获取series_id的正确结果(在phpmyadmin中)(以department_id = 3为例)

使用 Hardik Satasiya 代码,我必须更改几行,否则 View 将不会显示任何系列,而只是空行。

public function series (){

$sql = 'SELECT DISTINCT series_id FROM depcore_parts_products_series WHERE product_id IN (SELECT DISTINCT product_id FROM depcore_parts_products_departments WHERE department_id = :dep_id)';

$data = ['dep_id' => $this->id];
$query = \DB::select($sql, $data);
$data = $query;

$ids = array();
// I had to rewrite this pare and make it more inelegant still
// but the refresh method appeared to made it step out the execution cycle
foreach ($data as $key => $value) {
$ids[] = $value->series_id;
}

// in $data we are passing only id information
// so this records have only id, not db all attributes
// what ever you pass in $data will become model attributes if its in list
$collection = \depcore\parts\Models\Series::hydrate($ids);
return Series::published()->whereIn('id',$ids)->get();

}

block 部分

<div class="element-grid">
<h4 {% if hideChildren|length and departmentModel.id not in filters.departments %} class='inactive' {% endif %} ><a href="{{ url('/')}}/parts?Filter[departments][]={{ departmentModel.id }}">{{ departmentModel.name }}</a></h4>
{% if not hideChildren|length %}
<div class="list">
<div class="block left-block">
{% if departmentModel.getChildren|length %}
<ul class='departments'>
{% for child in departmentModel.getChildren %}
{% if child.published %}
<li><strong><a href="{{ url('/')}}/parts?Filter[departments][]={{ departmentModel.id }}&Filter[departments][]={{ child.id }}">{{ child.name }}</a></li></strong>
{% endif %}

{% endfor %}
</ul>
{% endif %}
<ul class="series">
{% for series in departmentModel.departmentSeries.series|slice(0,10) %}
<li><a href="{{ url('/')}}/parts?Filter[departments][]={{ departmentModel.id }}&{{ departmentModel.departmentSeries.childrenString }}&Filter[series][]={{ series.id }}">{{ series.name }}</a></li>
{% endfor %}
</ul>
</div>
<div class="block right-block">
<ul class="series series-right">
{% for series in departmentModel.departmentSeries.series|slice(10,length) %}
<li><a href="{{ url('/')}}/parts?Filter[departments][]={{ departmentModel.id }}&{{ departmentModel.departmentSeries.childrenString }}&Filter[series][]={{ series.id }}">{{ series.name }}</a></li>
{% endfor %}
</ul>
</div>
</div>
<img src="{{ departmentModel.image.file_name | media }}" alt="">
{% endif %}
</div>

departmentSeries 范围。

public function scopeDepartmentSeries( $query ){
$children = $query->getModel()->getChildren();
// dd($children);
if ( count( $children ) > 0 ) {
$seriesArray = array ( );

foreach ($children as $child) {
$childrenIds[] = 'Filter[departments][]='.$child->id;

foreach ($child->series ( ) as $series) {

if (!in_array($series->id,$seriesArray)) $seriesArray[] = $series->id;

if (!array_key_exists($series->id,$seriesArray)) $seriesArray[$series->id] = $series->name;

}
} // endforeach children as child
$childrenString = implode( '&', $childrenIds );
return ["series" => Series::whereIn ( 'id',$seriesArray )->get ( ),
"childrenString" => $childrenString];
}
return ["series" => $query->getModel()->series( )];
}

最佳答案

您可以利用原始查询并将获取的 ID 转换为您可以编写此代码的模型。

$sql = 'SELECT DISTINCT series_id FROM depcore_parts_products_series WHERE product_id IN (SELECT DISTINCT product_id FROM depcore_parts_products_departments WHERE department_id = :dep_id');

$data = ['dep_id' => 2];
$query = \DB::select($sql, $data);
$data = $query;

foreach ($data as $model) {
$ids[] = $model->series_id;
}

$returnData = Series::whereIn('id',$ids)->get();
// dd($returnData);

return $returnData;

如果您发现任何困难,请评论。

更新

my department model

use \October\Rain\Database\Traits\SimpleTree;

public $belongsTo = [
'parent' => ['HardikSatasiya\StackDemo\Models\Departments', 'key' => 'parent_id'],
];

public $hasMany = [
'children' => ['HardikSatasiya\StackDemo\Models\Departments', 'key' => 'parent_id'],
];

public function series() {

$sql = 'SELECT DISTINCT series_id FROM hardiksatasiya_stackdemo_product_series WHERE product_id IN (SELECT DISTINCT product_id FROM hardiksatasiya_stackdemo_department_product WHERE department_id = :dep_id)';

$data = ['dep_id' => $this->id];
$query = \DB::select($sql, $data);
$data = $query;
foreach ($data as $model) {
$ids[] = $model->series_id;
}

$returnData = Series::whereIn('id',$ids)->get();
// dd($returnData);

return $returnData;
}

public function scopeDepartmentSeries( $query ) {
$children = $query->getModel()->getChildren();
//dd($children);
if ( count( $children ) > 0 ) {
$seriesArray = array ( );

foreach ($children as $child) {
$childrenIds[] = 'Filter[departments][]='.$child->id;

foreach ($child->series ( ) as $series) {

if (!in_array($series->id,$seriesArray)) $seriesArray[] = $series->id;

// if (!array_key_exists($series->id,$seriesArray)) $seriesArray[$series->id] = $series->name;

}
} // endforeach children as child
$childrenString = implode( '&', $childrenIds );
return ["series" => Series::whereIn ( 'id',$seriesArray )->get ( ),
"childrenString" => $childrenString];
}
return ["series" => $query->getModel()->series( )];
}

and page code section

function onInit() {
$departmentModel = \HardikSatasiya\StackDemo\Models\Departments::find(1);
//dd($departmentModel->getChildren());
$this['departmentModel'] = $departmentModel;
}

and I am using html/partial you provided. and it seems its working here

关于mysql - 10 月 cms 查询加载时间较长,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48253576/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com