Created
August 7, 2016 20:33
-
-
Save cwhite92/a42f323aad4db8be3cc6e000f9e784c0 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Table create query: | |
CREATE TABLE `records` ( | |
`id` int(11) unsigned NOT NULL AUTO_INCREMENT, | |
`number` int(11) DEFAULT NULL, | |
PRIMARY KEY (`id`) | |
) ENGINE=InnoDB AUTO_INCREMENT=1000002 DEFAULT CHARSET=utf8; | |
Filled the table with one million rows, with `number` being a random integer between 0 and 100. | |
Both these queries take exactly the same amount of time: | |
SELECT COUNT(*) FROM (SELECT * FROM records WHERE number > 1) AS records; | |
SELECT COUNT(*) FROM records WHERE number > 1; | |
The `EXPLAIN`s for these queries are also the same. | |
Adding an ORDER BY to the subquery adds another 2-4ms to the query time: | |
SELECT COUNT(*) FROM (SELECT * FROM records WHERE number > 1 ORDER BY number) AS records; | |
But adding an ORDER BY to the regular query doesn't add any time at all: | |
SELECT COUNT(*) FROM records WHERE number > 1 ORDER BY number; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment