Skip to content

Instantly share code, notes, and snippets.

@svartalf
Created June 6, 2012 08:23
Show Gist options
  • Save svartalf/2880639 to your computer and use it in GitHub Desktop.
Save svartalf/2880639 to your computer and use it in GitHub Desktop.
MySQL benchmark for database index on the Django' `auth_user.is_staff` field
mysql> SELECT COUNT(*) FROM auth_user;
+----------+
| COUNT(*) |
+----------+
| 48158 |
+----------+
1 row in set (0.00 sec)
mysql> EXPLAIN SELECT COUNT(*) FROM auth_user WHERE is_staff = 1;
+----+-------------+-----------+------+---------------+------+---------+------+-------+-------------+
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
+----+-------------+-----------+------+---------------+------+---------+------+-------+-------------+
| 1 | SIMPLE | auth_user | ALL | NULL | NULL | NULL | NULL | 48199 | Using where |
+----+-------------+-----------+------+---------------+------+---------+------+-------+-------------+
1 row in set (0.00 sec)
mysql> EXPLAIN SELECT COUNT(*) FROM auth_user WHERE is_staff = 0;
+----+-------------+-----------+------+---------------+------+---------+------+-------+-------------+
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
+----+-------------+-----------+------+---------------+------+---------+------+-------+-------------+
| 1 | SIMPLE | auth_user | ALL | NULL | NULL | NULL | NULL | 48199 | Using where |
+----+-------------+-----------+------+---------------+------+---------+------+-------+-------------+
mysql> ALTER TABLE auth_user ADD INDEX (is_staff);
Query OK, 0 rows affected (0.10 sec)
Records: 0 Duplicates: 0 Warnings: 0
mysql> EXPLAIN SELECT COUNT(*) FROM auth_user WHERE is_staff = 0;
+----+-------------+-----------+------+---------------+----------+---------+-------+-------+-------------+
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
+----+-------------+-----------+------+---------------+----------+---------+-------+-------+-------------+
| 1 | SIMPLE | auth_user | ref | is_staff | is_staff | 1 | const | 24099 | Using index |
+----+-------------+-----------+------+---------------+----------+---------+-------+-------+-------------+
1 row in set (0.00 sec)
mysql> EXPLAIN SELECT COUNT(*) FROM auth_user WHERE is_staff = 1;
+----+-------------+-----------+------+---------------+----------+---------+-------+------+-------------+
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
+----+-------------+-----------+------+---------------+----------+---------+-------+------+-------------+
| 1 | SIMPLE | auth_user | ref | is_staff | is_staff | 1 | const | 36 | Using index |
+----+-------------+-----------+------+---------------+----------+---------+-------+------+-------------+
1 row in set (0.00 sec)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment