为啥我的现有客户数量不断增加?

Posted

技术标签:

【中文标题】为啥我的现有客户数量不断增加?【英文标题】:Why does my existingClients count keep rising?为什么我的现有客户数量不断增加? 【发布时间】:2021-07-25 14:22:15 【问题描述】:

我的任务是修复一些代码。每次程序运行时,它都会变得越来越慢。通过使用一些断点和分析,我认为我缩小了它背后的原因,但是我不确定如何解决它。

这个函数使用了大量的 cpu,我将它缩小到我的现有客户端变量,该变量无限期地添加 https://gyazo.com/67e44844a279a10e1b11165b941b1cf6

public void AddNewClientData()

  Logger logger = LogManager.GetCurrentClassLogger();

  try 
    string[] fileLines = File.ReadAllLines("ClientList.csv");
    foreach(string line in fileLines)
    
      string[] lineParts = line.Split(',');
      Client client = new Client()
      
        ClientName = lineParts[0],
          ClientPhone = lineParts[1],
          EntryTime = DateTime.Parse(lineParts[2]),
          ExitTime = DateTime.Parse(lineParts[3])
      ;

      bool clientExists = false;

      // Check that the user does not already exist
      string sqlGet = "Select * FROM ClientInOut";
      using(var connection = Helpers.GetConnection())
      

        connection.Open();
        List < Client > existingClients = connection.Query<Client>(sqlGet).ToList();

        for (int i = 0; i < fileLines.Length; i++)
        
          foreach(Client existingCustomer in existingClients)
          

            if (existingCustomer == client) 
              clientExists = true;

            
          

        

      
    // add the new client 
    if (clientExists == false) 
      string sql =
        "INSERT INTO ClientInOut (ClientName, ClientPhone, EntryTime, ExitTime) Values (@ClientName, @ClientPhone, @EntryTime, @ExitTime);";
      using(var connection = Helpers.GetConnection())
      
        connection.Open();
        var affectedRows = connection.Execute(sql, client);
      
    

  

【问题讨论】:

我猜existingCustomer == client 每次都会返回false。也许你应该重写 Client.Equals 函数,所以它比较字段而不是对象本身,并像 existingCustomer.Equals(client) 一样使用它 我想我会使用散列并将其沿客户端字段存储到数据库中。然后使用该哈希进行查询,而不是查询完整列表并进行迭代。 我看到一个非常不必要的嵌套 foreach() 循环,无论如何都应该在 SQL 中找到现有客户。 【参考方案1】:

== 运算符将比较对象引用,因此 existingCustomer == client 将返回 false(因为它们是不同的对象,因此它们不共享相同的引用)并且您最终会插入您的客户端。

我建议覆盖 Equals 并使用它而不是 ==

看下面的sn-p(只有两个属性可以避免噪音):

using System.IO;
using System;

public class Client

    public string Name  get;set; 
    public string Phone  get;set; 
    
    public override bool Equals(object other)
    
        if(other is Client)
        
            var c2 = (Client)other;
            // Compare the properties instead of objects references
            return c2.Name == this.Name && c2.Phone == this.Phone;
        
        return false;
    


class Program

    static void Main()
    
        Client c1 = new Client
        
            Name = "Test",
            Phone = "0055512345"
        ;
        Client c2 = new Client
        
            Name = "Test",
            Phone = "0055512345"
        ;
        
        Console.WriteLine(c1 == c2);      // returns false
        Console.WriteLine(c1.Equals(c2)); // returns true
    


注意这是极简代码,如果你选择这样,你可以改进很多:

添加null签入Equals代码 如果您使用足够新的 c# 版本进行编码,请使用类型模式匹配 也覆盖 GetHashCode 以消除警告 等

【讨论】:

== operator will compare whole objects,这并不完全正确。默认情况下,它将比较这些对象的引用(引用相等)。

以上是关于为啥我的现有客户数量不断增加?的主要内容,如果未能解决你的问题,请参考以下文章

为啥要双倍堆叠容量而不是仅仅增加固定数量?

vos优化客户线路数量

win2003 vpn增加客户端连接数量

分析时字节 [] 的荒谬大小和数量

为啥我的 javascript 不更新 Opencart category.tpl 中的数量

在 PHP 中,为啥我的 scandir 调用会返回错误数量的文件?