When we optimize clients' SQL queries I pretty often see a queries with SQL_CALC_FOUND_ROWS option used. Many people think, that it is faster to use this option than run two separate queries: one - to get a result set, another - to count total number of rows. In this post I'll try to check, is this true or not and when it is better to run two separate queries.
For my tests I've created following simple table:
PLAIN TEXT SQL:
- CREATE TABLE `count_test` (
- `a` int(10) NOT NULL AUTO_INCREMENT,
- `b` int(10) NOT NULL,
- `c` int(10) NOT NULL,
- `d` varchar(32) NOT NULL,
- PRIMARY KEY (`a`),
- KEY `bc` (`b`,`c`)
- ) ENGINE=MyISAM
Test data has been created with following script (which creates 10M records):
PLAIN TEXT PHP:
- …