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

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

报告相同问题?

悬赏问题

  • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线
  • ¥15 谁有desed数据集呀
  • ¥20 手写数字识别运行c仿真时,程序报错错误代码sim211-100
  • ¥15 关于#hadoop#的问题
  • ¥15 (标签-Python|关键词-socket)
  • ¥15 keil里为什么main.c定义的函数在it.c调用不了
  • ¥50 切换TabTip键盘的输入法
  • ¥15 可否在不同线程中调用封装数据库操作的类
  • ¥15 微带串馈天线阵列每个阵元宽度计算
  • ¥15 keil的map文件中Image component sizes各项意思