Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it OK to index all the fields in this mysql query?

I have this mysql query and I am not sure what are the implications of indexing all the fields in the query . I mean is it OK to index all the fields in the CASE statement, Join Statement and Where Statement? Are there any performance implications of indexing fields?

SELECT roots.id as root_id, root_words.*,
CASE 
WHEN root_words.title LIKE '%text%' THEN 1
WHEN root_words.unsigned_title LIKE '%normalised_text%' THEN 2
WHEN unsigned_source LIKE '%normalised_text%' THEN 3
WHEN roots.root LIKE '%text%' THEN 4
END as priorities
FROM  roots INNER JOIN root_words ON roots.id=root_words.root_id
WHERE (root_words.unsigned_title LIKE '%normalised_text%') OR (root_words.title LIKE '%text%')
OR (unsigned_source LIKE '%normalised_text."%') OR (roots.root LIKE '%text%') ORDER by priorities

Also, How can I further improve the speed of the query above?

Thanks!

like image 498
perpetual_dream Avatar asked Dec 28 '22 11:12

perpetual_dream


1 Answers

  1. You index columns in tables, not queries.

  2. None of the search criteria you've specified will be able to make use of indexes (since the search terms begin with a wild card).

  3. You should make sure that the id column is indexed, to speed the JOIN. (Presumably, it's already indexed as a PRIMARY KEY in one table and a FOREIGN KEY in the other).

To speed up this query you will need to use full text search. Adding indexes will not speed up this particular query and will cost you time on INSERTs, UPDATEs, and DELETEs.

like image 109
Larry Lustig Avatar answered Jan 11 '23 00:01

Larry Lustig