Should we use the "LIMIT clause" in the following example?

There is a structure: CREATE TABLE IF NOT EXISTS `categories` ( `id` int(11) unsigned NOT NULL AUTO_INCREMENT, `parent_id` int(11) unsigned NOT NULL DEFAULT '0', `title` varchar(255) NOT NULL, PRIMARY KEY (`id`), ) ENGINE=MyISAM DEFAULT CHARSET=utf8; Query_1: SELECT * FROM `categories` WHERE `id` = 1234 Query_2: SELECT * FROM `categories` WHERE `id` = 1234 LIMIT 1 

I need to get only one line. Since we use WHERE id=1234 (finding by PRIMARY KEY), it is obvious that the row with id = 1234 is only one in the whole table. After MySQL has found the string, will the engine continue searching when using Query_1?
Thanks in advance.

+4
source share
1 answer

Take a look at this SQLFiddle: http://sqlfiddle.com/#!2/a8713/4 and especially See the execution plan .

You see that MySQL recognizes the predicate in the PRIMARY column, and therefore it does not matter if you add LIMIT 1 or not.

PS: A little more explanation. Look at the column line of the execution plan. The number is the number of columns, as the query mechanism thinks, it should check. Since the contents of the columns are unique (like a primary key), this is 1. Compare this to this: http://sqlfiddle.com/#!2/9868b/2 in the same layout, but without a primary key. Here, rows says 8. (The execution plan is explained in the German MySQL link, http://dev.mysql.com/doc/refman/5.1/en/explain.html , for some reason in English is not so detailed.)

+6
source

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