我的查询:
$first = DB::table('news')
->selectRaw('"news" as tableName, id, title, description, imgPath')
->where(function($query) use ($q) {
$query->whereRaw("MATCH(title,description) AGAINST(? IN BOOLEAN MODE)", array($q));
});
$results = DB::table('productions')
->selectRaw('"productions" as tableName, id, title, description, imgPath')
->where(function($query) use ($q) {
$query->whereRaw("MATCH(title,description) AGAINST(? IN BOOLEAN MODE)", array($q));
})
->unionAll($first)
->get();
如你所见,其中有一个where()
,也有一个whereRaw()
。对吗?
结果很好,我的意思是它完全符合预期。我只是担心演出。我想我只需要一个where()
就可以了。然而,目前它工作得很好,但我担心如果数据集很大,那么它可能会很慢。
不管怎样,我的代码好吗?
不需要在where()
闭包中添加单个where。你也可以不使用
$first = DB::table('news')
->selectRaw('"news" as tableName, id, title, description, imgPath')
->whereRaw("MATCH(title,description) AGAINST(? IN BOOLEAN MODE)", array($q));
$results = DB::table('productions')
->selectRaw('"productions" as tableName, id, title, description, imgPath')
->whereRaw("MATCH(title,description) AGAINST(? IN BOOLEAN MODE)", array($q));
->unionAll($first)
->get();
但是如果你真的关心数据集变大时的性能,那么我建议你使用paginate()
函数而不是get()
没有什么我要说的是特别错误的功能方面,但有一些东西你可以清理,例如,你正在做一个selectRaw只是为了你可以别名表,但你从来没有使用别名。
$first = DB::table('news')
->select(['id', 'title', 'description', 'imgPath'])
->whereRaw("MATCH(title,description) AGAINST(? IN BOOLEAN MODE)");
$results = DB::table('productions')
->select(['id', 'title', 'description', 'imgPath'])
->whereRaw("MATCH(title,description) AGAINST(? IN BOOLEAN MODE)")
->unionAll($first)
->get();
试试上面的,如果这不起作用,试试你的代码,但是用select语句替换你的selectRaw语句。