Is it good if the table has one index and a composite index in one field?

CREATE TABLE test (
id INT NOT NULL,
last_name CHAR(30) NOT NULL,
first_name CHAR(30) NOT NULL,
PRIMARY KEY (id),
KEY `first_name` (first_name),
KEY `last_name` (last_name),
KEY `last_first` (last_name,first_name)
);

Is it good if the table has one index and a composite index in one field, if I have queries like:

SELECT * FROM test WHERE first_name = 'A';
SELECT * FROM test WHERE last_name= 'A';
SELECT * FROM test WHERE last_name= 'A' AND first_name = 'A';
+4
source share
2 answers

tl; dr Answer: Check it out. The answer largely depends on your data and your requests, so we can not give a final answer that is suitable for all scenarios. Until you have tested it, do not use the optional composite index. It smells of premature optimization.

--- --- . . , .

, . I.e., " , ". .

, - , . RDBMS :

  • : ,

, MySQL. .

?

, , RDBMS :

, , , , . : . , , . , ? , .

, , . , , 90% last_name= 'A' (.. ). . (3), .. , , . , .

, 0,00001% . (1) (3), , (3) , ( ). (2) ( ), , , ( , ).

, , . , . .

, . , MySQL, ( ) .

?

:

  • , .
  • . , , , , . , .

?

, , . , :

  • , ( )
  • , .. , .
  • , .. , , .

IMHO:

. . , : " : . ( !). , "

, . . , , .

0

, , . , , , , , , / ( ), , .

.

, , .

0

All Articles