For example, there is a MySQL request in the get.php file:

SELECT `id`, `uid` FROM `tasks` WHERE `uid` = 2 AND `read` = 0; 

and there is a request in the test.php file:

 SELECT `id`, `uid` FROM `tasks` WHERE `uid` = 2; 

What index to put in this case? Compound on uid and read? Or separately on uid and read?

  • one
    @ModaL, reread your past questions and answers to them. in the course of the second circle went. - Yura Ivanov

1 answer 1

Not. Indexes are generally optional, but this is done to increase performance. The index builds a search tree, after which SELECT like yours quickly (ideally for O (log (n)), where n is the number of records)

However, INSERT work longer, because have to recalculate the index.

An index is needed such as a condition; if the condition is in two fields, then the index is in two, if there is in each separately, then in two is not needed.

  • one
    if read = 0 for almost all entries, then a composite index is not needed; an index on uid is sufficient. - Yura Ivanov