Indexes with [code]< or >[/code] comparisons
8 Message(s) by 4 Author(s) originally posted in mysql discussion
| From: spamtheaussie |
Date: Friday, October 19, 2007
|
Hi,
I'm trying to figure out why MySQL won't use my
index when I do a
<
comparison, but will use it when I do an = comparison.
I thought a BTREE index should work for < comparisons?
mysql> create index start_
date _index using BTREE on
case_sessions(start_date);
Query OK, 423608
row s affected (12.99 sec)
Records: 423608 Duplicates: 0 Warnings: 0
mysql> explain SELECT * FROM case_sessions where start_date
<
'2007-06-02 08:00';
+----+-------------+---------------+------+------------------+------
+---------+------+--------+-------------+
| id | select_type | table | type | possible_key s | key |
key_len | ref | rows | Extra |
+----+-------------+---------------+------+------------------+------
+---------+------+--------+-------------+
| 1 | SIMPLE | case_sessions | ALL | start_date_index | NULL |
NULL | NULL | 423608 | Using where |
+----+-------------+---------------+------+------------------+------
+---------+------+--------+-------------+
1 row in set (0.00 sec)
mysql> explain SELECT * FROM case_sessions where start_date =
'2007-06-02 08:00';
+----+-------------+---------------+------+------------------
+------------------+---------+-------+------+-------+
| id | select_type | table | type | possible_keys |
key | key_len | ref | rows | Extra |
+----+-------------+---------------+------+------------------
+------------------+---------+-------+------+-------+
| 1 | SIMPLE | case_sessions | ref | start_date_index |
start_date_index | 8 | const | 1 | |
+----+-------------+---------------+------+------------------
+------------------+---------+-------+------+-------+
1 row in set (0.00 sec)
Can anyone give me a clue?
Thanks,
RobCREATE TABLE `case_sessions` (
`case_id` bigint(20) NOT NULL default '0',
`start_date` datetime NOT NULL default '0000-00-00 00:00:00',
`end_date` datetime default NULL,
`beginning_status_code` tinytext NOT NULL,
`working_agent_login_id` tinytext,
`updating_system` tinytext,
`updating_agent_login_id` tinytext,
`updating_external_party_id` tinytext,
`creation_date` datetime NOT NULL default '0000-00-00 00:00:00',
`created_by` varchar(8) NOT NULL default '',
`last_updated_date` datetime NOT NULL default '0000-00-00 00:00:00',
`last_updated_by` varchar(8) NOT NULL default '',
`source` tinytext NOT NULL,
PRIMARY KEY
(`case_id`,`start_date`,`beginning_status_code`(2),`source`(2)),
KEY `start_date_index` USING BTREE (`start_date`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8
| From: spamtheaussie |
Date: Friday, October 19, 2007
|
Hopefully this is slightly easier to read:
mysql> explain SELECT * FROM case_sessions where start_date <
'2007-06-02 08:00'\G;
*************************** 1. row ***************************
id: 1
select_type: SIMPLE
table: case_sessions
type: ALL
possible_keys: start_date_index
key: NULL
key_len: NULL
ref: NULL
rows: 412863
Extra: Using where
mysql> explain SELECT * FROM case_sessions where start_date =
'2007-06-02 08:00'\G;
*************************** 1. row ***************************
id: 1
select_type: SIMPLE
table: case_sessions
type: ref
possible_keys: start_date_index
key: start_date_index
key_len: 8
ref: const
rows: 1
Extra:
| From: Luuk |
Date: Friday, October 19, 2007
|
<spamtheaussie@xxxxxxxxxxx> schreef in bericht
Hopefully this is slightly easier to read:
mysql> explain SELECT * FROM case_sessions where start_date <
'2007-06-02 08:00'\G;
*************************** 1. row ***************************
id: 1
select_type: SIMPLE
table: case_sessions
type: ALL
possible_keys: start_date_index
key: NULL
key_len: NULL
ref: NULL
rows: 412863
Extra: Using where
mysql> explain SELECT * FROM case_sessions where start_date =
'2007-06-02 08:00'\G;
*************************** 1. row ***************************
id: 1
select_type: SIMPLE
table: case_sessions
type: ref
possible_keys: start_date_index
key: start_date_index
key_len: 8
ref: const
rows: 1
Extra:
using an index, or not, has somethings to do with optimizations...
I have a table wit a datetime
field in it. the table contains
data for every
day since july 2007;
when I do "select * from table where dateTime
< '2007-10-19';" the index
with this field is used.
and when I do "select * from table where dateTime >'2007-10-19'; the index
isn't used.
but, last but not least, there's a chapter on this
subject in the manual
(
http://dev.mysql.com/doc/refman/5.0/en/optimization.html) which i haven't
read......
| From: ZeldorBlat |
Date: Friday, October 19, 2007
|
wrote in message:
Hopefully this is slightly easier to read:
mysql> explain SELECT * FROM case_sessions where start_date <
'2007-06-02 08:00'\G;
*************************** 1. row ***************************
id: 1
select_type: SIMPLE
table: case_sessions
type: ALL
possible_keys: start_date_index
key: NULL
key_len: NULL
ref: NULL
rows: 412863
Extra: Using where
mysql> explain SELECT * FROM case_sessions where start_date =
'2007-06-02 08:00'\G;
*************************** 1. row ***************************
id: 1
select_type: SIMPLE
table: case_sessions
type: ref
possible_keys: start_date_index
key: start_date_index
key_len: 8
ref: const
rows: 1
Extra:
What does your data look like? If most of the rows are less than
2007-06-02 08:00 (which appears to be the case) it's probably faster
to scan the table than use an index (fewer
disk seeks). The optimizer
is smarter than you think...
| From: Luuk |
Date: Friday, October 19, 2007
|
"Luuk"
<luuk@xxxxxxxxxxx> schreef in bericht
<spamtheaussie@xxxxxxxxxxx> schreef in bericht
Hopefully this is slightly easier to read:
mysql> explain SELECT * FROM case_sessions where start_date <
'2007-06-02 08:00'\G;
*************************** 1. row ***************************
id: 1
select_type: SIMPLE
table: case_sessions
type: ALL
possible_keys: start_date_index
key: NULL
key_len: NULL
ref: NULL
rows: 412863
Extra: Using where
mysql> explain SELECT * FROM case_sessions where start_date =
'2007-06-02 08:00'\G;
*************************** 1. row ***************************
id: 1
select_type: SIMPLE
table: case_sessions
type: ref
possible_keys: start_date_index
key: start_date_index
key_len: 8
ref: const
rows: 1
Extra:
using an index, or not, has somethings to do with optimizations...
I have a table wit a datetime field in it. the table contains data for
every day since july 2007;
when I do "select * from table where dateTime < '2007-10-19';" the index
with this field is used.
and when I do "select * from table where dateTime >'2007-10-19'; the index
isn't used.
but, last but not least, there's a chapter on this subject in the manual
(http://dev.mysql.com/doc/refman/5.0/en/optimization.html) which I have
not read......
hmmmm, is was missing something, so the above isn't true,
damn, I should have read the pages the
link refers too...
the ebove should be:
- when I do "select * from table where dateTime
< '2007-10-19';" the index
with this field isn't used. (but the PRIMARY key is used)
- and when I do "select * from table where dateTime >'2007-10-19'; the index
IS used.
| From: Dirk McCormick |
Date: Friday, October 19, 2007
|
wrote in message:
wrote in message:
> Hopefully this is slightly easier to read:
> mysql> explain SELECT * FROM case_sessions where start_date
<
> '2007-06-02 08:00'\G;
> *************************** 1. row ***************************
> id: 1
> select_type: SIMPLE
> table: case_sessions
> type: ALL
> possible_keys: start_date_index
> key: NULL
> key_len: NULL
> ref: NULL
> rows: 412863
> Extra: Using where
> mysql> explain SELECT * FROM case_sessions where start_date =
> '2007-06-02 08:00'\G;
> *************************** 1. row ***************************
> id: 1
> select_type: SIMPLE
> table: case_sessions
> type: ref
> possible_keys: start_date_index
> key: start_date_index
> key_len: 8
> ref: const
> rows: 1
> Extra:
What does your data look like? If most of the rows are less than
2007-06-02 08:00 (which appears to be the case) it's probably faster
to scan the table than use an index (fewer disk seeks). The optimizer
is smarter than you think...
You're right it depends on the date. Interesting.
The
range of start_date is from 2002-07-02 to the present, with
gradually increasing density from then until the present.
It stops using the index when the date is greater than 2006-04-01.
So it makes sense that if it's retrieving all the row data anyway it
may as well just scan the table.
Unfortunately my actual use case is this:
SELECT * FROM case_sessions where start_date
< '2006-04-01' and
end_date > '2006-04-01'
I created an end_date_index and a combined index on start_date and
end_date called date_index, but in this case I guess it does not
realise that there are only going to be a handful of rows that will
match the WHERE criteria, so it still tries to
a) use the start_date index instead of using the
composite date_index
b) ignore all indexes after '2006-04-01'
I guess I will just force it to use my composite index.
Thanks a lot for your help!
Rob
| From: spamtheaussie |
Date: Friday, October 19, 2007
|
wrote in message:
wrote in message:
> Hopefully this is slightly easier to read:
> mysql> explain SELECT * FROM case_sessions where start_date <
> '2007-06-02 08:00'\G;
> *************************** 1. row ***************************
> id: 1
> select_type: SIMPLE
> table: case_sessions
> type: ALL
> possible_keys: start_date_index
> key: NULL
> key_len: NULL
> ref: NULL
> rows: 412863
> Extra: Using where
> mysql> explain SELECT * FROM case_sessions where start_date =
> '2007-06-02 08:00'\G;
> *************************** 1. row ***************************
> id: 1
> select_type: SIMPLE
> table: case_sessions
> type: ref
> possible_keys: start_date_index
> key: start_date_index
> key_len: 8
> ref: const
> rows: 1
> Extra:
What does your data look like? If most of the rows are less than
2007-06-02 08:00 (which appears to be the case) it's probably faster
to scan the table than use an index (fewer disk seeks). The optimizer
is smarter than you think...
You're right it depends on the date. Interesting.
The range of start_date is from 2002-07-02 to the present, with
gradually increasing density from then until the present.
It stops using the index when the date is greater than 2006-04-01.
So it makes sense that if it's retrieving all the row data anyway it
may as well just scan the table.
Unfortunately my actual use case is this:
SELECT * FROM case_sessions where start_date < '2006-04-01' and
end_date > '2006-04-01'
I created an end_date_index and a combined index on start_date and
end_date called date_index, but in this case I guess it does not
realise that there are only going to be a handful of rows that will
match the WHERE criteria, so it still tries to
a) use the start_date index instead of using the composite date_index
b) ignore all indexes after '2006-04-01'
I guess I will just force it to use my composite index.
Thanks a lot for your help!
Rob
| From: Dirk McCormick |
Date: Saturday, October 20, 2007
|
wrote in message:
wrote in message:
> Hopefully this is slightly easier to read:
> mysql> explain SELECT * FROM case_sessions where start_date <
> '2007-06-02 08:00'\G;
> *************************** 1. row ***************************
> id: 1
> select_type: SIMPLE
> table: case_sessions
> type: ALL
> possible_keys: start_date_index
> key: NULL
> key_len: NULL
> ref: NULL
> rows: 412863
> Extra: Using where
> mysql> explain SELECT * FROM case_sessions where start_date =
> '2007-06-02 08:00'\G;
> *************************** 1. row ***************************
> id: 1
> select_type: SIMPLE
> table: case_sessions
> type: ref
> possible_keys: start_date_index
> key: start_date_index
> key_len: 8
> ref: const
> rows: 1
> Extra:
What does your data look like? If most of the rows are less than
2007-06-02 08:00 (which appears to be the case) it's probably faster
to scan the table than use an index (fewer disk seeks). The optimizer
is smarter than you think...
You're right it depends on the date. Interesting.
The range of start_date is from 2002-07-02 to the present, with
gradually increasing density from then until the present.
It stops using the index when the date is greater than 2006-04-01.
So it makes sense that if it's retrieving all the row data anyway it
may as well just scan the table.
Unfortunately my actual use case is this:
SELECT * FROM case_sessions where start_date < '2006-04-01' and
end_date > '2006-04-01'
I created an end_date_index and a combined index on start_date and
end_date called date_index, but in this case I guess it does not
realise that there are only going to be a handful of rows that will
match the WHERE criteria, so it still tries to
a) use the start_date index instead of using the composite date_index
b) ignore all indexes after '2006-04-01'
I guess I will just force it to use my composite index.
Thanks a lot for your help!
Rob
Next Message: restore one database from full backup
Blogs related to Indexes with [code]< or >[/code] comparisons
database status loading
... excel database vbrun300.dll database vbscript database vcard database vds database vegas map zip
code list database vehicle options free online database vendor database vendor
comparison database vendor market share database vendors
...
wordperfect calendars
5.1
codes wordperfect variables wordperfect version wordperfect version 10 wordperfect version 11 wordperfect version 12 wordperfect version 9 wordperfect version history wordperfect versions wordperfect viewer wordperfect viewer corel
...
cms asp
... hjemmesidewebdesign.dk cms cms hjemmeside hjemmesidewebdesign.dk cms cnc router for sale cms cnc routers cms cnc wood router for sale cms coast gold site web cms
code cms
code cpt medicare cms
code life safety training cms
codes cms
...
php mysql client api version
... php
mysql club membership database php
mysql cms php
mysql cms open source php
mysql cms tutorial php
mysql code php
mysql code examples php
mysql code generator php
mysql code sample php
mysql code samples php
mysql codes php
mysql ...
sql between date
... password sql changing column sql char sql char function sql char functions sql char varchar sql character sql character
code sql character
codes sql character count sql character escape sql character function sql character functions
...
Area Code Listings
A full listing of all area
codes in North America sorted by State is available at: http://www.areacodelookup-usa.com/area-
code-listings-by-state.html And the complete
index of area
codes . Area
Code Listings livonia area
code li yi
...