![]() | |
#1
| |||
| |||
|
#2
| |||
| |||
|
|
I have a table from which I'm trying to find rows with datetimes that fall within a particular period. These might be Today, Monday, This Week, This Month, August, etc. My problem is that the report must be from the point of view of local time, while the datetimes are GMT. CREATE TABLE adminLogins ( loginTime datetime, account varchar(20) ); I don't have much experience. Should I do date arithmetic in the Where clause? I imagine this problem comes up often, but I wasn't able to find anything that really addresses it. If it matters, the server's running MySQL. |
#3
| |||
| |||
|
|
Yes, it matters. Personally I don't know MySQL. Presumably there are functions available that deal with this. If you were on Oracle 9 or newer you could make the column TIMESTAMP WITH LOCAL TIMEZONE and set your DB session's TZ appropriately. With MySQL you probably have to do something like select ... from ... where loginTime >= localtime2utc('2006-01-01 ...') and loginTime < localtime2utc('2006-02-01 ...') |
#4
| ||||
| ||||
|
|
In article <4nhj3aFafil5U1 (AT) individual (DOT) net>, Robert Klemme shortcutter (AT) googlemail (DOT) com> wrote: Yes, it matters. Personally I don't know MySQL. Presumably there are functions available that deal with this. If you were on Oracle 9 or newer you could make the column TIMESTAMP WITH LOCAL TIMEZONE and set your DB session's TZ appropriately. With MySQL you probably have to do something like select ... from ... where loginTime >= localtime2utc('2006-01-01 ...') and loginTime < localtime2utc('2006-02-01 ...') Thanks Robert, I realize I didn't compose the question very well. There's a DATE_SUB function, so if I'm at GMT -6, and the server is at GMT, I could write DATE_SUB(loginTime,INTERVAL 6 HOUR) |
|
I guess I could use that everywhere I'm dealing with the datetime, but it's going to lead to some long and messy statements. |
|
My primary concern obviously is to get the right data, but in the past I've written some naive SQL that resulted in poor performance, and it feels like I'm heading in that direction again. So I was hoping for some insight in the right way to handle this presumably common problem. |
|
What I really don't want to do is get more data than I need and filter it programatically, though I haven't been above doing that before ;-) |
#5
| |||
| |||
|
|
My primary concern obviously is to get the right data, but in the past I've written some naive SQL that resulted in poor performance, and it feels like I'm heading in that direction again. So I was hoping for some insight in the right way to handle this presumably common problem. -Steve |
#6
| |||
| |||
|
|
DATE_SUB(loginTime,INTERVAL 6 HOUR) How does that deal with daylight savings time? |
#7
| |||
| |||
|
|
2) To reduce problems with performance on your queries (other than the obvious indexing etc), ensure wherever possible that any function calls are applied on the variable field (as opposed to the database field) as applying the function on the database side will effectively nullify and index on that field as each value has to be parsed through a function. |
#8
| |||
| |||
|
|
In article <4514373e$0$3614$ed2e19e4 (AT) ptn-nntp-reader04 (DOT) plus.net>, AMO NoSpam (AT) SpamFreeWorld (DOT) com> wrote: 2) To reduce problems with performance on your queries (other than the obvious indexing etc), ensure wherever possible that any function calls are applied on the variable field (as opposed to the database field) as applying the function on the database side will effectively nullify and index on that field as each value has to be parsed through a function. Amo, I'm not following you. What do you mean by variable field? |
|
If write this: where to_days(DATE_SUB(lastVisitDate,INTERVAL 6 HOUR)) BETWEEN to_days(DATE_SUB(NOW(),INTERVAL 30 HOUR)) AND to_days(DATE_SUB(NOW(),INTERVAL 6 HOUR)) Will it have to be simply brute-forced? FWIW, there's an index on lastVisitDate. |
#9
| |||
| |||
|
|
Amo, I'm not following you. What do you mean by variable field? |
#10
| |||
| |||
|
|
In essense, don't do things like (structured english): SELECT * FROM myTable WHERE returnYear(myTable.lastUpdate) = 2006. Instead do: SELECT * FROM myTable WHERE myTable.lastUpdate BETWEEN 01/01/2006 AND 31/12/2006. Try not to perform functions on any field in the WHERE clause or any field that you wish to ORDER BY. Try doing it on the value you are trying to compare with or find other solutions. |
![]() |
| Thread Tools | |
| Display Modes | |
| |