我正在尝试使用搜索页面进行记录。以下是我的查询生成器,我认为我的查询性能有问题。我正在考虑将其切换为Eloquent并能够使用toArray()
函数进行转换。
$records = DB::table('customers')
->join('orders', function($join) use ($keyword)
{
$join->on('customers.id', '=', 'orders.cust_id')
//customers
->where('name', 'LIKE', '%'.$keyword.'%')
->orWhere('address_street', 'LIKE', '%'.$keyword.'%')
->orWhere('address_street2', 'LIKE', '%'.$keyword.'%')
->orWhere('address_city', 'LIKE', '%'.$keyword.'%')
->orWhere('address_postcode', 'LIKE', '%'.$keyword.'%')
->orWhere('contact_mobile', 'LIKE', '%'.$keyword.'%')
->orWhere('contact_email', 'LIKE', '%'.$keyword.'%')
->orWhere('remark', 'LIKE', '%'.$keyword.'%')
//orders
->orWhere('order_no', 'LIKE', '%'.$keyword.'%')
->orWhere('delivery_man', 'LIKE', '%'.$keyword.'%')
})
->take(10)
->get();
我还注意到查询花了很长时间比较我从查询客户端写了sql语句。这个查询的结果是4s,sql查询只花了0.3秒。下面是sql语句。
SELECT * FROM `customers`
INNER JOIN `orders` on `customers`.`id` = `orders`.`cust_id`
WHERE `name` LIKE '%keyword%' or `address_street` LIKE '%keyword%'
or `address_street2` LIKE '%keyword%' or `address_city` LIKE '%keyword%'
or `address_postcode` LIKE '%keyword%' or `contact_mobile` LIKE '%keyword%'
or `contact_office` LIKE '%keyword%' or `contact_email` LIKE '%keyword%'
or `remark` LIKE '%keyword%' or `order_no` LIKE '%keyword%'
or `delivery_man` LIKE '%keyword%'
LIMIT 10