Monday, January 9, 2017

Insert 1M Records by SQL Server

declare @id int
declare @accountID bigint
declare @IP varchar(20)
select @id = 1
while @id <= 1000000
begin

//Insert 1st Table
INSERT INTO tblE2Account values('TestAccount' + CAST(@id AS VARCHAR(10)) , 'TestName',14,1,'E2TestBY',GETDATE(),'E2TestBY',GETDate(), NULL,0,0,'63,119','10.101.1.115',2)

// Take ID 
Select @accountID = Max(E2AccountId) From tblE2Account where CreatedBy='E2TestBY'
   
// Take ID and put in next Insert
INSERT INTO tblE2IP values('999.9.9.'+ CAST(@id AS VARCHAR(10)) , '23.1167,113.25','No Hostname','Kuala Lumpur','Kuala Lumpur','MY','59200','AS38199 Macro Lynx Sdn Bhd, Internet Service Provider, Malaysia',1,1,'E2TestBY',GETDATE(),NULL,NULL)

Select @IP = '999.9.9.' + CAST(@id AS VARCHAR(10))

INSERT INTO tblE2Transaction values(@IP, null, 2,14,@accountID,1,'1010017387,20.00,0','E2TestBY',GETDATE(),NULL,NULL)

select @id = @id + 1


end


//Avarage 30k records int 20 mins

Dictionary

static readonly Dictionary<string, DeviceType> MappedDeviceType;

static fnMapDeviceType()
{
    MappedDeviceType = new Dictionary<string, DeviceType>();
    MappedDeviceType.Add("Windows NT 6.3", DeviceType.Windows);
    MappedDeviceType.Add("Windows NT 6.3; WOW64", DeviceType.Windows);
    MappedDeviceType.Add("Windows NT 10.0", DeviceType.Windows);
    MappedDeviceType.Add("Windows NT 10.0; WOW64", DeviceType.Windows);
    MappedDeviceType.Add("CPU iPhone OS 9_3_2 like Mac OS X", DeviceType.Iphone);
}

//Mapping Method
public static DeviceType MapDeviceType(string device)
{
    DeviceType mapValue = DeviceType.Unknown;

    if (!string.IsNullOrWhiteSpace(device))
    {

        if (Enum.TryParse(device, true, out mapValue))
            return mapValue;

        if (MappedDeviceType.TryGetValue(device, out mapValue))
            return mapValue;
    }
    return mapValue;

}

---------------------

//Contain en with key

   string word = "en/my";

            var mappingA = new Dictionary<string, string>()
            {
                { "en", "en-gb" },
                { "cn", "cn-zh" },
            };


string listPerson = mappingA.Where(x => word.Contains(x.Key)).Select(y => y.Value).FirstOrDefault();