Answer the question
In order to leave comments, you need to log in
Why is a slow query obtained when using PRIMARY KEY in SELECT?
I've been fighting for a day now, I can't understand why if you use the primary key in the selection, the selection is slower.
Table structure
CREATE TABLE notifications (
_id INT(11) NOT NULL AUTO_INCREMENT,
status INT(11) DEFAULT NULL,
sender VARCHAR(255) DEFAULT NULL,
receiver VARCHAR(255) DEFAULT NULL,
subject VARCHAR(512) DEFAULT NULL,
message TEXT DEFAULT NULL,
PRIMARY KEY (_id)
)
ENGINE = INNODB
AUTO_INCREMENT = 5
CHARACTER SET utf8
COLLATE utf8_general_ci;
INSERT INTO notifications VALUES
(1, 3, '[email protected]', '[email protected]', 'Subj', '123'),
(2, 3, '[email protected]', '[email protected]', 'Subj', '123'),
(3, 3, '[email protected]', '[email protected]', 'Subj', '123'),
(4, 3, '[email protected]', '[email protected]', 'Subj', '123');
SELECT subject,_id FROM `notifications` WHERE `status` = 1; -- 0,041c [0,013c выполнение, 0,028c выборка]
SELECT subject FROM `notifications` WHERE `status` = 1; -- 0,014c [0,013c выполнение, 0,001c выборка]
SELECT subject,_id FROM `notifications` WHERE `status` = 1; -- 0,005c [0,001c выполнение, 0,004c выборка]
SELECT subject FROM `notifications` WHERE `status` = 1; -- 0,002c [0,001c выполнение, 0,001c выборка]
SELECT subject,_id FROM `notifications` WHERE `status` = 1; -- 0,004c [< 0,001c выполнение, 0,004c выборка]
SELECT subject FROM `notifications` WHERE `status` = 1; -- 0,001c [< 0,001c выполнение, 0,001c выборка]
SELECT subject,_id FROM `notifications` WHERE `status` = 1; -- 0,0001c
SELECT subject FROM `notifications` WHERE `status` = 1; -- 0,0001c
DELIMITER $$
CREATE PROCEDURE insert_test_data()
BEGIN
DECLARE i INT DEFAULT 1;
WHILE i < 10000 DO
INSERT INTO `notifications` (`status`, `sender`, `receiver`, `subject`, `message`)
SELECT `status`, `sender`, `receiver`, `subject`, `message`
FROM `notifications`
WHERE _id = 1;
SET i = i + 1;
END WHILE;
END$$
DELIMITER ;
CALL insert_test_data();
DROP PROCEDURE insert_test_data;
Answer the question
In order to leave comments, you need to log in
I have mysql Ver 14.14 Distrib 5.5.35, for debian-linux-gnu (x86_64) using readline 6.2
both queries run the same - 0.0001 sec.
Have you tried changing the order of the fields?
SELECT <b>_id</b>, subject FROM `notifications` WHERE `status` = 1;
Purely theoretically, it should not affect, but maybe it affects the Windows platform somehow? KEY `status` (`status`)
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question