Skip to content

Instantly share code, notes, and snippets.

@cwhite92
Created August 7, 2016 20:33
Show Gist options
  • Save cwhite92/a42f323aad4db8be3cc6e000f9e784c0 to your computer and use it in GitHub Desktop.
Save cwhite92/a42f323aad4db8be3cc6e000f9e784c0 to your computer and use it in GitHub Desktop.
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