I'm trying to show that when selecting from a table with an appropriate index the number of rows has almost no effect on the query latency. So I created a table:
CREATE TABLE `perf_test` (
`first` int NOT NULL,
`second` int NOT NULL,
`third` int NOT NULL,
`fourth` int NOT NULL,
`fifth` int NOT NULL,
KEY `yyy` (`first`)
)
I ran two tests: one where the table has 1M rows, 100 of which match the query; the other where the table has 10M rows, 100 of which match the query. I issued the query SELECT * FROM perf_test WHERE first = 1
10 times.
The average run time for 1M rows was 0.007 seconds, for 10M rows it was 0.057, so an order of magnitude greater. Why would that be?
MySQL version is 8.0.19 and I'm running this on Windows 10, using MySQL command line client.
Edit (adding info from SHOW SESSION STATUS LIKE 'Handler%';
as per @RickJames suggestion):
mysql> select count(*) from perf_test where first = 1;
+----------+
| count(*) |
+----------+
| 100 |
+----------+
1 row in set (0.00 sec)
mysql> SHOW SESSION STATUS LIKE 'Handler%';
+----------------------------+-------+
| Variable_name | Value |
+----------------------------+-------+
| Handler_commit | 19 |
| Handler_delete | 11 |
| Handler_discover | 0 |
| Handler_external_lock | 34 |
| Handler_mrr_init | 0 |
| Handler_prepare | 4 |
| Handler_read_first | 0 |
| Handler_read_key | 10 |
| Handler_read_last | 0 |
| Handler_read_next | 1044 |
| Handler_read_prev | 0 |
| Handler_read_rnd | 0 |
| Handler_read_rnd_next | 0 |
| Handler_rollback | 0 |
| Handler_savepoint | 0 |
| Handler_savepoint_rollback | 0 |
| Handler_update | 0 |
| Handler_write | 0 |
+----------------------------+-------+
18 rows in set (0.00 sec)
mysql> select count(*) from perf_test_10m where first = 1;
+----------+
| count(*) |
+----------+
| 100 |
+----------+
1 row in set (0.00 sec)
mysql> SHOW SESSION STATUS LIKE 'Handler%';
+----------------------------+-------+
| Variable_name | Value |
+----------------------------+-------+
| Handler_commit | 20 |
| Handler_delete | 11 |
| Handler_discover | 0 |
| Handler_external_lock | 36 |
| Handler_mrr_init | 0 |
| Handler_prepare | 4 |
| Handler_read_first | 0 |
| Handler_read_key | 11 |
| Handler_read_last | 0 |
| Handler_read_next | 1144 |
| Handler_read_prev | 0 |
| Handler_read_rnd | 0 |
| Handler_read_rnd_next | 0 |
| Handler_rollback | 0 |
| Handler_savepoint | 0 |
| Handler_savepoint_rollback | 0 |
| Handler_update | 0 |
| Handler_write | 0 |
+----------------------------+-------+
18 rows in set (0.00 sec)
select first from ...
you're likely to see less time increase as it can take the data directly from the index. I'd expect the extra time for theselect * ...
would not increase very much with more rows in the table.