![]() | |
#1
| |||
| |||
|
#2
| |||
| |||
|
|
I have a table with a DATE column. I want to get a record set for all rows for this month. select * from TheTable where MeetingDate like '2011-11'; |
|
select * from TheTable where MeetingDate between '2011-11-01' and '2011-11-31'; Does it matter if there is no 31st of the month? Some quick tests suggest no, but I wouldn't depend on it. |
#3
| |||
| |||
|
|
I have a table with a DATE column. I want to get a record set for all rows for this month. select * from TheTable where MeetingDate like '2011-11'; Try: * * * * select * from TheTable where MeetingDate like '2011-11-%'; This is likely to use an index on MeetingDate if there is one. select * from TheTable where MeetingDate between '2011-11-01' and '2011-11-31'; Does it matter if there is no 31st of the month? Some quick tests suggest no, but I wouldn't depend on it. Also: * * * * select * from TheTable where year(MeetingDate) = '2011'and * * * * month(MeetingDate) = '11'; This is less likely to use an index on MeetingDate if there is one. |

#4
| |||
| |||
|
|
I have a table with a DATE column. I want to get a record set for all rows for this month. select * from TheTable where MeetingDate like '2011-11'; select * from TheTable where MeetingDate between '2011-11-01' and '2011-11-31'; Does it matter if there is no 31st of the month? |
#5
| |||
| |||
|
|
On 07-11-2011 19:21, SpreadTooThin wrote: I have a table with a DATE column. I want to get a record set for all rows for this month. select * from TheTable where MeetingDate like '2011-11'; select * from TheTable where MeetingDate between '2011-11-01' and '2011-11-31'; Does it matter if there is no 31st of the month? select * from TheTable where MeetingDate between '2011-11-01' and last_day('2011-11-01'); |
|
count(*) | +----------+ 9 | +----------+ |
#6
| |||
| |||
|
|
Luuk <L... (AT) invalid (DOT) lan> wrote: On 07-11-2011 19:21, SpreadTooThin wrote: I have a table with a DATE column. I want to get a record set for all rows for this month. select * from TheTable where MeetingDate like '2011-11'; select * from TheTable where MeetingDate between '2011-11-01' and '2011-11-31'; Does it matter if there is no 31st of the month? select * from TheTable where MeetingDate between '2011-11-01' and last_day('2011-11-01'); +1 LIKE should not be used because a DATE is internally stored as a DATE and *not* as a string. In order to check the LIKE predicate, MySQL has to convert the DATE to a string and thus cannot use an index lookup. Giving two date literals is OK because since those are constants, they can be converted to DATEs and then used for a range scan on the index. Example: mysql>show create table t1\G *************************** 1. row *************************** * * * *Table: t1 Create Table: CREATE TABLE `t1` ( * `c1` date DEFAULT NULL, * KEY `c1` (`c1`) ) ENGINE=InnoDB DEFAULT CHARSET=latin1 mysql>select count(*) from t1; +----------+ | count(*) | +----------+ | * * * *9 | +----------+ mysql>explain select count(*) from t1 where c1 between "2011-11-1" and "2011-11-30"\G *************************** 1. row *************************** * * * * table: t1 * * * * *type: range * * * * * key: c1 * * * * *rows: 3 * * * * Extra: Using where; Using index mysql>explain select count(*) from t1 where c1 like "2011-11-%"\G *************************** 1. row *************************** * * * * table: t1 * * * * *type: index * * * * * key: c1 * * * * *rows: 9 * * * * Extra: Using where; Using index The first query does a range scan on the index which will be the fastest possible plan for this type of query. The second query does an index scan and will scan *all* rows from the table. And for each row there are costs to convert the date to string and check the LIKE. XL |
#7
| |||
| |||
|
|
If this is to be used for more than one type of report, you could always use a "virtual column". |
#8
| |||
| |||
|
|
onedbguru <onedbg... (AT) yahoo (DOT) com> wrote: If this is to be used for more than one type of report, you could always use a "virtual column". [snip] No, you cannot. MySQL does not support virtual columns. Even if it would - this would be rather useless data duplication. A simple index on the DATE column does the trick. XL |
#9
| |||
| |||
|
|
On Nov 8, 2:59 am, Axel Schwenke <axel.schwe... (AT) gmx (DOT) de> wrote: onedbguru <onedbg... (AT) yahoo (DOT) com> wrote: If this is to be used for more than one type of report, you could always use a "virtual column". [snip] No, you cannot. MySQL does not support virtual columns. Even if it would - this would be rather useless data duplication. A simple index on the DATE column does the trick. XL Ahh... I was looking at something "future"??? http://forge.mysql.com/wiki/MySQL_vi...mns_ref_manual |
#10
| |||
| |||
|
|
On Nov 8, 2:59=A0am, Axel Schwenke <axel.schwe... (AT) gmx (DOT) de> wrote: MySQL does not support virtual columns. Ahh... I was looking at something "future"??? |
|
http://forge.mysql.com/wiki/MySQL_vi...mns_ref_manual |
![]() |
| Thread Tools | |
| Display Modes | |
| |