简体   繁体   中英

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!

  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.

警告:索引会加快检索时间,但会导致插入和更新运行得更慢。

To answer the implications of indexing every field, there is a performance hit when using indexes whenever the data that is indexed is modified, either through inserts, updates, or deletes. This is because SQL needs to maintain the index. It's a balance between how often the data is read versus how often it is modified.

In this specific query, the only index that could possibly help would be in your JOIN clause, on the fields roots.id and root_words.root_id .

None of the checks in your WHERE clause could be indexed, because of the leading '%' . This causes SQL to scan every row in these tables for a matching value.

If you are able to remove the leading '%' , you would then benefit from indexes on these fields... if not, you should look into implementing full-text search ; but be warned, this isn't trivial.

Indexing won't help when used in conjunction with LIKE '%something%' .

It's like looking for words in a dictionary that have ae in them somewhere. The dictionary (or Index in this case) is organised based on the first letter of the word, then the second letter, etc. It has no mechanism to put all the words with ae in them close together. You still end up reading the whole dictionary from beginning to end.


Indexing the fields used in the CASE clause will likely not help you. Indexing helps by making it easy to find records in a table. The CASE clause is about processing the records you have found, not finding them in the first place.


Optimisers can also struggle with optimising multiple unrelated OR conditions such as yours. The optimiser is trying to narrow down the amount of effort to complete your query, but that's hard to do when unrelated conditions could make a record acceptable.


All in all your query would benefit from indexes on roots(root_id) and/or roots(id) , but not much else.

If you were to index additional fields though, the two main costs are:
- Increased write time (insert, update or delete) due to additional indexes to write to
- Increased space taken up on the disk

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM