代码之家  ›  专栏  ›  技术社区  ›  Edwin Krause

开发环境和生产环境MySQL服务器的性能差异

  •  0
  • Edwin Krause  · 技术社区  · 6 年前

    我正在用Laravel5.6和Vuejs构建一个应用程序,从Laravel后端的API请求数据。在我的开发环境中,查询根本不需要时间,但是在生产环境中,API上的响应需要85.7秒,而在开发环境中则需要0.1秒。两个环境中的数据相同。

    使用控制器中的以下操作命令,我可以获得运行的确切查询,并可以逐个测试它们:

    DB::enableQueryLog();
    
        // then my eloquent stuff
        $query = Sportevent::whereHas('photos')->with('photos');
        if($request->input('year')){
            $year = $request->input('year');
            $query = $query->where('date','like', $year.'%');
        }
        if($request->input('country')){
            $country = $request->input('country');
            $query = $query->where('country',$country);
         }
        $sportevents = $query->orderBy('date',"DESC")->paginate(10);
    
    // then I display the queries:
    dd(DB::getQueryLog());
    

    以下是未选择年份或国家/地区而生成的查询,包括在两种环境中随时间变化的性能:

    // --------------------------------------------------------------
    // DEV: 0.0208 seconds | PROD: 73 seconds (had to use stopwatch)
    // --------------------------------------------------------------
    select count(*) as aggregate from `events` where exists 
       (select * from `photos` where `events`.`id` = `photos`.`eventID` 
       and `active` = 1)
    
    // ------------------------------------------
    // DEV: 0.025 seconds | PROD: 38.9721 seconds
    // ------------------------------------------
    select * from `events` where exists (select * from `photos` where
      `events`.`id` = `photos`.`eventID` and `active` = 1) 
       order by `date` desc limit 10 offset 0
    
    // ------------------------------------------
    // DEV: 0.0112 seconds | PROD: 0.0141 seconds
    // ------------------------------------------
    select * from `photos` where `active` = 1 and `photos`.`eventID` in 
        (11194, 11087, 10506, 10797, 9910, 10118, 10212, 9655, 10047, 10049)
    

    表事件包含大约6000个条目和少于50000个条目的照片。如果您在讨论表结构时需要进一步的详细信息,请在“注释”部分的“下投票”前通知我:—)

    在生产服务器上,有许多其他的应用程序在同一个MySQL安装中使用Laravel或Wordpress运行,它们都没有类似的问题。

    1 回复  |  直到 6 年前
        1
  •  0
  •   Edwin Krause    6 年前

    尽管前两个查询的输出量不大,但它们必须通过大多数数据,因此似乎是内存和/或CPU密集型查询。

    似乎我的开发环境比我的生产环境更有力量,因此我有着巨大的差异。

    最后的解决方案很简单:我在列中添加了一个索引类型的索引 photos.eventID 无论如何,这是个好主意。

    这个小小的改变使我的API在0.4秒内响应,而不是85.7秒。

    推荐文章