8

I'm using explain to test these queries. The col type is DATE

this uses index:

explain SELECT events.* FROM events WHERE events.date = '2010-06-11' 

this doesnt

explain SELECT events.* FROM events WHERE events.date >= '2010-06-11' 

index as follows (phpmyadmin)

Action Keyname Type Unique Packed Field Cardinality Collation Null Comment Edit Drop PRIMARY BTREE Yes No event_id 18 A Edit Drop date BTREE No No date 0 A 

i notice cardinality is 0, though there are some rows with the same date..

5
  • Cardinality 0 is a bit odd. Try running ANALYZE TABLE events and then try again? Commented Jun 18, 2010 at 11:13
  • and does the output of EXPLAIN still state that the index has cardinality 0? Commented Jun 18, 2010 at 11:23
  • If you actually run the query, does it give the output you would expect? Commented Jun 18, 2010 at 11:29
  • yep, still cardin of 0, yep, gets correct results Commented Jun 18, 2010 at 11:39
  • doesn't cardinality 0 => all values are the same? Commented May 18, 2020 at 6:37

1 Answer 1

11

If MySQL doesn't use the index, it has seen your query, and estimated that a table scan would probably be faster then using the index (in terms of IO / disk operations required probably). You can use a FORCE INDEX and check whether this query will actually be faster using the index or not.

SELECT events.* FROM events FORCE INDEX (date) WHERE events.date >= '2010-06-11'; 
Sign up to request clarification or add additional context in comments.

1 Comment

yes i think you're right, as its a test table with only 20rows (will be tens of thousands). how do i implement FORCE (to test)? this doesnt work: explain SELECT events.* FROM events WHERE events.date >= '2010-06-11' FORCE INDEX(date)

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.