瀏覽代碼

[TreeFrog] Using bulk updates (#8611)

* modified to use bulk-update query.

* update

* update
TreeFrog Framework 1 年之前
父節點
當前提交
cae068cebc
共有 1 個文件被更改,包括 33 次插入1 次删除
  1. 33 1
      frameworks/C++/treefrog/controllers/worldcontroller.cpp

+ 33 - 1
frameworks/C++/treefrog/controllers/worldcontroller.cpp

@@ -3,6 +3,7 @@
 #include "pworld.h"
 #include "mngworld.h"
 #include <TCache>
+#include <TSqlQuery>
 
 
 void WorldController::index()
@@ -246,17 +247,48 @@ void WorldController::cached_pqueries(const QString &num)
 
 void WorldController::pupdates(const QString &num)
 {
+    const QString statement("UPDATE world SET randomnumber = CASE id");
     QVariantList worlds;
+    QString ids;
+    QString q = statement;
+    q.reserve(4096);
     int d = std::min(std::max(num.toInt(), 1), 500);
     PWorld world;
 
+    auto blkupdate = [&q, &ids, &statement]() {
+        if (!ids.isEmpty()) {
+            ids.chop(1);
+            q += QStringLiteral(" END WHERE id IN (%1)").arg(ids);
+            TSqlQuery query;
+            query.exec(q);
+            ids.clear();
+            q = statement;
+        }
+    };
+
     for (int i = 0; i < d; ++i) {
         int id = Tf::random(1, 10000);
         world = PWorld::get(id);
         world.setRandomNumber( Tf::random(1, 10000) );
-        world.update();
+        q += QLatin1String(" WHEN ");
+        q += QString::number(world.id());
+        q += QLatin1String(" THEN ");
+        q += QString::number(world.randomNumber());
+        ids += QString::number(world.id());
+        ids += ',';
         worlds << world.toVariantMap();
+
+        if (!((i + 1) % 200)) {
+            blkupdate();
+        }
     }
+
+    if (d == 1) {
+        world.update();
+    } else {
+        blkupdate();
+    }
+
     renderJson(worlds);
 }