我在Laravel中写了一个查询:
$policy = DB::table('policies')->
join('customers','policies.customer_id','=','customers.id')->
join('cities','customers.city_id','=','cities.id')->
join('policy_motors','policies.id','=','policy_motors.policy_id')->
join('vehicle_makes','policy_motors.vehicle_make','=','vehicle_makes.id')->
join('vehicle_models','policy_motors.vehicle_model','=','vehicle_models.id')->
select('policies.policy_number','policies.insurance_premium','policies.commission',
'policies.effective_start_date',
'policies.effective_end_date','customers.name_en',
'customers.address1','customers.address2','cities.name_en','policy_motors.policy_type',
'vehicle_makes.name_en','vehicle_models.name_en')->
where('policies.policy_number','=','DB202017036583')->first();
这个查询在我的Mac上运行得很好。然而,当我的同事在他的Windows机器上运行相同的查询时,它花费了很长时间。所以他自己写了一个,那就是:
$policy = Policy::with('customer', 'motor', 'user')->
where('policy_number', 'RK202117017053')->first();
他的查询在他的Windows和我的Mac上运行得很好。
问题:
1、虽然我的查询只选择必需的列,但这要花很长时间。但是,他的查询执行得更快,因为该查询占用了联接表的所有列。为什么会这样?
2、在不同的机器上运行查询有什么区别,时间差应该那么大?