简体   繁体   English

Laravel paginate()不限制结果。

[英]Laravel paginate() not limiting results.

I have a table with well over 10k rows. 我有一张桌子,上面有超过1万行。 I am need to show the results and was going to use paginate() to help with the load. 我需要显示结果,并打算使用paginate()帮助加载。 However, when calling the query all the results are being called and not just limiting by the paginate() . 但是,在调用查询时,所有结果都将被调用,而不仅限于paginate()

   $category = Inventory::join('inventory_categories', 'inventory.sku', '=', 'inventory_categories.sku')
        ->select('inventory_images.image', 'inventory.id' , 'inventory.sku', 'inventory.name', 'inventory.price', 'inventory_categories.category')
        ->leftJoin('inventory_images', 'inventory.sku', '=', 'inventory_images.sku')
        ->where('inventory_categories.category', 'LIKE', '%'.$cat.'%')
        ->where('inventory.active', '=', '0')
        ->where('inventory.stock_quantity', '>', 2)
        ->paginate(16);

When doing an Event::listen on the query I get this in return: note the lack of Limit. 在查询上执行Event :: listen时,我得到以下回报:请注意缺少限制。

array (size=4)
  0 => string 'select count(*) as aggregate from `inventory` inner join `inventory_categories` on `inventory`.`sku` = `inventory_categories`.`sku` left join `inventory_images` on `inventory`.`sku` = `inventory_images`.`sku` where `inventory_categories`.`category` LIKE ? and `inventory`.`active` = ? and `inventory`.`stock_quantity` > ?' (length=321)
  1 => 
    array (size=3)
      0 => string '%SOMETHING%' (length=11)
      1 => string '0' (length=1)
      2 => int 2
  2 => float 82325.11
  3 => string 'mysql' (length=5)

The table has 30,606 products that meet this query. 该表包含30606个满足该查询的产品。 So it's basically loading every row before showing the page which takes forever (I think the float in the array is the time it took). 因此,它基本上是在显示永远耗时的页面之前加载每一行(我认为数组中的float是花费的时间)。 I need to limit the results and show the paginate properly. 我需要限制结果并正确显示分页。 I tried using take() and that didn't work. 我尝试使用take() ,但是没有用。 Is this something new with Laravel 4.2.2? Laravel 4.2.2是否有新功能? Because I am pretty sure this use to work in the past with no problems. 因为我很确定,这种用法过去可以正常使用。 But I would have to check my older projects to be sure. 但是我必须检查一下我的旧项目以确保。

我可以通过向每个表的Mysql数据库添加一个复合索引来使其工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM