青蛙

拿到了一种遍历数据库的方式,然后发现这个东西遍历数据库奇慢.
这是因为他用了句柄来作为遍历的方式,
而句柄是递增模式的,直到递增到 db.Handseed.Value ,但是这个值非常大,有long长度的14位那么大….而且是递增的….
所以千万不要用这种方法遍历数据库…..

  1. /// <summary>
  2. /// 句柄遍历数据库
  3. /// http://www.theswamp.org/index.php?topic=39272.0
  4. /// https://through-the-interface.typepad.com/through_the_interface/2013/06/dgn-clean-up-tool-on-its-way.html
  5. /// </summary>
  6. [CommandMethod("FastIteration", CommandFlags.Modal)]
  7. public void FastIteration()
  8. {
  9. Document doc = Acap.DocumentManager.MdiActiveDocument;
  10. Database db = doc.Database;
  11. Editor ed = doc.Editor;
  12. DateTime start = DateTime.Now;
  13. long linshi = 0;
  14. long amount = 0;
  15. using (Transaction tr = db.TransactionManager.StartTransaction())
  16. {
  17. try
  18. {
  19. for (amount = db.BlockTableId.Handle.Value; amount < db.Handseed.Value; amount++)
  20. {
  21. //ObjectId id = ObjectId.Null;
  22. //Below slow code is commented:
  23. //try {
  24. // id = TargetDb.GetObjectId(false, new Handle(i), 0);
  25. //}
  26. //catch {
  27. // ++exceptCount;
  28. // continue;
  29. //}
  30. TimeSpan len = DateTime.Now - start;
  31. if (len.Seconds != linshi && len.Seconds % 1 == 0)//满足一秒输出一次,免得卡死
  32. {
  33. linshi = len.Seconds;
  34. ed.WriteMessage($"\n\r数量: {amount},已经过了{len.Days}天,{len.Hours}小时,{len.Minutes}分,{len.Seconds}秒");
  35. //刷新内容
  36. Acap.UpdateScreen();
  37. //高版本要加这句令命令栏立即执行
  38. System.Windows.Forms.Application.DoEvents();
  39. }
  40. }
  41. }
  42. catch (System.Exception e)
  43. {
  44. TimeSpan len = DateTime.Now - start;
  45. ed.WriteMessage($"\n\r错误:{amount}+{e.Message}" + $"\n\r数量: {amount},已经过了{len.Days}天,{len.Hours}小时,{len.Minutes}分,{len.Seconds}秒");
  46. }
  47. }
  48. }

edata

而且我发现一个问题,这个句柄,如果你保存了,可能会写入很多的附加对象,句柄就会浪费很多..
    比如打开文件,句柄是2c5,继续画直线,2c6…. 2c7…2c8 如果保存了.就从 313 开始..
    中间因保存,重新打开后句柄浪费了75个左右…
ctrl+c应该是先复制一个对象到 剪贴板,然后粘贴.
所以要多生成几次.
co 就不会乱增..
p.png