ALTER TABLE SWITCH' statement failed. The table x' is partitioned while index 'x' is not partitioned.

1.L_Monitoring有这么些字段,ID,Collecttime,PlateType,PlateNO以及其他一些这段.
建立这个表的时候是个非分区表,其中ID是主键,并在Collecttime,PlateType,PlateNO上面建立了索引.

2.系统运行一阵子后,L_Monitoring数据变得非常大,5,6千万,而且后续还会更大.
所以要求将L_Monitoring表进行分区.分区方案是按照Collecttime进行每天分区.
Collecttime为分区字段,并将Collecttime字段改为聚集索引,原来主键ID改为非聚集索引.
请注意这个步骤:L_Monitoring表我是先建立索引,在建立分区;

3.系统要求清除三个月前的数据,只保留最近3个月的数据.所以,系统运行一阵子(3个月)后就需要清除数据.
加了JOB每天晚上从L_Monitoring删除数据,发现每天晚上数据库会在JOB执行删除的时候卡死.
或者说,每天晚上执行job的时候,对L_Monitoring表的操作会阻塞.
语句如下:delete L_Monitoring where Collecttime< ‘3个月前‘

4.所以清除数据方法必须更改,不可单纯的用delete.所以改为SWITCH PARTITION的方法来删除数据.
这个方法的原理就是:
a.先建立一个和L_Monitoring结构一样的表:L_Monitoring_SwitchOut(L_Monitoring_SwitchOut这表我先用Collecttime分区,然后建立索引,请注意这个先后关系)
b.用ALTER TABLE L_Monitoring SWITCH PARTITION x TO L_Monitoring_SwitchOut PARTITION x语句将某一个分区的数据移动到L_Monitoring_SwitchOut,这个步骤是瞬时完成的
c.truncate table L_Monitoring_SwitchOut
所以,这种方法删除数据,按理说,会很快的.

5.可是ALTER TABLE L_Monitoring SWITCH PARTITION的时候,报错如下:
‘ALTER TABLE SWITCH‘ statement failed. The table ‘ITMP2.dbo.L_Monitoring‘ is partitioned while index ‘IX_L_Monitoring_ID‘ is not partitioned.
其实这个错误提示得很明显了,index is not partitioned(索引没有分区),但是我当时没有搞清楚原理,所以导致各种实验.
原理就是:表的数据可以存不同分区,索引一样可存不同分区.
哎,我搞了好久,查了好多资料.
开始觉得有其他索引的表不能SWITCH,还试了SWITCH之前把索引删除,再SWITCH,然后recreate索引这种方法,这种方法可以swtich成功,但是每次recreate的时候也会阻塞.
最后各种实验终于发现在L_Monitoring表删除索引在recreate后,就可以swtich了.

好吧,可能你没看懂,为什么在索引第一次recreate后,就可以了呢.下面来解答:
1.表在建立索引的时候,如果表是分区表,则索引默认在分区上.
2.如果先建立索引,再分区,索引则在primary上;
所以,上面我步骤中,导致L_Monitoring和L_Monitoring_SwitchOut其实不同的,一个的索引在primary上,一个的索引在分区上.

明白了原理就好了,在上面第5步的时候, SWITCH之前,把PlateType,PlateNO,ID的索引删除在重建(只执行一次)就可以了.(或者利用stutio更改索引的stroage),重新建立的索引会在分区上,而L_Monitoring_SwitchOut的索引也是在分区后建立的,所以索引也存在分区上的.这样,2个表的结构才一模一样了,可以swtich了!

没代码和脚本,全文字,慢慢看吧,希望大家有帮助.

关键字:分区 delete 数据 卡死 分区表

ALTER TABLE SWITCH' statement failed. The table x' is partitioned while index 'x' is not partitioned.

时间: 2024-07-29 20:30:44

ALTER TABLE SWITCH' statement failed. The table x' is partitioned while index 'x' is not partitioned.的相关文章

mysql切换数据库提示警告:Reading table information for completion of table and column names

登录数据库后,选择数据库时发现以下提示, mysql> use testReading table information for completion of table and column namesYou can turn off this feature to get a quicker startup with -A Database changedmysql> 意思是 预读这个库中表以及表列信息,一般原因是当库中表很多,表中数据很大时,就会出现执行use <库名>后半天

switch statement

A switch statement allows a program to evaluate an expression and attempt to match the expression's value to a case label. If a match is found, the program executes the associated statement. A switch statement looks as follows: switch (expression) {

C++ basic - Switch statement

WHAT? 在写程序的时候我们会遇到很多种根据得出的数值就行不同处理的conditional statements,我们可以选用switch写法,以免使用太多的nested if会让程序difficult to read. syntax: switch(value) { case value1: ...; break; //break不能省略以免程序继续执行判断 case value2: ...; break; case value3: ...; break; default:...; } fo

解决hiveserver2报错:java.io.IOException: Job status not available - Error while processing statement: FAILED: Execution Error, return code 1 from org.apache.hadoop.hive.ql.exec.mr.MapRedTask

用户使用的sql: select count( distinct patient_id ) from argus.table_aa000612_641cd8ce_ceff_4ea0_9b27_0a3a743f0fe3; 下面做不同的测试: 1.beeline -u jdbc:hive2://0.0.0.0:10000 -e "select count( distinct patient_id ) from argus.table_aa000612_641cd8ce_ceff_4ea0_9b27_

解决:Reading table information for completion of table and column names

mysql -A不预读数据库信息(use dbname 更快)-Reading table information for completion of table and column names You can turn off this feature to get a quicker startup with -A mysql> use dbname Reading table information for completion of table and column names You

innodb table level lock 与lock table语句关系

DDL语句只申请意向级别的表锁.在lock table语句中,mysql会申请mysql级别表锁,同时innodb也会申请innodb级别表锁.前提是innodb_table_locks=1 https://www.percona.com/blog/2012/07/31/innodb-table-locks/ MySQL Table level locks and Innodb Table Levellocks are two separate beings. You almost never

Truncate table、Delete与Drop table的区别

Truncate table.Delete与Drop table的区别 TRUNCATE TABLE 在功能上与不带 WHERE 子句的 DELETE 语句相同:二者均删除表中的全部行.但 TRUNCATE TABLE 比 DELETE 速度快,且使用的系统和事务日志资源少. DELETE 语句每次删除一行,并在事务日志中为所删除的每行记录一项.TRUNCATE TABLE 通过释放存储表数据所用的数据页来删除数据,并且只在事务日志中记录页的释放. TRUNCATE TABLE 删除表中的所有行

Can rename table but can not truncate table

一个表无法truncate可是能够rename,这个乍听起来认为好奇怪,以下模拟该过程. 3个session: session1运行truncate和rename操作. session2运行lock表操作: session3进行监控. session1: [[email protected] contrib]$ psql gtlions psql (8.2.15) Type "help" for help. gtlions=# \d test Table "public.te

ireport5.6使用table组件,如何用table显示javaBean数据源

1.从组件面板添加一个table组件到报表中. 2.设计table的字段头. 合并操作 1. 2. 删除你不需要的列 新增你的合并列 3.在报表Parameters里新增一个参数dets(java.util.List) 4.配置table数据集 a.重命令数据集(方便) 右键->属性,即可修改. b.添加一个Parameters 新增一个table1(net.sf.jasperreports.engine.JRDataSource) c.手动配置代码,把dets参数传递给table1接收 <d