【问题标题】:Is my logic to check data in database efficient?我在数据库中检查数据的逻辑是否有效?
【发布时间】:2019-01-13 22:57:31
【问题描述】:

我使用 laravel 5.6

我有一个包含 50 万条记录的 json 文件。我想创建一个逻辑来检查每条记录的 id 是否已经存在于数据库中。如果它不存在,那么将有一个数据插入过程。如果它已经存在,将会有一个数据更新过程。在更新数据之前,它会检查json文件和数据库中的last_modified_date是否相同或不同。如果不一样就会更新

我已经提出了逻辑。我只是想确定我的逻辑是否有效

我的逻辑代码是这样的:

$path = storage_path('data.json');
$json = json_decode(file_get_contents($path), true);
foreach ($json['value'] as $value) {
    $last_modified_date = \Carbon\Carbon::parse($value['Last_Modified_Date']);
    $data = \DB::table('details')->where('id', '=', $value['Code'])->get();
    if ($data->isEmpty()) {
        \DB::table('details')->insert(
            [
                'id' => $value['Code'],
                'number' => $value['Number'],
                'last_modified_at' => $last_modified_date,
                ...
            ]
        );
    }
    else {
        \DB::table('details')
            ->where('id', '=', $value['Code'])
            ->where('last_modified_at', '<>', $last_modified_date)
            ->update([
                'id' => $value['Code'],
                'number' => $value['Number'],
                'last_modified_at' => $last_modified_date,
                ...
            ]);
    }
}

代码正在运行。但是这个过程似乎真的很漫长

您还有其他更好的解决方案吗?

更新

我找到另一个解决方案使用updateOrCreate

我尝试这样:

$path = storage_path('data.json');
$json = json_decode(file_get_contents($path), true);
foreach ($json['value'] as $value) {
    Details::updateOrCreate(
        [ 'id' => $value['Code'] ],
        [ 'number' => $value['Number'], 'last_modified_at' => $last_modified_date, ... ]
    );
}

你怎么看?

【问题讨论】:

  • @lagbox 是的,这是一样的。但我还有另一种情况,在更新之前先检查 last_modified_date。如果数据库和json之间的last_modified_data不同,那么它将更新数据。我想确保使用UpdateOrCreate 是正确的。
  • 有时最好改变你的逻辑以使你的代码更有效。
  • @Mohsen Safari 所以你的意思是我可以使用一个新案例updateOrCreate 吗?本案与前案略有不同。它将检查 json 文件和数据库中的 last_modified_date 是否相同或不同。如果它不同,那么它将更新。我想确定这个案例是否可以使用updateOrCreate。希望你给个肯定的答复
  • 不,你不能在updateOrCreate条件下使用&lt;&gt;
  • @Mohsen Safari 那么我的情况最好的解决方案是什么?

标签: json laravel laravel-5 eloquent laravel-5.6


【解决方案1】:

你不能在updateOrCreate中使用&lt;&gt;

我希望这段代码可以帮助你:

$path = storage_path('data.json');
$json = json_decode(file_get_contents($path), true);
foreach ($json['value'] as $value) {
    $detail = Details::firstOrCreate(
        [ 'id' => $value['Code'] ],
        [ 'number' => $value['Number'], 'last_modified_at' => $last_modified_date, ... ]
    );
    if($detail->last_modified_at != $last_modified_date) {
        $detail->update([
            'number' => $value['Number'],
            'last_modified_at' => $last_modified_date,
            ...
        ]);
    }
}

【讨论】:

  • 好的。我想问一下。为什么你更喜欢使用if($detail-&gt;last_modified_at != $last_modified_date) { 而不是-&gt;where('last_modified_at', '&lt;&gt;', $last_modified_date)
  • @SuccessMan 您的条件运行查询然后如果条件为真则更新记录,但在我的情况下不需要运行查询检查条件
  • 太棒了。非常感谢
  • 也许你能帮助我。看看这个:stackoverflow.com/questions/52147336/…
猜你喜欢
  • 2019-01-12
  • 1970-01-01
  • 2021-09-16
  • 2014-04-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-08-07
相关资源
最近更新 更多