My understanding was that indexing improve data retrieval speed.
Bit confused on this occasion and want to seek some advise. I have a table with millions of records. It takes around 90 seconds to fetch data using the select query and the where clause - example:
SELECT EMPID, FNAME, LNAME, ADDRESS, PHONE
FROM TBLEMPLOYEES
WHERE LNAME='ABBA'
When I create a non clustered index on this column (LNAME), and then run the same select statement, the query takes 300 seconds when executed for the first time. On subsequent runs it takes around 10 seconds. I believe that this improvement upon second execution is due to concept of caching?
When I re-run the same select statement but this time with a different search keyword - example:
SELECT EMPID, FNAME, LNAME, ADDRESS, PHONE
FROM TBLEMPLOYEES
WHERE LNAME='FERNS'
The query again takes around 300 seconds. The next time I execute the same query it takes around 10 seconds.
So is the benefit from indexing guaranteed or are they only applicable upon subsequent execution of the respective queries? Bit confused here as to why only subsequent executions of the query result in quick data retrieval.