HI Bernie
Thanks for the code. I've learned a lot in the last few days with your and
others help. I do like the idea of using "IN" but unfortunately it does seem
to be slower than my original idea. I've written some code based on Cor's
which demonstrates the difference:
The first bit of code filters the dataset using "IN" and the last bit does
it using a "Find". When I ran it this morning, the SQL code took 41 seconds
whilst the Find took 0.03 seconds. Quite a difference.
Obviously, I couldn't use the same technique i.e. "Find", from the outset,
using a non-primary key (because it needs a primary key to work).
Maybe the best approach is a mixture of both techniques?
Geoff
P.S. Code is below:
Dim dt As New DataTable
Private Sub Form1_Load(ByVal sender _
As System.Object, ByVal e As System.EventArgs) _
Handles MyBase.Load
dt.Columns.Add("ID")
dt.Columns.Add("PRODUCT")
dt.Columns.Add("COST")
For i As Integer = 0 To 200000
dt.Rows.Add(dt.NewRow)
dt.Rows(i)(0) = i.ToString
Dim rg As Random = New Random(i)
Dim rn As Integer = CInt(rg.Next(0, 200000))
dt.Rows(i)(1) = rn.ToString
rg = New Random(rn)
rn = CInt(rg.Next(0, 200000))
dt.Rows(i)(2) = rn.ToString
Next
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles Button1.Click
Dim start As DateTime = DateTime.Now
Dim numOfRows As Integer = dt.Rows.Count
Dim vue As DataView = New DataView(dt)
vue.Sort = "ID"
vue.RowFilter = "ID IN
('139211','139215','139214','139213','139212','139211','139210','139259','139258','139257','139251','139255','139254','139253','139252','139251','139250','139249','139248','139247','139241','139245','139244','139243','139242','139241','139240','139239','139238','139237','139231','139235','139234','139233','139232','139231','139230','139229','139228','139227','139221','139225','139224','139223','139222','139221','139220','139219','139218','139217','139211','139215','139214','139213','139212','139211','139210','139209','139208','139207','139201','139205','139204','139203','139202','139201','139200','139199','139198','139197','139191','139195','139194','139193','139192','139191','139190','139189','139188','139187','139181','139185','139184','139183','139182','139181','139180','139179','139178','139177','139171','139175','139174','139173','139172','139171','139170','139119','139118','139117','139111','139115','139114','139113','139112','139111','139110','139159','139158','139157','139151','139155','139154','139153','139152','139151','139150','139149','139148','139147','139141','139145','139144','139143','139142','139141','139140','139139','139138','139137','139131','139135','139134','139133','139132','139131','139130','139129','139128','139127','139121','139125','139124','139123','139122','139121','139120','139119','139118','139117','139111','139115','139114','139113','139112','139111','139110','139109','139108','139107','139101','139105','139104','139103','139102','139101','139100','139099','139098','139097','139091','139095','139094','139093','139092','139091','139090','139089','139088','139087','139081','139085','139084','139083','139082','139081','139080','139079','139078','139077','139071','139075','139074','139073','139072','139071','139070','139019','139018','139017','139011','139015','139014','139013','139012','139011','139010','139059','139058','139057','139051','139055','139054','139053','139052','139051','139050','139049','139048','139047','139041','139045','139044','139043','139042','139041','139040','139039','139038','139037','139031','139035','139034','139033','139032','139031','139030','139029','139028','139027','139021','139025','139024','139023','139022','139021','139020','139019','139018','139017','139011','139015','139014','139013','139012','139011','139010','139009','139008','139007','139001','139005','139004','139003','139002','139001','139000','138999','138998','138997','138991','138995','138994','138993','138992','138991','138990','138989','138988','138987','138981','138985','138984','138983','138982','138981','138980','138979','138978','138977','138971','138975','138974','138973','138972','139275','139274','139273','139272','139271','139270','139219','139218','139217')"
DataGrid1.DataSource = dt
Dim finish As DateTime = DateTime.Now
Dim time As TimeSpan = finish.Subtract(start)
Debug.WriteLine("Time taken = " & time.TotalSeconds)
DataGrid1.DataSource = dt
dt.PrimaryKey = New DataColumn() {dt.Columns("ID")}
start = DateTime.Now
Dim newTable As DataTable = dt.Clone
Dim str1() As String = {"139211", "139215", "139214", "139213", "139212",
"139211", "139210", "139259", "139258", "139257", "139251", "139255",
"139254", "139253", "139252", "139251", "139250", "139249", "139248",
"139247", "139241", "139245", "139244", "139243", "139242", "139241",
"139240", "139239", "139238", "139237", "139231", "139235", "139234",
"139233", "139232", "139231", "139230", "139229", "139228", "139227",
"139221", "139225", "139224", "139223", "139222", "139221", "139220",
"139219", "139218", "139217", "139211", "139215", "139214", "139213",
"139212", "139211", "139210", "139209", "139208", "139207", "139201",
"139205", "139204", "139203", "139202", "139201", "139200", "139199",
"139198", "139197", "139191", "139195", "139194", "139193", "139192",
"139191", "139190", "139189", "139188", "139187", "139181", "139185",
"139184", "139183", "139182", "139181", "139180", "139179", "139178",
"139177", "139171", "139175", "139174", "139173", "139172", "139171",
"139170", "139119", "139118", "139117", "139111", "139115", "139114",
"139113", "139112", "139111", "139110", "139159", "139158", "139157",
"139151", "139155", "139154", "139153", "139152", "139151", "139150",
"139149", "139148", "139147", "139141", "139145", "139144", "139143",
"139142", "139141", "139140", "139139", "139138", "139137", "139131",
"139135", "139134", "139133", "139132", "139131", "139130", "139129",
"139128", "139127", "139121", "139125", "139124", "139123", "139122",
"139121", "139120", "139119", "139118", "139117", "139111", "139115",
"139114", "139113", "139112", "139111", "139110", "139109", "139108",
"139107", "139101", "139105", "139104", "139103", "139102", "139101",
"139100", "139099", "139098", "139097", "139091", "139095", "139094",
"139093", "139092", "139091", "139090", "139089", "139088", "139087",
"139081", "139085", "139084", "139083", "139082", "139081", "139080",
"139079", "139078", "139077", "139071", "139075", "139074", "139073",
"139072", "139071", "139070", "139019", "139018", "139017", "139011",
"139015", "139014", "139013", "139012", "139011", "139010", "139059",
"139058", "139057", "139051", "139055", "139054", "139053", "139052",
"139051", "139050", "139049", "139048", "139047", "139041", "139045",
"139044", "139043", "139042", "139041", "139040", "139039", "139038",
"139037", "139031", "139035", "139034", "139033", "139032", "139031",
"139030", "139029", "139028", "139027", "139021", "139025", "139024",
"139023", "139022", "139021", "139020", "139019", "139018", "139017",
"139011", "139015", "139014", "139013", "139012", "139011", "139010",
"139009", "139008", "139007", "139001", "139005", "139004", "139003",
"139002", "139001", "139000", "138999", "138998", "138997", "138991",
"138995", "138994", "138993", "138992", "138991", "138990", "138989",
"138988", "138987", "138981", "138985", "138984", "138983", "138982",
"138981", "138980", "138979", "138978", "138977", "138971", "138975",
"138974", "138973", "138972", "139275", "139274", "139273", "139272",
"139271", "139270", "139219", "139218", "139217"}
For Each st As String In str1
Dim row As DataRow = dt.Rows.Find(st)
If newTable.Rows.Find(st) Is Nothing Then
newTable.ImportRow(row)
End If
Next
finish = DateTime.Now
time = finish.Subtract(start)
Debug.WriteLine("Time taken = " & time.TotalSeconds)
DataGrid1.DataSource = newTable
End Sub