A simple data table is created:
CREATE TABLE IF NOT EXISTS `table_1` ( id INT NOT NULL AUTO_INCREMENT, user_id INT( 33 ), user_name VARCHAR( 255 ), PRIMARY KEY ( `id` ) ) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci As you can see from this example, unique keys are created in the id column (due to AUTO_INCREMENT ). The user_id field also contains the unique values of user identifiers generated by the script (using mt_rand) during registration in the personal account.
For sampling I make the following request:
SELECT `user_name` FROM `table_1` WHERE `user_id`=28572 I thought about using indexes in my data table to speed up sampling. The documentation says:
The presence of an index can significantly increase the speed of execution of some queries and reduce the time to search for the necessary data due to their physical or logical ordering.
Questions:
Do I need to create indexes for the
user_idfield to speed up the sampling of millions of records, if all the identifier values in this field areuser_idunique?If you still need to, then which index should be created: cluster or non-cluster (I do not quite understand the differences between them)?
The
WHEREcondition (in my example above) makes the DBMS iterate over all the records in the table? Or does the DBMS immediately refer specifically to only those records that satisfy the searchuser_id = 28572(user_id = 28572), without affecting the rest of the records?
idif you already have another unique key? - Petr AbdulinidDESC. - StasHappyname_userFROMtable_1ORDER BYidDESC LIMIT 0, 10 - StasHappyuser_idandid. - Yura Ivanov