Does the index in the column you select add to the query speed?

I have a fairly common setup, I have a table of tags and pairs of entries. The query is slow, so adding an index to the column of the tag I'm querying helps speed it up.

tag | site
123 | 456
789 | 101

My question is whether it is useful to add an index for both of these columns as a single index. It will never be selected by the site, so is there any use for it? I also cannot guarantee that each pairing is unique without making any changes, but if I did, would it help with performance?

A typical query might look like this: <pre> the SELECT site, tagthe FROM sitetagsWHERE tag= '123' OR tag= '789'

+4
source share
2 answers

If you always search by tag, you only need to index the column tag.

Adding a column to the index when not in use introduces unnecessary overhead when inserting or updating a record, as well as consuming more storage.

But a composite index ( tag, site) can provide additional optimization, since MySQL should only read the index to satisfy your query ( EXPLAINusually marks this optimization as using index).

If your operation is mostly read, not written, then using a composite index may be a bad idea.

, tag , , , .

EXPLAIN.

+5

, , , , .

, , , .

, . , , , (, ) .

. :

WHERE tag IN('123','789')
0

Source: https://habr.com/ru/post/1685386/


All Articles