I am adding index to mysql queries and below is the query.
SELECT *
FROM
JOBSEEKER
WHERE
JOBSEEKER.VCEMAIL='godavary@gmail.com'
AND JOBSEEKER.ITJOBSEEKERSTATUS<>5 LIMIT 1
Using EXPLAIN:
*************************** 1. row ***************************
id: 1
select_type: SIMPLE
table: JOBSEEKER
type: ALL
possible_keys: NULL
key: NULL
key_len: NULL
ref: NULL
rows: 121702
Extra: Using where
1 row in set (0.00 sec)
I added an index to the table and got the following results:
*************************** 1. row ***************************
id: 1
select_type: SIMPLE
table: JOBSEEKER
type: ref
possible_keys: EMAIL_I
key: EMAIL_I
key_len: 103
ref: const
rows: 1
Extra: Using where
1 row in set (0.00 sec)
I see that the key length is very high here, but previously it was nothing/NULL. After indexing query execution time is improved.
Is this fine or should I index ITJOBSEEKERSTATUS as well?
The
key_lenproperty is (as you know) the length of the key used in the index. TheNULLvalue from the original table means that there was no key at all, and therefore no index. In this case, the ‘high’ key value is indicative of the number of bytes it take to index, which will be higher using a string (such as an email address) than with something like an integer ID, for example. Despite the ‘long’ key length, using an index helps MySQL find the correct rows more quickly, which is evidenced by your improved execution time.Also, you can see that your query went from
ALL(full table scan – slow and usually want to avoid) toref(better – in this case you are matching just those rows in yourWHEREcondition in this case).A true expert will be able to articulate this better, but the general idea is that it is good 🙂