Multiple ADD INDEX together or separately?

Asked

Viewed 208 times

1

I wanted to know the practical difference between this, in Mysql:

ALTER TABLE `tabela` ADD INDEX `col1` (`col1`);
ALTER TABLE `tabela` ADD INDEX `col2` (`col2`);

For this:

ALTER TABLE `tabela` ADD INDEX `col1col2` (`col1`, `col2`);

Since in both cases the INDEX is affecting both columns required.

If, for example, you use:

$mysqli->query("SELECT id FROM tabela WHERE col1 = 'qualquer' AND col2 > 0");

Would there be a difference in performance in using the separate INDEX method or together? If there are both (separated and together) would have some benefit in this case, or would have to manually define the query() to use INDEX "united"?

1 answer

2


If you do multiple times the query you said:

$mysqli->query("SELECT id FROM tabela WHERE col1 = 'qualquer' AND col2 > 0");

Perhaps it would be more pertinent to do the composite index. Because it works exactly like the index of a book, imagine that if you have two information for you to find a chapter of a book for example, name and page number, soon you find that information more easily, the same principle governs the search of data in a database.

If queries based on each column separately are more frequent, it is more advantageous to create two indexes.

source: https://stackoverflow.com/questions/179085/multiple-indexes-vs-multi-column-indexes

  • The ideal is to save the two types. After 10 days of use I checked this. Simply the index joined took 3 SECONDS to complete the query. I added two Dexes alone. Puf. Lowered to 0.156 seconds. A reasonable result.

  • No doubt it would increase the processing, this type of index, because it is a string. unlike the numeral that corresponds to an integer.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.