 |
Laravel model relationships ดึงข้อมูล 10000+ record error 500 |
|
 |
|
|
 |
 |
|
database อะไร
spec แค่ไหน
ทำ index ไว้มั้ย
จำเป็นต้องเอามทั้งหมด 10,000 มั้ย ถ้าจำเป็น แล้ว server ไม่ไหว ทำ micro service วิ่ง get มาเก็บใน temp table ได้หรือเปล่า
มันมีหลายสาเหตุครับ
|
 |
 |
 |
 |
Date :
2018-12-07 17:39:13 |
By :
mongkon.k |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
จงดูข้า
Province.php
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Province extends Model
{
/**
* The table associated with the model.
*
* @var string
*/
protected $table = 'provinces';
/**
* The attributes that are mass assignable.
*
* @var array
*/
protected $fillable = [
'name'
];
/**
* The attributes that should be mutated to dates.
*
* @var array
*/
protected $dates = ['created_at', 'updated_at'];
/**
* Get the districts for the province.
*/
public function districts() {
return $this->hasMany(District::class);
}
}
District.php
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class District extends Model
{
/**
* The table associated with the model.
*
* @var string
*/
protected $table = 'districts';
/**
* The attributes that are mass assignable.
*
* @var array
*/
protected $fillable = [
'name'
];
/**
* The attributes that should be mutated to dates.
*
* @var array
*/
protected $dates = ['created_at', 'updated_at'];
/**
* Get the province of the district.
*/
public function province() {
return $this->belongsTo(Province::class);
}
CreateProvincesTable
<?php
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class CreateProvincesTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('provinces', function (Blueprint $table) {
$table->increments('id');
$table->string('name');
$table->timestamps();
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::drop('provinces');
}
}
CreateDistrictsTable
<?php
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class CreateDistrictsTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('districts', function (Blueprint $table) {
$table->increments('id');
$table->string('name');
$table->timestamps();
});
Schema::table('districts', function (Blueprint $table) {
$table->integer('province_id')->unsigned()->after('name');
$table->foreign('province_id')->references('id')
->on('provinces')->onDelete('cascade');
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::table('districts', function (Blueprint $table) {
$table->dropForeign('districts_province_id_foreign');
});
Schema::drop('districts');
}
}
|
 |
 |
 |
 |
Date :
2018-12-11 15:10:39 |
By :
ห้ามตอบเกินวันละ 2 กระทู้ |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
น่าจะเป็นตอนที่มัน render แล้ว timeout หรือเปล่าครับ 10000 rows query ไม่นานนะ ลองใช้ pagination เข้ามาช่วยดู
|
 |
 |
 |
 |
Date :
2018-12-17 09:35:53 |
By :
DK |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
ลอง เอา 10000 คูณ ด้วยความยาวของของข้อมูลใน 1 เรคคอร์ดดูครับ
10000 = 10 k
ความยาว 1 เรคคอร์ด = 100 byte (แค่ id ชื่อ นามสกุล รหัสผ่าน option) ก็จะเท่ากับ 1 mb
+ข้อมูลใน โปรแกรม มีตัวแปรเยอะไหม ซ้ำซ้อนไหม
และ ถ้าเรียกใช้ ด้วย fetch_array() ก็จะ คูณ 2เท่า
สร้าง function ส่งพารามิเ้ตอร์ แบบไหน ส่งแบบ value หรือ pointer
มีอีกหลายอย่างที่ทำให้ข้อมูลเล็กๆ มันขยายออกไปได้อีก
พยามใช้ตัวแปรประเภท object ให้มากๆ เพราะจะเป็นการอ้างอิงแบบ pointer ช่วยลดหน่วยความจำจากการส่งผ่านข้อมูล
ปล. อีกอย่าง เปิด การแจ้ง error เอาไว้ อ่าน error message ให้มากๆ จะได้รู้ว่าจะแก้ไขอย่างไร
|
ประวัติการแก้ไข 2018-12-17 10:00:28
 |
 |
 |
 |
Date :
2018-12-17 09:56:29 |
By :
NewbiePHP |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
|
|