例如有一个表
CREATE TABLE `content` (  `conid` bigint(20) unsigned NOT NULL AUTO_INCREMENT COMMENT '文章id',  `uid` int(20) unsigned NOT NULL COMMENT '用户id',  `contype` tinyint(4) NOT NULL COMMENT '文章类型',  `content` text NOT NULL COMMENT '文章内容',  `img` varchar(18) NOT NULL COMMENT '上传图片',  `comnum` smallint(5) unsigned NOT NULL COMMENT '文章评论数',  `sub` smallint(6) NOT NULL COMMENT '顶踩差',  `isdelete` tinyint(3) unsigned NOT NULL COMMENT '文章是否已删除',  `datatime` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '发布时间',  PRIMARY KEY (`conid`),  KEY `uid` (`uid`),  KEY `datatime` (`datatime`)) ENGINE=InnoDB  DEFAULT CHARSET=utf8 AUTO_INCREMENT=107 ;
因为  contype   只有三个取值 , 想根据  contype   的三个不同取值建立表分区不知道这样的表分区 和 根据  contype  的不同取值建立三个表  相比,哪个好?另外 表分区对性能影响大吗?  表分区不支持  外键 和 全文表索引吗?谢谢

解决方案 »

  1.   

    不支持FULLTEXT indexes(全文索引)具体目的是什么?查询加快速度?
      

  2.   


    查询语句类似: SELECT * FROM content WHERE `contype` = '1'因为有时只需要查询contype = 1的情况,有时候需要查询 contype = 2  的情况而有的时候  又不用管contype所以我考虑建立表分区那要是  直接给 contype 建立索引  不知道可不可以  代替表分区?
      

  3.   

    contype 只有3个值,索引的效率不太高
      

  4.   


    按 contype 分区后,对 where contype = @x 这类查询会比不分区效率提高而有的时候  又不用管contype
    对这类查询没有实际帮助。
      

  5.   


    索引效率不高而且表分区还不能使用全文索引(之后可能会用到全文索引)那就是说,要分别建立三个表那可以在三个表中根据 datatime 字段排序  依次显示数据么不知道  select  语句该怎么写?       这样效率高么?
      

  6.   

    SELECT * FROM content WHERE `contype` = '1'
    如果大量的操作是上述,建议分区
      

  7.   

    示例
    ALTER TABLE a PARTITION BY RANGE(id) (PARTITION p1 VALUES LESS THAN (2000),PARTITION p2 VALUES LESS THAN (4000)); 
      

  8.   

    我的语句:Alter table content add partition BY RANGE (contype)
    (
    Partition p0 values less than(2),
    Partition p1 values less than(3),
    Partition p2 values less than(4)
    );但是提示:#1064 - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'BY RANGE (contype) ( Partition p0 values less than(2), Partition p1 values le' at line 1
      

  9.   

    对不起,刚是因为第一行多了  “add”  但是去除了add之后还是提醒   #1503 - A PRIMARY KEY must include all columns in the table's partitioning function不是说  list 分区需要 主键么  难道  range分区也需要把  主键加入?mysql版本是  5.5.24
      

  10.   


    请教  那这个  以contype 为依据建立分区表的语句怎么写
      

  11.   

    示例:
    mysql> CREATE TABLE part_tab 
        ->      ( c1 int default NULL, 
        -> c2 varchar(30) default NULL, 
        -> c3 date default NULL 
        -> 
        ->      ) engine=myisam 
        ->      PARTITION BY RANGE (year(c3)) (PARTITION p0 VALUES LESS THAN (1995), 
        ->      PARTITION p1 VALUES LESS THAN (1996) , PARTITION p2 VALUES LESS THAN (1997) , 
        ->      PARTITION p3 VALUES LESS THAN (1998) , PARTITION p4 VALUES LESS THAN (1999) , 
        ->      PARTITION p5 VALUES LESS THAN (2000) , PARTITION p6 VALUES LESS THAN (2001) , 
        ->      PARTITION p7 VALUES LESS THAN (2002) , PARTITION p8 VALUES LESS THAN (2003) , 
        ->      PARTITION p9 VALUES LESS THAN (2004) , PARTITION p10 VALUES LESS THAN (2010), 
        ->      PARTITION p11 VALUES LESS THAN MAXVALUE ); 
    Query OK, 0 rows affected (0.00 sec)
      

  12.   


    意思是,如果我的表CREATE TABLE `content` (  `conid` bigint(20) unsigned NOT NULL AUTO_INCREMENT COMMENT '文章id',  `uid` int(20) unsigned NOT NULL COMMENT '用户id',  `contype` tinyint(4) NOT NULL COMMENT '文章类型',  `content` text NOT NULL COMMENT '文章内容',  PRIMARY KEY (`conid`),  KEY `uid` (`uid`),)我要以 contype  来做表分区的话就需要  去掉  conid  上的主键?
      

  13.   

    CREATE TABLE `content` (   `conid` BIGINT(20) UNSIGNED NOT NULL AUTO_INCREMENT COMMENT '文章id',   `uid` INT(20) UNSIGNED NOT NULL COMMENT '用户id',   `contype` TINYINT(4) NOT NULL COMMENT '文章类型',   `content` TEXT NOT NULL COMMENT '文章内容',   PRIMARY KEY (`conid`,`contype`),   KEY `uid` (`uid`)
    ) PARTITION BY RANGE (`contype`) (PARTITION p0 VALUES LESS THAN (1),
     PARTITION p1 VALUES LESS THAN (2),PARTITION p2 VALUES LESS THAN (3))
      

  14.   

    是的,先DROP掉,然后再创建复合主键,把contype  加入到主键。
      

  15.   

    因为以后可能要用到全文索引,所以表分区可能有点不合适所以,想把这三部分放在三个完全相同的表中但是有时候,又需要合并这三个表的数据联合查询,按照时间的顺序列出20条数据请教一个合理的快速的select语句?谢谢
      

  16.   

    select * from tt where `contype` =1 order by 时间 limit 20
    `contype`、时间建立复合索引
      

  17.   


    没有,因为contype只有三个取值,所以我要根据contype建立三个表,分别保存这些数据,但有时候要从这三个表中连表查询,使这些数据按datatime的时间顺序列出30条数据不知道该使用join或者union或者其他select语句?
      

  18.   

    select * from tt1 order by 时间 limit 20
    union all
    select * from tt2 order by 时间 limit 20
    union all
    select * from tt3 order by 时间 limit 20
      

  19.   


    这么写报语法错误似乎应该这样写select * from con_1 
    union all
    select * from con_2 
    order by `时间` limit 20但这样查表很慢的,它每个表都要遍历
      

  20.   

    SELECT * FROM (SELECT * FROM tt1 ORDER BY 时间 LIMIT 20) A
    UNION ALL
    SELECT * FROM (SELECT * FROM tt2 ORDER BY 时间 LIMIT 20) A
    UNION ALL
    SELECT * FROM (SELECT * FROM tt3 ORDER BY 时间 LIMIT 20) A
      

  21.   

    select * from (
    (select * from con_1  order by `时间` limit 20)
    union all
    (select * from con_2  order by `时间` limit 20)
    ) t  
    order by `时间` limit 20