一张ip表,数据量35w条左右,select ip_id, ip_start, ip_end, ip_country, ip_local, ip_province_code, ip_city_code from t_ip_address where ip_start <=3663609113 and ip_end >=3663609113#创建3个索引
create index index_start_end on t_ip_address(ip_start,ip_end)create index index_start on t_ip_address(ip_start)create index index_end on t_ip_address(ip_end)#FORCE INDEX分别使用索引EXPLAIN select * from t_ip_address FORCE INDEX(index_start_end) where ip_start <=1663609113 and ip_end >=1663609113*************************** 1. row ***************************
id: 1
select_type: SIMPLE
table: t_ip_address
type: ALL
possible_keys: i_start_end
key: NULL
key_len: NULL
ref: NULL
rows: 100993
Extra: Using where
1 row in set (0.00 sec)其他情况FORCE INDEX(index_start) 或者 FORCE INDEX(index_end) 显示同上type:all,key:null,是什么原因没用使用索引查询???
看了mysqlperformanceblog里面例子是没有问题的,为什么这张表没有效果?还请帮忙,!!!ps:本人结贴很到位,学习是目的
create index index_start_end on t_ip_address(ip_start,ip_end)create index index_start on t_ip_address(ip_start)create index index_end on t_ip_address(ip_end)#FORCE INDEX分别使用索引EXPLAIN select * from t_ip_address FORCE INDEX(index_start_end) where ip_start <=1663609113 and ip_end >=1663609113*************************** 1. row ***************************
id: 1
select_type: SIMPLE
table: t_ip_address
type: ALL
possible_keys: i_start_end
key: NULL
key_len: NULL
ref: NULL
rows: 100993
Extra: Using where
1 row in set (0.00 sec)其他情况FORCE INDEX(index_start) 或者 FORCE INDEX(index_end) 显示同上type:all,key:null,是什么原因没用使用索引查询???
看了mysqlperformanceblog里面例子是没有问题的,为什么这张表没有效果?还请帮忙,!!!ps:本人结贴很到位,学习是目的
解决方案 »
- mysql如何把user表的password字段32位MD5加密啊
- mysql数据库中记录如何对比删除
- mysql 怎么判断是一周之内的时间段?
- 关于mysql rollback
- 求助..JSP 连接 MySQL 时, DriverManager.getConnection() 返回 null...
- 如何在MySQl的Administrator中创建表??
- 为什么我的MYSQL装不了呀。
- 不小心删了一个数据库,不知能否恢复?Mysql的
- MYSQL 什么语句可以查看表中的所有索引名
- java调用数据库 先insert再select得不到数据
- 引号里嵌引号。ACMAIN_CHM,wwwwb请进
- Rowid_Mysql 环境取数,通过什么方式缔造一个rowid之类的东西呢?
delete from t_ip_address where ip_id>100000
按照1楼的知识,显示如下:mysql> show index from t_ip_address;
+--------------+------------+-----------------+--------------+-------------+----
-------+-------------+----------+--------+------+------------+---------+
| Table | Non_unique | Key_name | Seq_in_index | Column_name | Col
lation | Cardinality | Sub_part | Packed | Null | Index_type | Comment |
+--------------+------------+-----------------+--------------+-------------+----
-------+-------------+----------+--------+------+------------+---------+
| t_ip_address | 0 | PRIMARY | 1 | ip_id | A
| 98638 | NULL | NULL | | BTREE | |
| t_ip_address | 1 | index_start | 1 | ip_start | A
| 98638 | NULL | NULL | YES | BTREE | |
| t_ip_address | 1 | index_end | 1 | ip_end | A
| 98638 | NULL | NULL | YES | BTREE | |
| t_ip_address | 1 | index_start_end | 1 | ip_start | A
| 98638 | NULL | NULL | YES | BTREE | |
| t_ip_address | 1 | index_start_end | 2 | ip_end | A
| 98638 | NULL | NULL | YES | BTREE | |
+--------------+------------+-----------------+--------------+-------------+----
-------+-------------+----------+--------+------+------------+---------+
5 rows in set (0.14 sec)显示有点乱
你的几个字段在表中基本上都没有重复的。mysql> show index from t_ip_address;
+--------------+------------+-----------------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+
| Table | Non_unique | Key_name | Seq_in_index | Column_name | Collation | Cardinality | Sub_part | Packed | Null | Index_type | Comment |
+--------------+------------+-----------------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+
| t_ip_address | 0 | PRIMARY | 1 | ip_id | A | 98638 | NULL | NULL | | BTREE | |
| t_ip_address | 1 | index_start | 1 | ip_start | A | 98638 | NULL | NULL | YES | BTREE | |
| t_ip_address | 1 | index_end | 1 | ip_end | A | 98638 | NULL | NULL | YES | BTREE | |
| t_ip_address | 1 | index_start_end | 1 | ip_start | A | 98638 | NULL | NULL | YES | BTREE | |
| t_ip_address | 1 | index_start_end | 2 | ip_end | A | 98638 | NULL | NULL | YES | BTREE | |
+--------------+------------+-----------------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+
5 rows in set (0.14 sec)
建议先用 analyze table t_ip_address; 让索引信息重新刷新一下。
再 show index from t_ip_address; 一下。然后
select count(*) from t_ip_address;
select count(*) from t_ip_address where ip_start <=1663609113 ;
select count(*) from t_ip_address where ip_end >=1663609113;在count(*)的结果几乎=表中总记录数的时候,mysql会使用全表扫描而不是去利用索引。