免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
12下一页
最近访问板块 发新帖
查看: 5190 | 回复: 10
打印 上一主题 下一主题

mysql范围查询不识别分区?? [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2010-02-05 11:13 |只看该作者 |倒序浏览
各位,mysql做范围查询的时候怎么不识别分区啊,一直在做全表扫描!!

e.g: select count(*)  from time >='2010-01-21' and time <'2010-01-25' and id='5';

无论我是按照id或者time分区,也无论我是range 分区或者是hash 分区,只要做这种范围遍历,就会一直扫描全表。

但是如果这样查询:
   
    select count(*)  from time ='2010-01-21’;

就能够使用分区。。。。。


有碰到过这种情况的吗? 有什么好的解决办法没有?

论坛徽章:
0
2 [报告]
发表于 2010-02-05 13:02 |只看该作者
select count(*)  from id='5' and ...

论坛徽章:
0
3 [报告]
发表于 2010-02-05 13:02 |只看该作者
字段类型有没问题哦…… 不太懂……帮顶……













__________________________________
九一苹果家族  电脑硬件知识圈  家用电器圈  单游网  手中时尚数码圈  掌上有你手机  淘梦之地  全能手机  大杂堂  Doc文档中心  淘贼网

论坛徽章:
0
4 [报告]
发表于 2010-02-05 13:57 |只看该作者
回复 2# justlooks


    还是全表遍历,只要是>= < 这种,就会遍历全表

论坛徽章:
0
5 [报告]
发表于 2010-02-05 14:40 |只看该作者
分区功能不是很稳定,还有些bug,不知你的表是什么类型的,有没有使用索引?

论坛徽章:
0
6 [报告]
发表于 2010-02-05 15:41 |只看该作者
我试了下是可以的,版本是5.1.42
mysql [localhost] {msandbox} (test) > create table t_partition(
    -> id int(5),
    -> time timestamp,
    -> name char(5)
    -> )
    -> partition by range (id) (
    ->    partition p0 values less than (3),
    ->    partition p1 values less than (6),
    ->    partition p2 values less than MAXVALUE
    -> );
Query OK, 0 rows affected (0.06 sec)

mysql [localhost] {msandbox} (test) > select * from t_partition;
+------+---------------------+------+
| id   | time                | name |
+------+---------------------+------+
|    1 | 2010-02-05 15:38:39 | aa   |
|    2 | 2010-02-05 15:38:39 | bb   |
|    3 | 2010-02-05 15:38:39 | cc   |
|    4 | 2010-02-05 15:38:39 | dd   |
|    5 | 2010-02-05 15:38:39 | ee   |
|    6 | 2010-02-05 15:38:39 | ff   |
|    7 | 2010-02-05 15:38:39 | gg   |
|    8 | 2010-02-05 15:38:39 | hh   |
+------+---------------------+------+
8 rows in set (0.00 sec)

mysql [localhost] {msandbox} (test) > explain partitions  select * from t_partition where id=1 and time< now();
+----+-------------+-------------+------------+------+---------------+------+---------+------+------+-------------+
| id | select_type | table       | partitions | type | possible_keys | key  | key_len | ref  | rows | Extra       |
+----+-------------+-------------+------------+------+---------------+------+---------+------+------+-------------+
|  1 | SIMPLE      | t_partition | p0         | ALL  | NULL          | NULL | NULL    | NULL |    8 | Using where |
+----+-------------+-------------+------------+------+---------------+------+---------+------+------+-------------+
1 row in set (0.00 sec)

mysql [localhost] {msandbox} (test) > explain partitions  select * from t_partition where time< now();
+----+-------------+-------------+------------+------+---------------+------+---------+------+------+-------------+
| id | select_type | table       | partitions | type | possible_keys | key  | key_len | ref  | rows | Extra       |
+----+-------------+-------------+------------+------+---------------+------+---------+------+------+-------------+
|  1 | SIMPLE      | t_partition | p0,p1,p2   | ALL  | NULL          | NULL | NULL    | NULL |    8 | Using where |
+----+-------------+-------------+------------+------+---------------+------+---------+------+------+-------------+

论坛徽章:
0
7 [报告]
发表于 2010-02-05 16:23 |只看该作者
回复 6# justlooks


    你的mysql是什么版本啊
  
   我的是5.1.34社区版

论坛徽章:
0
8 [报告]
发表于 2010-02-05 16:24 |只看该作者
回复 5# youcz


    myisam
   
    有索引

论坛徽章:
0
9 [报告]
发表于 2010-02-05 16:31 |只看该作者
回复 6# justlooks


    你帮查询下id >='1' and id <'6'   看看,谢谢

论坛徽章:
0
10 [报告]
发表于 2010-02-05 16:59 |只看该作者
因为6在第3个分区你如果写 <6还是会找3个分区的

要写 〈=5 才可以
mysql [localhost] {msandbox} (test) > explain partitions select * from t_partition where id>=1 and id <=5
    -> ;
+----+-------------+-------------+------------+------+---------------+------+---------+------+------+-------------+
| id | select_type | table       | partitions | type | possible_keys | key  | key_len | ref  | rows | Extra       |
+----+-------------+-------------+------------+------+---------------+------+---------+------+------+-------------+
|  1 | SIMPLE      | t_partition | p0,p1      | ALL  | NULL          | NULL | NULL    | NULL |    8 | Using where |
+----+-------------+-------------+------------+------+---------------+------+---------+------+------+-------------+
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP