SELECT * FROM users WHERE age = 29;
SELECT * FROM users WHERE age = 29;
CREATE INDEX age ON users(age);
SELECT * FROM users WHERE age < 29;
SELECT * FROM users ORDER BY register_date;
CREATE INDEX register_date ON users(register_date);
id | name | age
1 | Den | 29
2 | Alyona | 15
3 | Putin | 89
4 | Petro | 12
age index
12
15
29
89
age index и связь с записями
12: 4
15: 2
29: 1
89: 3
SELECT * FROM users WHERE email = 'golotyuk@gmail.com';
CREATE UNIQUE INDEX email ON users(email);
SELECT * FROM users WHERE age = 29 AND gender = 'male'
CREATE INDEX age_gender ON users(age, gender);
id | name | age | gender
1 | Den | 29 | male
2 | Alyona | 15 | female
3 | Putin | 89 | tsar
4 | Petro | 12 | male
age_gender
12male
15female
29male
89tsar
SELECT * FROM users WHERE age <= 29 AND gender = 'male';
age_gender
12male
15female
29male
89tsar
SELECT * FROM users WHERE gender = 'male' ORDER BY age;
CREATE INDEX gender_age ON users(gender, age);
SELECT * FROM users WHERE gender = 'male' AND country = 'UA' ORDER BY age, register_time;
CREATE INDEX gender_country_age_register ON users(gender, country, age, register_time);
mysql> EXPLAIN SELECT * FROM users WHERE email = 'golotyuk@gmail.com';
mysql> EXPLAIN SELECT * FROM users WHERE email = 'golotyuk@gmail.com';
mysql> EXPLAIN SELECT * FROM users WHERE age = 29 AND gender = 'male';
mysql> EXPLAIN SELECT * FROM users WHERE age <= 29 AND gender = 'male';
mysql> Create index gender_age on users(gender, age);
mysql> EXPLAIN SELECT * FROM users WHERE age < 29 and gender = 'male';
SELECT * FROM users WHERE age = 29 AND gender = 'male'
mysql> select age, count(*) from users group by age;
+------+----------+
| age | count(*) |
+------+----------+
| 15 | 160 |
| 16 | 250 |
| ... |
| 76 | 210 |
| 85 | 230 |
+------+----------+
68 rows in set (0.00 sec)
mysql> select gender, count(*) from users group by gender;
+--------+----------+
| gender | count(*) |
+--------+----------+
| female | 8740 |
| male | 4500 |
+--------+----------+
2 rows in set (0.00 sec)
CREATE TABLE `users` (
`id` int(10) unsigned NOT NULL AUTO_INCREMENT,
`email` varchar(128) NOT NULL,
`name` varchar(128) NOT NULL,
PRIMARY KEY (`id`),
) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8
mysql> show table status;
+-------------------+--------+---------+------------+--------+----------------+-------------+-----------------+--------------+-----------+----------------+---------------------+-------------+------------+-----------------+----------+----------------+---------+
| Name | Engine | Version | Row_format | Rows | Avg_row_length | Data_length | Max_data_length | Index_length | Data_free | Auto_increment | Create_time | Update_time | Check_time | Collation | Checksum | Create_options | Comment |
+-------------------+--------+---------+------------+--------+----------------+-------------+-----------------+--------------+-----------+----------------+---------------------+-------------+------------+-----------------+----------+----------------+---------+
...
| users | InnoDB | 10 | Compact | 314 | 208 | 65536 | 0 | 16384 | 0 | 355 | 2014-07-11 01:12:17 | NULL | NULL | utf8_general_ci | NULL | | |
+-------------------+--------+---------+------------+--------+----------------+-------------+-----------------+--------------+-----------+----------------+---------------------+-------------+------------+-----------------+----------+----------------+---------+
18 rows in set (0.06 sec)