Is there a more efficient method to find dupes in Sql server

偶尔善良 提交于 2020-01-06 04:28:28

问题


I have this question for which I provided a solution. However, I don't feel that is as efficient as it could be:

using (DataContext context = new DataContext(SqlConnection)
{
    var custInfo = context.GetTable<tbl_CustomerInfo>();

    string compID = ImportCust.Rows[0]["CompanyID"].ToString();

    var imports = from cust in ImportCust.AsEnumerable()
                  select cust.Field<int>("CustomerID");

    var dupes = from import in imports
                join cust in custInfo
                on import equals cust.CustomerID 
                where cust.CompanyID== pivnum
                select cust;

    var records = dupes.GetEnumerator();

    while (records.MoveNext())
    { custInfo.DeleteOnSubmit(records.Current); }

    context.SubmitChanges();
}

I have used Stopwatch to look at elapsed time spanning iteration of records to completion of SubmitChanges. The elapsed times seem to have no rhyme or reason:

10666 records completed in 20 seconds
15425 records completed in 12 seconds
289 records completed in 21 seconds

Obviously, one thing that would speed things up is if I were to drop indexes. Can that be done programmatically? In addition, is there a better method than what I have provided?


回答1:


you could use SQL statment:

-- TSQL (SQL Server 2005/2008): --

select CompanyID from tbl_CustomerInfo
    group by CompanyID
    having COUNT(*)>1



回答2:


The linq version of Tefod's sql:

from ci in dc.tbl_CustomerInfo
group ci by ci.CompanyID into g
where g.Count() > 1
select g.Key


来源:https://stackoverflow.com/questions/4454930/is-there-a-more-efficient-method-to-find-dupes-in-sql-server

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!