Unix Technical Forum

getting records for 1 day

This is a discussion on getting records for 1 day within the MySQL forums, part of the Database Server Software category; --> in SQL, i wonder if I do a select * from tablefoo where add_date = "2007-11-01" then it may ...


Go Back   Unix Technical Forum > Database Server Software > MySQL

FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 02-28-2008, 11:29 AM
Summercool
 
Posts: n/a
Default getting records for 1 day

in SQL, i wonder if I do a


select * from tablefoo where add_date = "2007-11-01"


then it may not show any record as it will only match recorded added
exactly at 2007-11-01 00:00:00
so to limit that day, I could use

select * from tablefoo where date(add_date) = "2007-11-01"

except I think if the table has millions of records, then it can take
forever to run, as it will go through all records and apply the date
function on each record's add_date.

so the following

select * from tablefoo where add_date >= "2007-11-01" and add_date <
"2007-11-02"

should work... except it is quite verbose... i wonder if there is a
better way?

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #2 (permalink)  
Old 02-28-2008, 11:29 AM
J.O. Aho
 
Posts: n/a
Default Re: getting records for 1 day

Summercool wrote:
> in SQL, i wonder if I do a
>
>
> select * from tablefoo where add_date = "2007-11-01"


this works for mysql:
SELECT * FROM tablefoo WHERE add_date>20071101 AND add_date<20071102



--

//Aho
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #3 (permalink)  
Old 02-28-2008, 11:29 AM
Paul Lautman
 
Posts: n/a
Default Re: getting records for 1 day

Summercool wrote:
> in SQL, i wonder if I do a
>
>
> select * from tablefoo where add_date = "2007-11-01"
>
>
> then it may not show any record as it will only match recorded added
> exactly at 2007-11-01 00:00:00

Only if add_date is a DATETIME or TIMESTAMP type of field as oposed to a
DATE type.


Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #4 (permalink)  
Old 02-28-2008, 11:29 AM
Awlnoing
 
Posts: n/a
Default Re: getting records for 1 day


"Paul Lautman" <paul.lautman@btinternet.com> wrote in message
news:5p5r0uFpgjp6U1@mid.individual.net...
> Summercool wrote:
>> in SQL, i wonder if I do a
>>
>>
>> select * from tablefoo where add_date = "2007-11-01"
>>
>>
>> then it may not show any record as it will only match recorded added
>> exactly at 2007-11-01 00:00:00

> Only if add_date is a DATETIME or TIMESTAMP type of field as oposed to a
> DATE type.
>
>


Many possibilities...

I always add a indexed DateID column that holds a FK to my Dates table. An
integer lookup will always be faster.

or

You could add (and index) a column that holds only the date portion of the
datetime field. There are many ways to truncate the time portion, I use
convert(datetime,convert(varchar(50),add_date,101) )

or

You could use ...BETWEEN '2007-11-01' AND '2007-11-02'


Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #5 (permalink)  
Old 02-28-2008, 11:29 AM
VC
 
Posts: n/a
Default Re: getting records for 1 day

Try this.

select * from tablefoo
where convert(varchar, add_date, 101) = '11/01/2007'


"Summercool" <Summercoolness@gmail.com> wrote in message
news:1194176707.385279.102190@t8g2000prg.googlegro ups.com...
> in SQL, i wonder if I do a
>
>
> select * from tablefoo where add_date = "2007-11-01"
>
>
> then it may not show any record as it will only match recorded added
> exactly at 2007-11-01 00:00:00
> so to limit that day, I could use
>
> select * from tablefoo where date(add_date) = "2007-11-01"
>
> except I think if the table has millions of records, then it can take
> forever to run, as it will go through all records and apply the date
> function on each record's add_date.
>
> so the following
>
> select * from tablefoo where add_date >= "2007-11-01" and add_date <
> "2007-11-02"
>
> should work... except it is quite verbose... i wonder if there is a
> better way?
>



Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #6 (permalink)  
Old 02-28-2008, 11:29 AM
Paul Lautman
 
Posts: n/a
Default Re: getting records for 1 day

VC wrote:
>> in SQL, i wonder if I do a
>>
>>
>> select * from tablefoo where add_date = "2007-11-01"
>>
>>
>> then it may not show any record as it will only match recorded added
>> exactly at 2007-11-01 00:00:00
>> so to limit that day, I could use
>>
>> select * from tablefoo where date(add_date) = "2007-11-01"
>>
>> except I think if the table has millions of records, then it can take
>> forever to run, as it will go through all records and apply the date
>> function on each record's add_date.
>>
>> so the following
>>
>> select * from tablefoo where add_date >= "2007-11-01" and add_date <
>> "2007-11-02"
>>
>> should work... except it is quite verbose... i wonder if there is a
>> better way?

> Try this.
>
> select * from tablefoo
> where convert(varchar, add_date, 101) = '11/01/2007'
>
>
> "Summercool" <Summercoolness@gmail.com> wrote in message
> news:1194176707.385279.102190@t8g2000prg.googlegro ups.com...

Please do not top post - top posting fixed.

Why is your suggestion any better than the OP's date() function call?



Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #7 (permalink)  
Old 02-28-2008, 11:29 AM
Charles Hooper
 
Posts: n/a
Default Re: getting records for 1 day

