Global Date Filter for DataAdapter?

  • Thread starter Thread starter Toby
  • Start date Start date
T

Toby

I've a DataAdapter with:

SELECT ID, Employee, [Date], Period_End_Date, Job, Description, Exported,
Units, Cost_Code, Category, Class, Chargeout_Level, PayID, Rate
FROM tblTimeEntry
WHERE (Employee LIKE ?) AND (Period_End_Date = ?)
ORDER BY [Date]

I then get my Data by the following:

' Select Data Based upon Current Week Ending and Employee ID
daUserData.SelectCommand.Parameters.Add("@Employee", OleDbType.Char.Char, 10)
daUserData.SelectCommand.Parameters.Add("@MyDateField ", OleDbType.Date)
daUserData.SelectCommand.Parameters(0).Value = strUserID
daUserData.SelectCommand.Parameters(1).Value = WeekEndDate
daUserData.Fill(DsUserData.tblTimeEntry)

with the 'date' variable WeekEndDate I choose a 'specific' date.

What I'd like to know, is what do I use as a 'global' search for ALL dates
using the above?

"%" and "*" of course do not work as I have to search a 'DATE'.

Anyone know?

Thanks

Toby
 
Hi Toby,

You cannot use any wildcards for the dates. What you could do here is to use
SQL statement without date condition or use doubled condition for the date
and pass range of the dates, which basically covers everything. Something
like

SELECT ID, Employee, [Date], Period_End_Date, Job, Description,
Exported,
Units, Cost_Code, Category, Class, Chargeout_Level, PayID, Rate
FROM tblTimeEntry
WHERE (Employee LIKE ?) AND Period_End_Date >= ? AND Period_End_Date <=
?
ORDER BY [Date]

daUserData.SelectCommand.Parameters.Add("@Employee", OleDbType.Char.Char,
10)
daUserData.SelectCommand.Parameters.Add("@MyDateField1", OleDbType.Date)
daUserData.SelectCommand.Parameters.Add("@MyDateField2", OleDbType.Date)
daUserData.SelectCommand.Parameters(0).Value = strUserID
daUserData.SelectCommand.Parameters(1).Value = WeekStartDate
daUserData.SelectCommand.Parameters(2).Value = WeekEndDate
daUserData.Fill(DsUserData.tblTimeEntry)

In case wehn you need to check for one date, you could pass it as a value
for both parameters

--
Val Mazur
Microsoft MVP
Check Virus Alert, stay updated
http://www.microsoft.com/security/incident/blast.asp


Toby said:
I've a DataAdapter with:

SELECT ID, Employee, [Date], Period_End_Date, Job, Description, Exported,
Units, Cost_Code, Category, Class, Chargeout_Level, PayID, Rate
FROM tblTimeEntry
WHERE (Employee LIKE ?) AND (Period_End_Date = ?)
ORDER BY [Date]

I then get my Data by the following:

' Select Data Based upon Current Week Ending and Employee ID
daUserData.SelectCommand.Parameters.Add("@Employee", OleDbType.Char.Char, 10)
daUserData.SelectCommand.Parameters.Add("@MyDateField ", OleDbType.Date)
 
You cannot use any wildcards for the dates. What you could do here is to use
SQL statement without date condition or use doubled condition for the date
daUserData.SelectCommand.Parameters(1).Value = WeekStartDate
daUserData.SelectCommand.Parameters(2).Value = WeekEndDate

Okay... I recall seeing this start-end date once. But didn't realize that it
was my only option.

Thanks!

I'll take it from here on!

Toby
 
Val Mazur said:
Hi Toby,

You cannot use any wildcards for the dates. What you could do here is to use
SQL statement without date condition or use doubled condition for the date
and pass range of the dates, which basically covers everything. Something
like

SELECT ID, Employee, [Date], Period_End_Date, Job, Description,
Exported,
Units, Cost_Code, Category, Class, Chargeout_Level, PayID, Rate
FROM tblTimeEntry
WHERE (Employee LIKE ?) AND Period_End_Date >= ? AND Period_End_Date <=
?
ORDER BY [Date]

If your SQL dialect supports it, consider changing the two tests for the
date criteria to a single test using the BETWEEN clause. Something like
this :

AND Period_End_Date BETWEEN ? AND ?

regards
roy fine
 
Roy Fine said:
If your SQL dialect supports it, consider changing the two tests for the
date criteria to a single test using the BETWEEN clause. Something like

I'm using OLE and an Access DB file.
AND Period_End_Date BETWEEN ? AND ?

I'll see if this works too.

Thanks!

Toby
 
Yes, it supports BETWEEN

What I found interested (not sure if this is correct)... is that now with the
WeekStart and WeekEnd search... my loading of the application has increased (5
seconds or so out of 10 normally).

Would this be 'typical'?

Toby
 
Hi Toby,

If you do not have indexes created for those fields, then yes, you could
expect it. You should build indexes on a fields, involved into search by
WHERE clause
 
If you do not have indexes created for those fields, then yes, you could
expect it. You should build indexes on a fields, involved into search by
WHERE clause

Indexes? Hmmm... not sure by what you mean.

Sorry... I've only been using DBs for about 1-1/2 months now. And while I've
learnt a lot... I'm still out of my depth here sometimes :(

