Speed up the search process in a big amount of data? - vb.net

I have a SQLite (v3) database with following example table:
5 columns ("FirstName", "LastName", "Street", ZIPCode, "City")
more than 1'100'000 rows
I'm looking for the fastest method in VB.NET to find an entered search string in the entire data. This entered search string should match the complete content of a field OR only a part of it.
So far, I try to load the whole data into a datatable (takes about 40 seconds). Then I try to search with this SQL command:
dt.Select("FirstName LIKE '%" + SearchString + "%'")
In my other method, I create a sorted DataView and search with the RowFilter command:
dvSORTED.RowFilter = "FirstName LIKE '%" & SearchString & "%'"
The first method returns smaller amounts of results (~100) within 1,1 seconds, the second method needs 1,5 seconds.
This is true for a search in a single column. For every additional column, the search time will be multiplied by the above time. A search in all columns needs about 8 seconds (5 x 1,5).
Little comparison: If I execute this SQL command directly in SQLiteSpy
SELECT FirstName, LastName, Street, ZIPCode, City FROM Addresses WHERE FirstName LIKE "%Peter%" or LastName LIKE "%Peter%" or Street LIKE "%Peter%" or City LIKE "%Peter%"
then I have to wait about 10 seconds for the result.
How can I speed up the search process in all the available data?

Try putting all of the strings you need to search in a single string and ask if it contains the value you are looking for. Here are my test results with 1.7M records (fairly unique):
If DT.Select("street like '%" & SearchString& "%'").Count > 0 Then
This took about 11 seconds to run
If I put all my values into a string with a pike "|" separator and then ran
If StringToSearch.Contains(SearchString) then...
This took about 20ms. Even the time to put all the strings into a single string only took 2 seconds so I could reload the StringToSearch every time and still be faster than the SELECT. I wasn't even using a StringBuilder. But, if you can keep the variable 'StringToSearch' around, and not rebuild it everytime, it will be way faster.
BTW, iterating through the rows and asking each field if it contains the value took 1.5 seconds:
If dr("street").ToString.Contains(SearchString) Then
Lastly, I did try Regex but is was the same time as Contains.

Related

SQL: Compare Date Range in a String text field

I have an MS Access db. I am writing an application in C# to access it. I have a field "FileName" of type "Short Text in MS Access. Data in FileName field is like "Test 11-12-2004 15.11.15".
Using a Date Range, I got to search records based on FileName field. I am not able to get - How do I compare the date of this format and retrieve the records ? FileName is a Text type and date is a substring of it. Retrieving only the date part and comparing with >= beginDate && <= endDate seems like a puzzle to me.
Can anyone suggest how do I write SQL query to perform this date range comparision and retrieve those records - "Select * from TestHead where FileName......" ????
Any help is appreciated.
Thanks a lot,
In your C# code, as you are going through the records, I'd split the string like this:
char[] delimiters = {' '};
string[] FileNameParts = FileName.Split(delimiters);
This will result in an array FileNameParts, the second element of which will contain the date, which you can convert to an actual date for use in the query:
DateTime FileNameDate = Convert.ToDateTime(FileNameParts(1))
Something along the lines of:
sSQL = "SELECT * FROM Table WHERE " & beginDate & " <= " & FileNameDate
I see this as preferable to adding a column to your table that contains the date substring of the FileName field, because then you constantly need to be updating that column whenever existing records are modified or new records are added. That means more clutter on the C# side, or an UPDATE query on the Access side which at least needs to get called periodically. Either way it would be more communication with the database.

textbox matching pattern from database table

I have a name textbox and I want to find any names included in the typed text in the database table patient details name column. I know how to use LIKE operator if you know the letters you want the search to start/end with etc. but this time I want textbox. I think my issue is with the quotations; I tried to play around with it but it didn't work!
From x in PatientDetails where ( x.Patient_Name Like '%" Textbox1.Text "%' )
For example: If a Patient name in the database is: John Matt
and a user typed Matt, the above record for John Matt should be returned.
P.S I tried looking it up in Google but it mostly discuss characters not entered text box
Thank you all.
Something like this would do
C#
var query = (from x in PatientDetails
where x.Patient_Name.Contains(Textbox1.Text)
select x).ToList();
VB.NET - Converted using CodeConverter
Dim query = (From x In PatientDetails Where
x.Patient_Name.Contains(Textbox1.Text)x).ToList()

Can we allow user to only select one parameter at a time in SSRS