On Nov 4, 6:45 am, Summercool <Summercooln...@gmail.com> wrote:
> in SQL, i wonder if I do a
>
> select * from tablefoo where add_date = "2007-11-01"
>
> then it may not show any record as it will only match recorded added
> exactly at 2007-11-01 00:00:00
> so to limit that day, I could use
>
> select * from tablefoo where date(add_date) = "2007-11-01"
>
> except I think if the table has millions of records, then it can take
> forever to run, as it will go through all records and apply the date
> function on each record's add_date.
>
> so the following
>
> select * from tablefoo where add_date >= "2007-11-01" and add_date <
> "2007-11-02"
>
> should work... except it is quite verbose... i wonder if there is a
> better way?


Is the ADD_DATE column defined in the database as a DATE? If so, some
people may be tempted to write:
SELECT
*
FROM
TABLEFOO
WHERE
TRUNC(ADD_DATE) = '01-NOV-2007';

The assumption of the above is that there is an index on the ADD_DATE
column that will help speed data retrieval... only to find that Oracle
performs a full tablescan to identify the matching rows. A function
based index could be set up to allow the above syntax to execute
without a full tablescan, but is that the best approach? In my
opinion, I would not create another index unless there were no other
choices. For example, I would use one of the following, most likely
the first:
SELECT
*
FROM
TABLEFOO
WHERE
ADD_DATE >= '01-NOV-2007'
AND ADD_DATE < '02-NOV-2007';

SELECT
*
FROM
TABLEFOO
WHERE
ADD_DATE BETWEEN '01-NOV-2007' AND '02-NOV-2007'
AND ADD_DATE <> '02-NOV-2007';

The second predicate in the second SQL statement's WHERE clause is
necessary to prevent those matches that occur at exactly midnight on
02-NOV-2007 from being included. If ADD_DATE is a VARCHAR2 column,
either of the above methods will also work (after reformatting the
date constant), but Oracle may incorrectly predict the number of rows
that will be returned by the query, and may force a full tablescan,
even if there is an index on the ADD_DATE column - if that happens, an
INDEX hint may be used to force an index based execution plan.

In summary: The shortest programming solution may not be the most
efficient solution.

Charles Hooper
IT Manager/Oracle DBA
K&M Machine-Fabricating, Inc.

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #8 (permalink)  
Old 02-28-2008, 11:29 AM
Maxim Demenko
 
Posts: n/a
Default Re: getting records for 1 day

Charles Hooper schrieb:
> On Nov 4, 6:45 am, Summercool <Summercooln...@gmail.com> wrote:
>> in SQL, i wonder if I do a
>>
>> select * from tablefoo where add_date = "2007-11-01"
>>
>> then it may not show any record as it will only match recorded added
>> exactly at 2007-11-01 00:00:00
>> so to limit that day, I could use
>>
>> select * from tablefoo where date(add_date) = "2007-11-01"
>>
>> except I think if the table has millions of records, then it can take
>> forever to run, as it will go through all records and apply the date
>> function on each record's add_date.
>>
>> so the following
>>
>> select * from tablefoo where add_date >= "2007-11-01" and add_date <
>> "2007-11-02"
>>
>> should work... except it is quite verbose... i wonder if there is a
>> better way?

>
> Is the ADD_DATE column defined in the database as a DATE? If so, some
> people may be tempted to write:
> SELECT
> *
> FROM
> TABLEFOO
> WHERE
> TRUNC(ADD_DATE) = '01-NOV-2007';
>
> The assumption of the above is that there is an index on the ADD_DATE
> column that will help speed data retrieval... only to find that Oracle
> performs a full tablescan to identify the matching rows. A function
> based index could be set up to allow the above syntax to execute
> without a full tablescan, but is that the best approach? In my
> opinion, I would not create another index unless there were no other
> choices. For example, I would use one of the following, most likely
> the first:
> SELECT
> *
> FROM
> TABLEFOO
> WHERE
> ADD_DATE >= '01-NOV-2007'
> AND ADD_DATE < '02-NOV-2007';
>
> SELECT
> *
> FROM
> TABLEFOO
> WHERE
> ADD_DATE BETWEEN '01-NOV-2007' AND '02-NOV-2007'
> AND ADD_DATE <> '02-NOV-2007';
>
> The second predicate in the second SQL statement's WHERE clause is
> necessary to prevent those matches that occur at exactly midnight on
> 02-NOV-2007 from being included. If ADD_DATE is a VARCHAR2 column,
> either of the above methods will also work (after reformatting the
> date constant), but Oracle may incorrectly predict the number of rows
> that will be returned by the query, and may force a full tablescan,
> even if there is an index on the ADD_DATE column - if that happens, an
> INDEX hint may be used to force an index based execution plan.
>
> In summary: The shortest programming solution may not be the most
> efficient solution.
>
> Charles Hooper
> IT Manager/Oracle DBA
> K&M Machine-Fabricating, Inc.
>


Charles, he has crossposted on all possible RDBMS newsgroups, i don't
think, there is a commons answer (in terms of performance) to his
question, first should be specified, on which RDBMS is it intended to run.

Best regards

Maxim
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On
Forum Jump


All times are GMT. The time now is 06:15 PM.


Powered by vBulletin® Version 3.6.5
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
SEO by vBSEO 3.2.0
www.UnixAdminTalk.com