Thank you very much :)
I gave it a try and found out, that actually your last comment about the index was the thing I needed. Sometimes it's just some small improvement which is needed... ;)
Comparison:
Old Query:
SELECT /* [things omitted] */ articles.blogpost_id, articles.id AS articleid
FROM blogposts
JOIN articles ON articles.blogpost_id = blogposts.id
WHERE blogposts.deleted = 0
AND blogposts.title LIKE '%{de}%'
AND blogposts.visible = 1
AND blogposts.date_published <= NOW()
ORDER BY blogposts.date_created DESC
LIMIT 0 , 50
New query:
SELECT /* [things omitted] */ articles.blogpost_id, articles.id AS articleid
FROM
(
SELECT B.*
FROM
(
SELECT id FROM blogposts
WHERE date_published <= NOW()
AND deleted = 0 AND visible = 1
AND title LIKE '%{de}%'
ORDER BY date_created DESC
LIMIT 0,50
) A
INNER JOIN blogposts B USING (id)
) blogposts
INNER JOIN articles
ON blogposts.id = articles.blogpost_id
Now without the index mentioned:
Old Explain Result:
id select_type table type possible_keys key key_len ref rows Extra
1 SIMPLE articles ALL blogpost_id NULL NULL NULL 6915 Using temporary; Using filesort
1 SIMPLE blogposts eq_ref PRIMARY PRIMARY 4 articles.blogpost_id 1 Using where
New Explain Result:
id select_type table type possible_keys key key_len ref rows Extra
1 PRIMARY <derived2> ALL NULL NULL NULL NULL 50
1 PRIMARY articles ref blogposts_id blogposts_id 4 blogposts.id 1
2 DERIVED <derived3> ALL NULL NULL NULL NULL 50
2 DERIVED B eq_ref PRIMARY PRIMARY 4 A.id 1
3 DERIVED blogposts ALL deleted,visible,date_published deleted 1 28198 Using filesort
Explain result with index on old query:
id select_type table type possible_keys key key_len ref rows Extra
1 SIMPLE blogposts ref PRIMARY,deleted,visible,date_published,deleted_vis... deleted_visible_date_created 2 const,const 27771 Using where
1 SIMPLE articles ref blogposts_id blogposts_id 4 db.blogposts.id 1
Explain result with index on new query:
id select_type table type possible_keys key key_len ref rows Extra
1 PRIMARY <derived2> ALL NULL NULL NULL NULL 50
1 PRIMARY articles ref blogposts_id blogposts_id 4 blogposts.id 1
2 DERIVED <derived3> ALL NULL NULL NULL NULL 50
2 DERIVED B eq_ref PRIMARY PRIMARY 4 A.id 1
3 DERIVED blogposts ref deleted,visible,date_published,deleted_visible_dat... deleted_visible_date_created 2 27771 Using where
Speed on old query without/with index:
0.1835/0.0037
Speed on new query without/with index:
0.1883/0.0035
So because of the just marginal differency between the old/new query I prefer to still use the old query but with the index. But I will keep this in mind, as if someday the old query is too slow :)
What would be interesting for me is to know, how you got the idea to set the index like this?
I think as I published this question, I tried also with deleted_visible but instead of date_created I used date_published (as it's in the where-clause)...
Thanks :)
UPDATE by RolandoMySQLDBA 2011-05-19 13:03
What gave it away to me was the WHERE and ORDER BY clauses.
In the WHERE clause, deleted (0) and visible (1) are static values. In the ORDER BY clause, the date_created was like a moving target among all the rows with deleted=0 and visible=1. So, I placed the static variables in front of the index first, then the moving target last in the index. Usually, that is part of the underlying principle of refactoring SQL statements. You need indexes that will support your WHERE, GROUP BY, and ORDER clauses.