我有几张桌子,比方说3:
tblitem (100,000+ records)
tblcate (10 records)
tblcondition (10 records)
现在我想从tblitem中一次只选择50个。这就是我想要的:
select *
from (select * from tblitem where enabled = true order by date desc limit 0, 50) t
inner join tblcate c on t.cateid = c.id
inner join tblcondition con on t.conid = con.id;
在Laravel我写道:
$result = DB::table('tblitem as t')->where('enabled', true)->orderBy('date', desc)->skip(0)->take(50)
->join('tblcate as c', 't.cateid', '=', 'c.id')
->join('tblcondition as con', 't.conid', '=', 'con.id');
但后来我从Laravel那里得到了这个sql:
select *
from tblitem t inner join tblcate c on t.cateid = c.id
inner join tblcondition con on t.conid = con.id
where t.enabled = true
order by t.date desc
limit 0, 50;
这使服务器加入第一个表中的所有记录,然后查询速度很慢。
如何让它首先执行第一个表条件,以便减少记录加入并使查询更快。
谢谢。