dongxia2068 2016-12-30 13:09
浏览 251
已采纳

Laravel orWhere()/ MySQL或查询需要很长时间

I'm using Laravel 4.2 and my application is used for tracking inventory across multiple locations.

The database is set up with an inventory_items table, inventory_locations table and a pivot table between them inventory_items_inventory_location, which contains the quantity values whilst referencing both the inventory item and location the record belongs to.

My query is to find inventory items that have any location quantity value more than or equal to 0. In Laravel I'm using a subquery and orWhere like so:

InventoryItem::whereHas('inventoryLocations', function($q) {
  $q->where('reserved', '>=', 0)
     ->orWhere('available', '>=', 0) # slow
     ->orWhere('inbound', '>=', 0) # slow
     ->orWhere('total', '>=', 0); # slow
})->toSql();

Which gives the following SQL:

select * from `inventory_items`
where `inventory_items`.`deleted_at` is null
and (
  select count(*) from `inventory_locations`
  inner join `inventory_item_inventory_location`
  on `inventory_locations`.`id` = `inventory_item_inventory_location`.`inventory_location_id` 
  where `inventory_item_inventory_location`.`inventory_item_id` = `inventory_items`.`id`
  and `reserved` >= ?
  or `available` >= ? # slow
  or `inbound` >= ? # slow
  or `total` >= ? # slow
) >= 1

The problem is that with the or statements (marked in the code by #slow) the query time is up to 1s directly with Sequel Pro, more than 5s through my Laravel app (or through artisan tinker). Without these 'or' checks (i.e. just checking for one quantity type e.g. 'reserved') the query is <100ms on Sequel Pro and similar on the app/tinker.

I'm not sure why adding these extra 'or' checks adds so much time to the query. Any ideas how to make a more performant query?

  • 写回答

1条回答 默认 最新

  • dougan6402 2016-12-30 14:00
    关注

    See the resulting query and its WHERE conditions. You definitely miss some brackets there, as I guess what you need is

    where `inventory_item_inventory_location`.`inventory_item_id` = `inventory_items`.`id`
    and (
       `reserved` >= ?
       or `available` >= ? #
       or `inbound` >= ?
       or `total` >= ?
    )
    

    instead of

    where `inventory_item_inventory_location`.`inventory_item_id` = `inventory_items`.`id`
    and `reserved` >= ?
    or `available` >= ? # slow
    or `inbound` >= ? # slow
    or `total` >= ?
    

    It results in full table scan which is terribly slow for tables with high amount of rows.

    In order to fix that, replace

    InventoryItem::whereHas('inventoryLocations', function($q) {
      $q->where('reserved', '>=', 0)
       ->orWhere('available', '>=', 0) # slow
       ->orWhere('inbound', '>=', 0) # slow
       ->orWhere('total', '>=', 0); # slow
    })->toSql();
    

    with

    InventoryItem::whereHas('inventoryLocations', function($q) {
      $q->where(function($subquery) {
        $subquery->where('reserved', '>=', 0)
         ->orWhere('available', '>=', 0)
         ->orWhere('inbound', '>=', 0)
         ->orWhere('total', '>=', 0);
      });
    })->toSql();
    

    Check out MySQL's EXPLAIN command that lets you analyse how query will be executed and how many rows will be queried - http://dev.mysql.com/doc/refman/5.7/en/explain.html

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!
  • ¥15 drone 推送镜像时候 purge: true 推送完毕后没有删除对应的镜像,手动拷贝到服务器执行结果正确在样才能让指令自动执行成功删除对应镜像,如何解决?