I have three date columns. My report has 6 parameters: start and end date range for all three columns. Currently user has to select all date range but what if I want to allow user to only select one date range at a time. I cannot do "allow NULL values" option in the parameter because that see it as a field containing null value. I don't think it's possible to allow user to select only one parameter at a time so I'm trying an approach where there will be three parameters: one will consists of date field names. And rest two are based on date range of the field that is select from previous parameter. For example user selects a date field name from first and then date parameters will be cascaded and grab a value of date field based on the date field name that is selected in previous parameter. But I'm not sure exactly how to approach this. Any ideas?
I do that in some of my reports. The first parameter is "Does the range apply to A, B, or C" and the second and third parameters are the start and end data respectively. Well, I use integers, but dates should work the same as long as you format them.
The way it works, is you set the query in your dataset to be a function, and build it as mostly a quote but with the parameter values substituted in. A typical one might be
= "SELECT * FROM dbo.Trips WHERE " + Parameters!WhatField.Value + " between '" + FormatDateTime(Parameters!StartDate.Value, dateformat.shortdate) + "' and '" + FormatDateTime(Parameters!EndDate.Value, dateformat.shortdate) + "'"
Your parameter "WhatField" is a drop down list with 3 permitted values, make the value be the field name and the display be what your user wants to see as a description of the field.
I think you have to set the query for delayed evaluation somewhere (but I can't spot where right now, so maybe I'm mis-remembering), and you should set default values for your parameters that don't crash your report, but other than that it's fairly straightforward.
Oh, and to make the query a function it's just like a text query but hit the button to the right of the text box - it has a "fx" on it
If you need an even more complex query, you can put the whole query text in code (off the report properties) and call that function from the "fx" button to generate your query string.

Why doesn't my query use my criteria?

I have a db in Access and I'm trying to get a textbox to run my query and pass an other bounded textbox's value in as the criteria in DLookUp. I have the query running in design view and when I enter the criteria directly it returns the correct results. When I open the report it gives me the sum of all the possible rows. In other words it doesn't filter the rows.
I haven't used Access in about twelve years, thankfully, and everything I've done up to this point has been tutorial/example patchwork, but here it is...
SQL Query:
SELECT Sum(IIf(Attended=-1,1,0)) AS attendance
FROM Students_Classes_Attendance
WHERE (((CStr([Students_Classes_Attendance].[Class_Id]))=[classId]));
DLookUp as Control Source:
=DLookUp("[Total Attendance by Class]![attendance]",
"[Total Attendance by Class]",
"[Class_Id] =" & [Class_Id])
I'm lost at the moment. I'm guessing that the value isn't there before the query fires and since the criteria is an optional parameter that it's being passed null, but I would hope you'd get an error from that. Not that #Error is very meaningful anyway.
Does anyone know for certain the problem and the best way to correct it? Thanks.
Edit:
I did the changes recommended in the answer so now my DLookUp looks like...
=DLookUp("[attendance]",
"[Total Attendance by Class]",
"[Class_Id] =" & [Class_Id])
...still returns the total for all rows. Removing the criteria completely makes no difference either, which returns me to thinking it has something to do with the bound textbox not having a value.
DLookup uses the following syntax:
Syntax for numerical values:
DLookup("FieldName" , "TableName" , "Criteria = n")
Syntax for strings: (note the single apostrophe before and after the string value)
DLookup("FieldName" , "TableName" , "Criteria= 'string'")
Syntax for dates: (note the # before and after the date value)
DLookup("FieldName" , "TableName" , "Criteria= #date#")
I believe you just need to remove the table name from the first parameter. Try this:
=DLookUp("[attendance]", "[Total Attendance by Class]", "[Class_Id] = " & [Class_Id])
Keep in mind that if Class_Id is a Text Field, you need to surround it by single quotes:
=DLookUp("[attendance]", "[Total Attendance by Class]", "[Class_Id] = '" & [Class_Id] & "'")

Search access db using 3 fields

How can I search using 3 fields on vb.net
Usually we use something like:
Dim query As String = "Insert into () values () Where id=1"
I am not really good in access or sql, so I asked.
week, sow order and piglet# are different fields, because I need to segregate in the future if the user searches only on weeks.
What I want is, get the data which is the same as what the user inputs and display it in the DGV. Is something like this possible:
Dim query As String = "Insert into (FarrowDate) values (dtpFarrow.Text) Where week=1,soworder=1,pigletnumber=0"
' display the data in the DGV '
Help please, they only gave me a 2 week deadline >.<
In your query you cannot use where in insert statement.Use update statement
Update tablename set FarrowDate='' where PigletNumber='0' And Week='1' And SowOrder='1'