sql 优化
explain SELECT PostTime FROM a WHERE BrokerId='781132' ORDER BY PostTime DESC LIMIT 1;
+----+-------------+-----------------------+------+----------------+----------+---------+-------+------+-----------------------------+
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
+----+-------------+-----------------------+------+----------------+----------+---------+-------+------+-----------------------------+
| 1 | SIMPLE | a | ref | BrokerId,idx_3 | BrokerId | 5 | const | 1 | Using where; Using filesort |
+----+-------------+-----------------------+------+----------------+----------+---------+-------+------+-----------------------------+
show index from a;
+-----------------------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------
+
| Table | Non_unique | Key_name | Seq_in_index | Column_name | Collation | Cardinality | Sub_part | Packed | Null | Index_type | Comment
|
+-----------------------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------
+
| a | 0 | PRIMARY | 1 | DaId | A | 1376213 | NULL | NULL | | BTREE |
|
| a| 1 | BrokerId | 1 | BrokerId | A | 688106 | NULL | NULL | YES | BTREE |
|
| a| 1 | idx_1 | 1 | CityId | A | 22 | NULL | NULL | YES | BTREE |
|
| a| 1 | idx_1 | 2 | IsPass | A | 22 | NULL | NULL | YES | BTREE |
|
| a| 1 | idx_1 | 3 | BrokerId | A | 1376213 | NULL | NULL | YES | BTREE |
|
|a| 1 | idx_1 | 4 | AreaCode | A | 1376213 | NULL | NULL | YES | BTREE |
|
| a| 1 | idx_3 | 1 | BrokerId | A | 1376213 | NULL | NULL | YES | BTREE |
|
| a| 1 | idx_3 | 2 | AreaCode | A | 1376213 | NULL | NULL | YES | BTREE |
|
| a| 1 | idx_3 | 3 | PostTime | A | 1376213 | NULL | NULL | YES | BTREE |
|
| a| 1 | IsPass | 1 | IsPass | A | 22 | NULL | NULL | YES | BTREE |
|
+-----------------------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+------
给brokerid,posttime建个联合索引?还能优化么?
------解决方案--------------------给brokerid,posttime建个联合索引
已经是最优的了
------解决方案--------------------没什么可优化的了。即使创建 index on (brokerid,posttime) 也未必有多大作用, 你的 BrokerId 平均相同也就两条。