您好,欢迎来到三六零分类信息网!老站,搜索引擎当天收录,欢迎发信息

laravel学习记录之强制指定索引进行查询

2025/9/8 8:42:13发布24次查看
为什么需要强制索引?数据库没有使用我们设想的索引进行sql查询,导致查询特别慢。
mysql强制索引查询语句select * from user where age = 26 force index(age); // 强制索引
select * from user where age = 26 use index(age); // 优先按照这种索引查找
/** * 检测某个表中是否存在某个索引 * @param $table * @param $index * @return bool * @author zhaohao * @date 2019-08-26 17:42 */if(!function_exists('hasindex')) { function hasindex($table, $name) { $conn = illuminatesupportfacadesschema::getconnection(); $dbschemamanager = $conn->getdoctrineschemamanager(); $doctrinetable = $dbschemamanager->listtabledetails($table); return $doctrinetable->hasindex($name); }}
在laravel的代码里面需要这样写:在这里用when方法来判断此索引是否存在,日过不存在的话就不用这个索引,不然会报错,避免有人误删索引后,导致系统报错。
此处强制索引的语句是:
->from(db::raw('`erp_agents` force index (`test`)'))
例如:
$agents = agent::where($wheretype) ->when(hasindex('agent', 'test'),function ($q){ $q->from(db::raw('`erp_agents` force index (`test`)')); }) ->when(request('position',false),function ($q){ $q->wherein('position_id',request('position')); }) ->wherein('agents.status', $validstatus) ->where('worked_at', '<=', $end) ->where('is_suppose', 0) ->adddomination('m.statistics-human-view') ->leftjoin('positions', 'positions.id', '=', 'agents.position_id') ->get(['worked_days', 'worked_at']);
【相关推荐:最新的五个laravel视频教程】
以上就是laravel学习记录之强制指定索引进行查询的详细内容。
该用户其它信息

VIP推荐

免费发布信息,免费发布B2B信息网站平台 - 三六零分类信息网 沪ICP备09012988号-2
企业名录 Product