Toby
 
Hi,

Index allows you to boost selection from the table. When you create index,
for the specific field(s), then database maintains it to provide faster
search. I would suggest you to read about databases structure and how to
create indexes. This is not a five minutes topic. If you do not create
appropriate indexes, then you should expect slow selection from the database
 
VERY GOOD advice Val!

I would only add that if one creates too many indexes, additions, inserts,
and update ALL suffer.

roy fine
 
Toby,

Without indexes, the database is left to search the entire table for matches
on the Where clause - these are often referred to as FTS or Full Table Scan.
These acceses occur when you select from a single table, and also when you
join two or more tables.

As a general rule of thumb, when you are expecting less than 10% of the rows
to be returned, and where the size of the table is greater than 10 blocks,
indexes have the potential of adding an incredible performance boost.

Consider the problem in searching a table of customers for those records
that have the phone number of 800-555-1212 (US NAN assumed here).

If there are no indexes, and if there are 2,000,000 rows in the database,
and each row is on average 320 bytes, and if you expect only one or two rows
in the resultset, the database reads all 2,000,000 rows, comparing each
phone number to the value above. If we assume a block size of 8k, then we
may be able to squeeze 25 or so records into a block. To read the 2,000,000
rows would require 87,000 or so disk reads. (**disk reads are VERY slow**)

This would result in a substantial amount of compute resources to do the
2,000,000 string compares, as well as an incredible amount of disk activity
if we assume that the customer table was not already in memory (650 mbytes
plus some for overhead of segment headers, etc) - and all that work for just
a couple of records. One solution would be an index on phonenumber. An
index is a separate segment, very much related to the associated table, and
is organized in a structure that is optimized for seaches, especially
searches that involve a test for equality (or inequality). In the simplest
case (excluding IOTs, clusters, and composite indexes) the index is
typically a balanced, sorted tree structure.

The root of the tree is some value near the middle, and the root has two
nodes - a left node and a right node. The left and right nodes are for all
values that less than the root value and greater than the root value
respectively. Each of these left and right nodes is again a distinct node
that is the root for the structure below it, each with a left and right
node, and so on until you get to the terminal nodes, which are typically
called leaf nodes. A leaf node contains a value (a phone number is our
case) and a block and offset pointer into the associated table.

While this is an absurd simplification, the concept is quite solid. The
subject is treated quite well by Knuth in his Vol I, The Art of Computer
Programming-Fundamental Algorithms. The actual implementation is platform
specific, and well optimized for block (or multiblock) disk reads (i.e. a
node may not be atomic as implied, rather a block of nodes - or as many
values as would fit in a block) - mainly because indexes are there to
minimize disk reads (although other benefits include supporting constraints
and sorting).

Back to our problem of above; using the index, the records that satisfy the
where clause might be accessed in as few as a 6 or so disk reads. It is the
ratio of the disk reads using an index to the disk reads of a FTS that we
might use to measure the effectiveness of the index. But indexes do not
alway help! Consider what happens when we write the select statement such
that it returns 1,000,000 records (i.e. all of the records in a particular
state or country) The worse case for the FTS would be 87,000 disk reads.
But with the index, we could easily exceed that count several times if the
distibution of the items in the data table were dramatically different from
the distribution of the keys in the index block (i.e. the index is sorted by
state or country, the table data is sorted by person's last name, and no two
adjacent index leaf nodes point to records in the same block in the data
table). This is a case where the index actually slows performance - and a
FTS is a LOT quicker.

As Val said, this subject is not a 5 minute topic - and moreover, it is not
about "to index or not to index", it is about appropriate indexes - based on
the data and how it will be accessed (select, update, insert, etc). Hang in
there -- while this stuff may seem overly complicated at first, it's just
way tooooo much fun to even consider doing anything else... :)

regards
roy fine
 
Val Mazur said:
Index allows you to boost selection from the table. When you create index,
for the specific field(s), then database maintains it to provide faster

.... sort of like a search of the original DB... but now only a fraction of the
size? (and of course, on the Client side of the line)
search. I would suggest you to read about databases structure and how to
create indexes. This is not a five minutes topic. If you do not create

Hmmm... I've David Sceppa's ADO.NET book... and a quick glance shows there is
nothing in the Index on 'indexes'. I'll have to dig into this some more.

Thanks!

Toby
 
Without indexes, the database is left to search the entire table for matches
on the Where clause - these are often referred to as FTS or Full Table Scan.
Okay.

ratio of the disk reads using an index to the disk reads of a FTS that we
might use to measure the effectiveness of the index. But indexes do not
alway help! Consider what happens when we write the select statement such
that it returns 1,000,000 records (i.e. all of the records in a particular

I understand. Currently I'm only reading about 20,000 to 30,000 records (not
in the Millions). So for me, it's probably not an issue.
As Val said, this subject is not a 5 minute topic - and moreover, it is not
about "to index or not to index", it is about appropriate indexes - based on

Points taken. I'll try finding if I've anything on "Indexes" in my books
(can't see anything yet)... if I do, I'll probably try it out to see IF in my
case it is worth it or not.

Thanks for the info!

Toby
 
Back
Top