pbootcms网站模板|日韩1区2区|织梦模板||网站源码|日韩1区2区|jquery建站特效-html5模板网

    1. <i id='o2kKU'><tr id='o2kKU'><dt id='o2kKU'><q id='o2kKU'><span id='o2kKU'><b id='o2kKU'><form id='o2kKU'><ins id='o2kKU'></ins><ul id='o2kKU'></ul><sub id='o2kKU'></sub></form><legend id='o2kKU'></legend><bdo id='o2kKU'><pre id='o2kKU'><center id='o2kKU'></center></pre></bdo></b><th id='o2kKU'></th></span></q></dt></tr></i><div class="g8eyiga" id='o2kKU'><tfoot id='o2kKU'></tfoot><dl id='o2kKU'><fieldset id='o2kKU'></fieldset></dl></div>
      <tfoot id='o2kKU'></tfoot>
        <bdo id='o2kKU'></bdo><ul id='o2kKU'></ul>
    2. <legend id='o2kKU'><style id='o2kKU'><dir id='o2kKU'><q id='o2kKU'></q></dir></style></legend>

      <small id='o2kKU'></small><noframes id='o2kKU'>

        在 Eloquent 中計算兩條記錄之間的值差異

        Calculating value differences between two records in Eloquent(在 Eloquent 中計算兩條記錄之間的值差異)

          1. <legend id='VBNq5'><style id='VBNq5'><dir id='VBNq5'><q id='VBNq5'></q></dir></style></legend>
              <bdo id='VBNq5'></bdo><ul id='VBNq5'></ul>

                <i id='VBNq5'><tr id='VBNq5'><dt id='VBNq5'><q id='VBNq5'><span id='VBNq5'><b id='VBNq5'><form id='VBNq5'><ins id='VBNq5'></ins><ul id='VBNq5'></ul><sub id='VBNq5'></sub></form><legend id='VBNq5'></legend><bdo id='VBNq5'><pre id='VBNq5'><center id='VBNq5'></center></pre></bdo></b><th id='VBNq5'></th></span></q></dt></tr></i><div class="c2ooyuy" id='VBNq5'><tfoot id='VBNq5'></tfoot><dl id='VBNq5'><fieldset id='VBNq5'></fieldset></dl></div>

                <small id='VBNq5'></small><noframes id='VBNq5'>

                <tfoot id='VBNq5'></tfoot>
                  <tbody id='VBNq5'></tbody>
                  本文介紹了在 Eloquent 中計算兩條記錄之間的值差異的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)吧!

                  問題描述

                  我想用 eloquent 計算兩條記錄之間的差異.例如,我有下表:

                  I would like to calculate the difference between two records in eloquent. For example, I have following table:

                  ----------------------------------
                  | Id | value | type              |
                  ----------------------------------
                  | 1  | 100   | FOO               |
                  | 2  | 500   | FOO               |
                  | 3  | 800   | FOO               |
                  | 4  | 200   | BAR               |
                  | 5  | 600   | BAR               |
                  | 6  | 1000  | FOO               |
                  ----------------------------------
                  

                  假設(shè)模型名稱是 FooBar,所以當(dāng)我過濾表格時,例如,使用 FooBar::where('type', 'FOO')->get(); 我將得到以下結(jié)果:

                  Let's say the model name is FooBar, so when I filter the table, for example, using FooBar::where('type', 'FOO')->get(); I will get following result:

                  ----------------------------------
                  | Id | value | type  | diff      |
                  ----------------------------------
                  | 1  | 100   | FOO   | 0         |
                  | 2  | 500   | FOO   | 400       | (500 - 100)
                  | 3  | 800   | FOO   | 300       | (800 - 500)
                  | 6  | 1000  | FOO   | 200       | (1000 - 800)
                  ----------------------------------
                  

                  現(xiàn)在,也許我可以使用原始查詢更輕松地實(shí)現(xiàn)這一點(diǎn),例如聲明變量來存儲以前的記錄(例如:SET @id := 0 并在 SELECT 語句中設(shè)置它).但在這種情況下,如果可能,我更喜歡使用 Eloquent.

                  Now, probably I could achieve this more easily with raw query, like declaring variable to store previous record (e.g: SET @id := 0 and set it in SELECT statement). But in this case I prefer to use Eloquent if possible.

                  我目前的解決方法是循環(huán)結(jié)果集并手動計算,我擔(dān)心這會影響性能.

                  My current workaround is loop the result set and calculate manually which I'm afraid it will affect the performance.

                  有什么想法嗎?

                  推薦答案

                  我不介意 eloquent 對性能的小影響,但會循環(huán)通過計算差異的結(jié)果集.. 我的意思是,如果我有數(shù)千條記錄,一個一個循環(huán)是粗略的想法

                  I don't mind small performance impact from eloquent, but looping thru the result set to calculate difference.. I mean, cmon if I had thousands records, looping one by one is rough idea

                  那么我有一個驚喜給你 - 這是一個小的性能測試:

                  Then I have a surprise for you - Here is a small performance test:

                  class Seq extends Eloquent {
                      protected $table = 'helper.seq';
                      protected $primaryKey = 'i';
                  }
                  
                  Route::get('/loop', function () {
                      $limit = 10000;
                  
                      $st = microtime(true);
                      $data = Seq::orderBy('i')->take($limit)->get();
                      var_dump(microtime(true) - $st);
                  
                      $st = microtime(true);
                      foreach ($data as $row) {
                          $row->i;
                      }
                      var_dump(microtime(true) - $st);
                  
                      $pdo = DB::getPdo();
                      $st = microtime(true);
                      $data2 = $pdo
                          ->query("select * from helper.seq order by i limit $limit")
                          ->fetchAll(PDO::FETCH_OBJ);
                      var_dump(microtime(true) - $st);
                  
                      $st = microtime(true);
                      foreach ($data2 as $k => $row) {
                          if ($k == 0) {
                              $row->diff = 0;
                          } else {
                              $row->diff = $row->i - $data2[$k-1]->i;
                          }
                      }
                      var_dump(microtime(true) - $st);
                  });
                  

                  helper.seq 是一張只有一個 int 列和 100 萬行的表.

                  helper.seq is a table with only one int column and 1M rows.

                  結(jié)果是:

                  0.779045s <- Fetch from DB with Eloquent
                  
                  1.022058s <- Read Eloquent data (Only one column and do nothing with it)
                  
                  0.020002s <- Fetch from DB with PDO
                  
                  0.009999s <- Calculate all diffs in a loop
                  

                  所以eloquent 對性能的影響很小"是:

                  So the "small performance impact from eloquent" is:

                  • 從數(shù)據(jù)庫中獲取數(shù)據(jù)時,比使用普通 PDO 和 stdClass 慢近 20 倍.
                  • 在循環(huán)中讀取屬性/屬性時,至少比 stdClass 慢 100 倍.
                  • Almost 20 times slower than using plain PDO and stdClass when fetching data from database.
                  • At least 100 times slower than stdClass when reading properties/attributes in a loop.

                  因此,如果您想提高性能,請在處理大量數(shù)據(jù)時切換到普通 PDO,或者至少使用默認(rèn)的 Builder.

                  So if you want to improve the peroformance, switch to plain PDO when dealing with big amounts of data or at least use the default Builder.

                  現(xiàn)在你仍然可以嘗試在 MySQL 中完成這項(xiàng)工作,但要求使用 Eloquent 是沒有意義的.

                  Now you can still try to do the job in MySQL, but the requirement to use Eloquent wouldn't make sence.

                  然而,您可以嘗試混合版本 - 使用 Eloquent 構(gòu)建查詢,但使用 getQuery() 將其轉(zhuǎn)換為 DatabaseQueryBuilder.

                  However you can try a mixed version - Use Eloquent to build the query, but convert it to DatabaseQueryBuilder with getQuery().

                  $fooBars = FooBar::where('type', 'FOO')->orderBy('id')
                      ->getQuery()
                      ->select(['*', DB::raw('coalesce(`value` - @last, 0)'), DB::raw('@last := `value`')])
                      ->get();
                  

                  但我總是避免在應(yīng)用程序代碼中以這種方式使用會話變量,因?yàn)槲乙呀?jīng)看到許多此類解決方案在版本升級后返回錯誤/意外的結(jié)果.

                  But I would always avoid using session variables this way in application code, because i've seen many of such solutions returning wrong/unexpected results after a version upgrade.

                  還是不相信?以下是一些其他測試:

                  Still not convinced? Here are some other tests:

                  在轉(zhuǎn)換為 DatabaseQueryBuilder 的 Eloquent 查詢中使用會話變量:

                  Using session variables in an Eloquent query converted to DatabaseQueryBuilder:

                  $st = microtime(true);
                  $data = Seq::getQuery()
                      ->select(['*', DB::raw('coalesce(i - @last, 0)'), DB::raw('@last := i')])
                      ->orderBy('i')->take($limit)->get();
                  var_dump(microtime(true) - $st);
                  
                  // runtime: 0.045002s
                  

                  使用轉(zhuǎn)換后的 Eloquent 查詢的 PHP 解決方案:

                  PHP solution using converted Eloquent query:

                  $st = microtime(true);
                  $data2 = Seq::getQuery()->orderBy('i')->take($limit)->get();
                  foreach ($data2 as $k => $row) {
                      if ($k == 0) {
                          $row->diff = 0;
                      } else {
                          $row->diff = $row->i - $data2[$k-1]->i;
                      }
                  }
                  var_dump(microtime(true) - $st);
                  
                  // runtime: 0.039002
                  

                  帶有普通 PDO 和 stdClass

                  PHP solution with plain PDO and stdClass

                  $st = microtime(true);
                  $data3 = $pdo
                      ->query("select * from helper.seq s1 order by i limit $limit")
                      ->fetchAll(PDO::FETCH_OBJ);
                  foreach ($data3 as $k => $row) {
                      if ($k == 0) {
                          $row->diff = 0;
                      } else {
                          $row->diff = $row->i - $data3[$k-1]->i;
                      }
                  }
                  var_dump(microtime(true) - $st);
                  
                  // runtime: 0.035001s
                  

                  帶有普通 PDO 和關(guān)聯(lián)數(shù)組的 PHP 解決方案:

                  PHP solution with plain PDO and assotiative arrays:

                  $st = microtime(true);
                  $data4 = $pdo
                      ->query("select * from helper.seq s1 order by i limit $limit")
                      ->fetchAll(PDO::FETCH_ASSOC);
                  foreach ($data4 as $k => $row) {
                      if ($k == 0) {
                          $row['diff'] = 0;
                      } else {
                          $row['diff'] = $row['i'] - $data4[$k-1]['i'];
                      }
                  }
                  var_dump(microtime(true) - $st);
                  
                  // runtime: 0.027001s
                  

                  您首選的解決方案是最慢且最不可靠的.所以你的問題的答案對你的問題來說是一個糟糕的解決方案.

                  Your prefered solution is the slowest and the least reliable. So the answer to your question is a bad solution for your problem.

                  這篇關(guān)于在 Eloquent 中計算兩條記錄之間的值差異的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網(wǎng)!

                  【網(wǎng)站聲明】本站部分內(nèi)容來源于互聯(lián)網(wǎng),旨在幫助大家更快的解決問題,如果有圖片或者內(nèi)容侵犯了您的權(quán)益,請聯(lián)系我們刪除處理,感謝您的支持!

                  相關(guān)文檔推薦

                  Deadlock exception code for PHP, MySQL PDOException?(PHP、MySQL PDOException 的死鎖異常代碼?)
                  PHP PDO MySQL scrollable cursor doesn#39;t work(PHP PDO MySQL 可滾動游標(biāo)不起作用)
                  PHP PDO ODBC connection(PHP PDO ODBC 連接)
                  Using PDO::FETCH_CLASS with Magic Methods(使用 PDO::FETCH_CLASS 和魔術(shù)方法)
                  php pdo get only one value from mysql; value that equals to variable(php pdo 只從 mysql 獲取一個值;等于變量的值)
                  MSSQL PDO could not find driver(MSSQL PDO 找不到驅(qū)動程序)

                    • <tfoot id='eFKW0'></tfoot>
                        <tbody id='eFKW0'></tbody>

                    • <small id='eFKW0'></small><noframes id='eFKW0'>

                      <legend id='eFKW0'><style id='eFKW0'><dir id='eFKW0'><q id='eFKW0'></q></dir></style></legend>
                      • <bdo id='eFKW0'></bdo><ul id='eFKW0'></ul>

                            <i id='eFKW0'><tr id='eFKW0'><dt id='eFKW0'><q id='eFKW0'><span id='eFKW0'><b id='eFKW0'><form id='eFKW0'><ins id='eFKW0'></ins><ul id='eFKW0'></ul><sub id='eFKW0'></sub></form><legend id='eFKW0'></legend><bdo id='eFKW0'><pre id='eFKW0'><center id='eFKW0'></center></pre></bdo></b><th id='eFKW0'></th></span></q></dt></tr></i><div class="sk20yws" id='eFKW0'><tfoot id='eFKW0'></tfoot><dl id='eFKW0'><fieldset id='eFKW0'></fieldset></dl></div>

                            主站蜘蛛池模板: 科昊仪器超纯水机系统-可成气相液氮罐-美菱超低温冰箱-西安昊兴生物科技有限公司 | 酶联免疫分析仪-多管旋涡混合仪|混合器-莱普特科学仪器(北京)有限公司 | 铸铁平台,大理石平台专业生产厂家_河北-北重机械 | 开锐教育-学历提升-职称评定-职业资格培训-积分入户 | 道康宁消泡剂-瓦克-大川进口消泡剂供应商 | 杭州高温泵_热水泵_高温油泵|昆山奥兰克泵业制造有限公司 | LED太阳能中国结|发光红灯笼|灯杆造型灯|节日灯|太阳能灯笼|LED路灯杆装饰造型灯-北京中海轩光电 | 3dmax渲染-效果图渲染-影视动画渲染-北京快渲科技有限公司 | 企业VI设计_LOGO设计公司_品牌商标设计_【北京美研】 | 土壤墒情监测站_土壤墒情监测仪_土壤墒情监测系统_管式土壤墒情站-山东风途物联网 | Brotu | 关注AI,Web3.0,VR/AR,GPT,元宇宙区块链数字产业 | 杭州代理记账费用-公司注销需要多久-公司变更监事_杭州福道财务管理咨询有限公司 | 齿轮减速马达一体式_蜗轮蜗杆减速机配电机-德国BOSERL齿轮减速电动机生产厂家 | 深圳3D打印服务-3D打印加工-手板模型加工厂-悟空打印坊 | 动物麻醉机-数显脑立体定位仪-北京易则佳科技有限公司 | 深圳律师咨询_深圳律师事务所_华荣【免费在线法律咨询】网 | 贵州自考_贵州自学考试网| 活性炭-蜂窝-椰壳-柱状-粉状活性炭-河南唐达净水材料有限公司 | 防爆电机-高压防爆电机-ybx4电动机厂家-河南省南洋防爆电机有限公司 | 砂磨机_立式纳米砂磨机_实验室砂磨机-广州儒佳化工设备厂家 | 制丸机,小型中药制丸机,全自动制丸机价格-甘肃恒跃制药设备有限公司 | 儿童乐园|游乐场|淘气堡招商加盟|室内儿童游乐园配套设备|生产厂家|开心哈乐儿童乐园 | Akribis直线电机_直线模组_力矩电机_直线电机平台|雅科贝思Akribis-杭州摩森机电科技有限公司 | 汽车润滑油厂家-机油/润滑油代理-高性能机油-领驰慧润滑科技(河北)有限公司 | 油冷式_微型_TDY电动滚筒_外装_外置式电动滚筒厂家-淄博秉泓机械有限公司 | 联系我们-腾龙公司上分客服微信19116098882 | 卫生纸复卷机|抽纸机|卫生纸加工设备|做卫生纸机器|小型卫生纸加工需要什么设备|卫生纸机器设备多少钱一台|许昌恒源纸品机械有限公司 | 范秘书_懂你的范文小秘书| 盘煤仪,盘料仪,盘点仪,堆料测量仪,便携式激光盘煤仪-中科航宇(北京)自动化工程技术有限公司 | 全自动翻转振荡器-浸出式水平振荡器厂家-土壤干燥箱价格-常州普天仪器 | 全自动实验室洗瓶机,移液管|培养皿|进样瓶清洗机,清洗剂-广州摩特伟希尔机械设备有限责任公司 | 溶氧传感器-pH传感器|哈美顿(hamilton) | 浴室柜-浴室镜厂家-YINAISI · 意大利设计师品牌 | 咿耐斯 |-浙江台州市丰源卫浴有限公司 | 东莞ERP软件_广州云ERP_中山ERP_台湾工厂erp系统-广东顺景软件科技有限公司 | 沧州友城管业有限公司-内外涂塑钢管-大口径螺旋钢管-涂塑螺旋管-保温钢管生产厂家 | 东莞喷砂机-喷砂机-喷砂机配件-喷砂器材-喷砂加工-东莞市协帆喷砂机械设备有限公司 | 桥架-槽式电缆桥架-镀锌桥架-托盘式桥架 - 上海亮族电缆桥架制造有限公司 | 北京软件开发_软件开发公司_北京软件公司-北京宜天信达软件开发公司 | 桁架机器人_桁架机械手_上下料机械手_数控车床机械手-苏州清智科技装备制造有限公司 | 双工位钻铣攻牙机-转换工作台钻攻中心-钻铣攻牙机一体机-浙江利硕自动化设备有限公司 | 万师讲师网-优质讲师培训师供应商,讲师认证,找讲师来万师 |