I have the following query running against a mysql database:
select value from fact_data
join entities on fact_data.entity_id=entities.id
where entities.ticker='{value}'
limit 1
fact_data is a big table with > 23 million records. If {value} exists in entities.ticker, then the query returns in under a second. But if {value} doesn’t exist, the query takes over 2 minutes to return. It is as if a table scan is being done on fact_table. Why wouldn’t mysql first do a check to see if there exists an entities.ticker of {value} and then if there isn’t, there is no need to do any additional processing.
BTW, here is the output of an EXPLAIN on the above query:
id,select_type,table,type,possible_keys,key,key_len,ref,rows,Extra
1,SIMPLE,fact_data,ALL,NULL,NULL,NULL,NULL,23414811,
1,SIMPLE,entities,eq_ref,PRIMARY,PRIMARY,4,findata.fact_data.entity_id,1,"Using where"
Add an index to the entities.ticker field.
Indexes drammatically speed up query execution time if they are part of the where clause.