I want to know which one is better indexed in the below 2 examples:
Below query has index(category_id,date_created )
EXPLAIN SELECT postid
FROM posts
WHERE category_id =53
AND approval = ‘yes’
ORDER BY date_created DESC
LIMIT 0 , 10
id select_type table type possible_keys key key_len ref rows Extra
1 SIMPLE posts index category_id date_created 9 NULL 199 Using where
Below query has one index(date_created ), removed index (category_id)
EXPLAIN SELECT postid
FROM posts
WHERE category_id =53
AND approval = ‘yes’
ORDER BY date_created DESC
LIMIT 0 , 10
id select_type table type possible_keys key key_len ref rows Extra
1 SIMPLE posts index NULL date_created 9 NULL 10 Using where
===============
Which one among the two is better indexing ? The difference is rows_examined. Should I be concerned about query execution time as its not much differnce in above two or the rows_examined?
The first query with indexed on date and category is the best indexed query and gives the results faster than the other query which is indexed only on date created.The second query gives results irrespective of category.
Could you give the output of ‘CREATE TABLE posts’?
As you can see from key_len in your first post, both use exactly the same number of bytes from the index.
In your last post, I can’t see why it says filesort in your explain output.
My guess is that you get a filesort due to that you have forgotten to create the index(category_id, date_created).
But I suggest that you should create the index(category_id, approval, date_created) since it looks like you always want to have approval=‘yes’ in your queries.
That way all conditions for the query are met by the index without having to fetch the approval data from the table.