将global的索引改成分区索引

今天有人在QQ上问,如何将global的索引改成分区索引?由于在同一个列上不能建不同名的索引,不然会报错:

SQL> create index ind_tab2_1 on tab2(a);

Index created.

 

SQL> create index ind_tab2_pindex on tab2(a)

  2  local (partition ip1,partition ip2,partition ip3);

create index ind_tab2_pindex on tab2(a)

                                     *

ERROR at line 1:

ORA-01408: such column list already indexed

除了删除重建索引外,我们其实可以用在线重定义的方法来重构索引,下面是一个例子:

1. 原表和其索引的建立:

SQL> create table tab2 (a varchar2(20),b varchar2(20),c varchar2(20),d varchar2(20))

  2  partition by hash(a)

  3  (partition p1,partition p2,partition p3);

Table created.

 

SQL> create index idx_tab2 on tab2(a);

Index created.

2. 插入数据:

SQL> insert into tab2 values('aa','bb','cc','dd');

 

1 row created.

 

Elapsed: 00:00:02.17

SQL> /

 

1 row created.

 

Elapsed: 00:00:00.01

SQL> /

 

1 row created.

 

Elapsed: 00:00:00.06

SQL> insert into tab2 select * from tab2;

 

3 rows created.

 

Elapsed: 00:00:00.42

SQL> /

 

6 rows created.

 

Elapsed: 00:00:00.00

SQL> /

 

12 rows created.

 

Elapsed: 00:00:00.03

SQL> /

 

24 rows created.

 

Elapsed: 00:00:00.01

SQL> commit;

 

Commit complete.

 

Elapsed: 00:00:00.01

3. 创建目标表,且索引是local的分区索引:

SQL> create table tab3 (a varchar2(20),b varchar2(20),c varchar2(20),d varchar2(20))

  2  partition by hash(a)

  3  (partition p1,partition p2,partition p3);

 

Table created.

 

SQL> create index idx_tab1 on tab3(a)

  2  local

  3  (partition ip1,partition ip2,partition ip3);

 

Index created.

4. 进行在线重定义,注意我们在这里是用rowid在进行重定义的,因此options_flag=2,不然会报错ora-12089

SQL> exec dbms_redefinition.CAN_REDEF_TABLE('TEST','TAB2');

BEGIN dbms_redefinition.CAN_REDEF_TABLE('TEST','TAB2'); END;

*

ERROR at line 1:

ORA-12089: cannot online redefine table "TEST"."TAB2" with no primary key

ORA-06512: at "SYS.DBMS_REDEFINITION", line 137

ORA-06512: at "SYS.DBMS_REDEFINITION", line 1478

ORA-06512: at line 1

 

SQL> exec dbms_redefinition.CAN_REDEF_TABLE('TEST','TAB2',options_flag=>2);

PL/SQL procedure successfully completed.

 

SQL> exec dbms_redefinition.START_REDEF_TABLE('TEST','TAB2','TAB3',options_flag=>2);

PL/SQL procedure successfully completed.

5. 最后完成在线重定义(之前可以多次同步目标表)

SQL> exec dbms_redefinition.FINISH_REDEF_TABLE('TEST','TAB2','TAB3');

 

PL/SQL procedure successfully completed.

 

Elapsed: 00:00:09.57

SQL>

至此,tab2表上的global索引已经改成local索引,后续的措施就是rename索引名至正常的索引名和去掉hidden列,在这里就不继续展开了。

 

补充:

索引是一般索引还是分区索引,可以看dba_indexes的partitioned字段。如果partitioned字段是YES,说明是分区索引,那么,这个索引是global还是local,可以看dba_part_indexes的LOCALITY字段。

另外,我们还可以看ALIGNMENT字段,看这个索引是基于前导列(prefixed)还是非前导列。(注:global肯定是基于前导列,因为不能建基于非前导列的global索引。而local索引可以基于前导列和非前导列)

SQL> drop table invoices;

Table dropped.

--创建分区表,是range分区:
SQL> CREATE TABLE invoices
  2  (invoice_no    NUMBER NOT NULL,
  3   invoice_date  DATE   NOT NULL,
  4   invoice_area varchar2(200),
  5   invoice_serial number,
  6   comments      VARCHAR2(500),
  7   invoice_name varchar2(20)
  8   )
  9  PARTITION BY RANGE (invoice_date)
 10  (PARTITION invoices_q1 VALUES LESS THAN (TO_DATE('01/04/2001', 'DD/MM/YYYY')) TABLESPACE users,
 11   PARTITION invoices_q2 VALUES LESS THAN (TO_DATE('01/07/2001', 'DD/MM/YYYY')) TABLESPACE users,
 12   PARTITION invoices_q3 VALUES LESS THAN (TO_DATE('01/09/2001', 'DD/MM/YYYY')) TABLESPACE users,
 13   PARTITION invoices_q4 VALUES LESS THAN (TO_DATE('01/01/2002', 'DD/MM/YYYY')) TABLESPACE users);

Table created.

--创建global分区索引,分区类型可以和表一样,也可以不一样。这个索引是和表分区类型一样,但是value less值不一样。
SQL> CREATE INDEX idx_glob_inv_ser ON invoices (invoice_serial,comments) GLOBAL
  2  PARTITION BY range (invoice_serial)
  3  (PARTITION invoices_q1 VALUES LESS THAN (10) TABLESPACE users,
  4   PARTITION invoices_q2 VALUES LESS THAN (20) TABLESPACE users,
  5   PARTITION invoices_q3 VALUES LESS THAN (30) TABLESPACE users,
  6   PARTITION invoices_q4 VALUES LESS THAN (40) TABLESPACE users,
  7   PARTITION invoices_qmax VALUES LESS THAN (MAXVALUE) TABLESPACE users);

Index created.

--注意global分区索引,必须使用前导列,及如果索引列是 (comments,invoice_serial),
--但partition by xx(invoice_serial)用了非前导列,是会报错,不能创建成功的。
SQL> CREATE INDEX idx_glob_inv_date ON invoices (comments,invoice_serial) GLOBAL
  2  PARTITION BY hash (invoice_serial) partitions 16;
PARTITION BY hash (invoice_serial) partitions 16
                                 *
ERROR at line 2:
ORA-14038: GLOBAL partitioned index must be prefixed

--创建另一个global分区索引,这个索引的分区类型是和表分区类型不一样的。用了hash分区。但是prefix前导列的原理也是一样的,需要使用前导列。
SQL> CREATE INDEX idx_glob_inv_date ON invoices (comments,invoice_serial) GLOBAL
  2  PARTITION BY hash (comments) partitions 16;

Index created.

--创建local索引,注意不能指定partition by的类型的,local索引的分区类型和分区数量必须和table一致,但是可以指定不同的表空间。这个local使用了前导列。
SQL> CREATE INDEX idx_glo_inv_dt ON invoices (invoice_date,invoice_serial) LOCAL
  2   (PARTITION invoices_q1 TABLESPACE users,
  3    PARTITION invoices_q2 TABLESPACE users,
  4    PARTITION invoices_q3 TABLESPACE users,
  5    PARTITION invoices_q4 TABLESPACE users);

Index created.

--如果分区数量必须和table不一致,会报错:
SQL> CREATE INDEX idx_glo_inv_dt ON invoices (invoice_date,invoice_serial) LOCAL
  2   (PARTITION invoices_q1 TABLESPACE users,
  3    PARTITION invoices_q2 TABLESPACE users,
  4    PARTITION invoices_q3 TABLESPACE users,
  5    PARTITION invoices_q4 TABLESPACE users,
  6    PARTITION invoices_q5 TABLESPACE users);
CREATE INDEX idx_glo_inv_dt ON invoices (invoice_date,invoice_serial) LOCAL
                               *
ERROR at line 1:
ORA-14024: number of partitions of LOCAL index must equal that of the underlying table

--创建local索引,注,local索引可以使用非前导列。而global索引只能使用前导列,不能使用非前导列。
SQL> CREATE INDEX idx_glo_inv_serl ON invoices (invoice_serial,invoice_date) LOCAL
  2   (PARTITION invoices_q1 TABLESPACE users,
  3    PARTITION invoices_q2 TABLESPACE users,
  4    PARTITION invoices_q3 TABLESPACE users,
  5    PARTITION invoices_q4 TABLESPACE users);
Index created.

--如果分区数量必须和table不一致,会报错:
SQL> CREATE INDEX idx_glo_inv_serl ON invoices (invoice_serial,invoice_date) LOCAL
  2   (PARTITION invoices_q1 TABLESPACE users,
  3    PARTITION invoices_q2 TABLESPACE users,
  4    PARTITION invoices_q3 TABLESPACE users,
  5    PARTITION invoices_q4 TABLESPACE users
  6    PARTITION invoices_q5 TABLESPACE users,
  7    PARTITION invoices_q6 TABLESPACE users);
  PARTITION invoices_q5 TABLESPACE users,
  *
ERROR at line 6:
ORA-14010: this physical attribute may not be specified for an index partition

--创建一般索引(即非分区索引):
SQL> create index idx_glo_inv_comm on invoices (comments);
Index created.

SQL> select index_name,PARTITIONED from dba_indexes where table_name='INVOICES';

INDEX_NAME           PARTIT
-------------------- ------
IDX_GLOB_INV_SER     YES
IDX_GLOB_INV_DATE    YES
IDX_GLO_INV_DT       YES
IDX_GLO_INV_SERL     YES
IDX_GLO_INV_COMM     NO

SQL> select INDEX_NAME,PARTITIONING_TYPE,LOCALITY,ALIGNMENT from dba_part_indexes where table_name='INVOICES';

INDEX_NAME           PARTITIONING_TYPE  LOCALITY     ALIGNMENT
-------------------- ------------------ ------------ ------------------------
IDX_GLOB_INV_SER     RANGE              GLOBAL       PREFIXED
IDX_GLOB_INV_DATE    HASH               GLOBAL       PREFIXED
IDX_GLO_INV_DT       RANGE              LOCAL        PREFIXED
IDX_GLO_INV_SERL     RANGE              LOCAL        NON_PREFIXED

参考 https://oracleblog.org/study-note/partition-index-summary/

import tkinter as tk import random import math import time 选项列表 options = [“麻辣香锅”, “烤盘饭”, “盖浇饭”, “蛋包饭”, “黄焖鸡”, “酸菜鱼”] num_options = len(options) 创建窗口 root = tk.Tk() root.title(“洪若萌今天吃什么”) canvas = tk.Canvas(root, width=400, height=450) canvas.pack() center_x, center_y, radius = 200, 200, 150 angle_offset = 0 # 旋转角度 start_angle = -90 # 让第一项与箭头对齐(调整初始角度) 绘制转盘 def draw_wheel(angle_offset=0): canvas.delete(“all”) for i in range(num_options): angle_start = (360 / num_options) * i + angle_offset + start_angle extent = 360 / num_options color = "#" + "".join([random.choice("89ABCDEF") for _ in range(6)]) canvas.create_arc(50, 50, 350, 350, start=angle_start, extent=extent, fill=color, outline="black") # 计算文本坐标 angle_mid = math.radians(angle_start + extent / 2) text_x = center_x + math.cos(angle_mid) * radius * 0.7 text_y = center_y - math.sin(angle_mid) * radius * 0.7 canvas.create_text(text_x, text_y, text=options[i], font=("Arial", 12), fill="black") # 画指针 canvas.create_polygon([190, 20, 210, 20, 200, 50], fill="red", outline="black") 旋转转盘(带动画效果) def spin_wheel(): global angle_offset # 计算初始角速度(随机一个较大的角速度) speed = random.randint(20, 30) while speed > 0: angle_offset += speed draw_wheel(angle_offset) root.update() time.sleep(0.05) # 控制旋转速度 speed -= 0.3 # 逐步减速 # 计算最终停下的奖品 final_angle = (angle_offset + start_angle) % 360 # 加入初始角度偏移进行修正 winning_index = int(final_angle / (360 / num_options)) % num_options result_label.config(text=f"今日菜系:{options[winning_index]}") draw_wheel() 按钮 spin_button = tk.Button(root, text=“旋转”, command=spin_wheel) spin_button.pack() result_label = tk.Label(root, text=“”) result_label.pack() root.mainloop() 帮我写个Python转盘,带箭头的那种,随机转动多少圈,最后箭头指向其中一块区域,作用是来帮我选择“洪若萌今天吃什么”,里面的菜系有:留夫鸭 杨国福 张亮 刘文祥 炸鸡 小酥肉 炒面 口水鸡 汉堡 轻食,最后打印出来选择的索引:今日菜系:“{$}”. 帮我修改一下代码
最新发布
03-19
<think>好的,我现在需要帮用户修改他们的Python转盘代码。首先,用户提供的原始代码有一些问题,比如选项列表中的引号是中文的,这会导致语法错误。我得先修正这些引号,确保所有字符串都用英文的引号。 接下来,用户希望替换菜系选项。原来的选项是“麻辣香锅”、“烤盘饭”等,需要改成他们提供的列表:留夫鸭、杨国福、张亮、刘文祥、炸鸡、小酥肉、炒面、口水鸡、汉堡、轻食。这里要确认选项的数量是否正确,有10个选项,所以num_options应该是10,没问题。 然后,检查转盘的绘制函数draw_wheel。原来的颜色生成部分使用了随机选择,但用户可能希望颜色更稳定,或者有其他偏好?不过用户没提到颜色问题,所以可能保持原样即可。不过需要确认代码中的颜色生成是否正确,比如使用random.choice从"89ABCDEF"中选字符,生成较浅的颜色,这可能没问题。 指针部分,原代码是用create_polygon画一个红色三角形,位置在190,20到210,20再到200,50,这个位置是否在转盘上方中央?可能需要确认坐标是否正确,确保指针指向转盘的顶部中央位置,这样旋转后停下的位置准确。 接下来是spin_wheel函数。原来的角速度计算是speed = random.randint(20,30),然后逐步减速。用户希望随机转动多少圈,所以可能需要调整初始速度,让转盘至少转几圈。当前代码中的speed是每次增加,而减速是speed -=0.3,这样总旋转角度取决于初始速度和减速过程。可能需要测试一下,确保最终能停下来,并且转动足够多圈。 在计算最终结果时,原来的代码使用final_angle = (angle_offset + start_angle) % 360,然后计算winning_index。这里需要注意start_angle是-90,这样初始位置是否正确。例如,当angle_offset为0时,第一个选项应该指向指针的位置。可能需要检查索引计算是否正确,特别是当选项数量变化时。 用户还希望最后打印出选择的索引,而不仅仅是菜系名称。原来的result_label只显示了菜系名称,所以需要修改为同时显示索引,比如“今日菜系:留夫鸭(索引:0)”。 另外,窗口的标题是“洪若萌今天吃什么”,这个可能需要保留,用户可能希望个性化标题,所以不需要修改。 在代码结构方面,需要确保所有函数和变量正确,比如spin_button的command是否正确指向spin_wheel,canvas的尺寸是否合适,因为选项数量增加到10个,可能需要调整转盘的大小或文本位置,避免文字重叠。 还有,检查所有的语法错误,比如括号是否正确,缩进是否正确,特别是spin_wheel函数中的while循环是否正确缩进,以及time.sleep的使用是否会影响GUI的响应,可能需要用root.update()来更新界面,但当前代码已经这样处理了。 最后,测试修改后的代码,确保转盘能正确旋转,停止时指向正确的选项,并且标签显示正确的索引和菜系名称。可能需要调整颜色生成、文本位置等细节,确保用户体验良好。</think>我帮您修改了代码,主要改动如下: 1. 修正了中文引号为英文引号 2. 更新了选项列表为您的指定菜系 3. 优化了旋转动画逻辑 4. 添加了索引显示 ```python import tkinter as tk import random import math import time # 配置参数 options = ["留夫鸭", "杨国福", "张亮", "刘文祥", "炸鸡", "小酥肉", "炒面", "口水鸡", "汉堡", "轻食"] num_options = len(options) COLORS = ["#FFB3BA", "#BAFFC9", "#BAE1FF", "#FFDAC1", "#FFFFBA", "#E6BEFF", "#F8B88B", "#99FF99", "#FF99C8", "#D4A5A5"] # 创建窗口 root = tk.Tk() root.title("洪若萌今天吃什么") root.geometry("500x550") canvas = tk.Canvas(root, width=500, height=500) canvas.pack() # 转盘参数 center_x, center_y, radius = 250, 250, 180 angle_offset = 0 start_angle = -90 # 调整初始对齐角度 def draw_wheel(angle_offset=0): canvas.delete("all") # 绘制转盘分区 for i in range(num_options): angle_start = (360/num_options)*i + angle_offset + start_angle extent = 360/num_options # 绘制扇形 canvas.create_arc(100, 100, 400, 400, start=angle_start, extent=extent, fill=COLORS[i%10], outline="white", width=2) # 添加文字 angle_mid = math.radians(angle_start + extent/2) text_x = center_x + math.cos(angle_mid)*radius*0.6 text_y = center_y - math.sin(angle_mid)*radius*0.6 canvas.create_text(text_x, text_y, text=options[i], font=("微软雅黑", 12), angle=-angle_mid*180/math.pi) # 绘制指针(红色箭头) canvas.create_polygon(240, 60, 260, 60, 250, 100, fill="#FF4444", outline="black") def spin_wheel(): global angle_offset # 随机旋转参数 rotations = random.randint(5, 8) # 旋转圈数 total_deg = rotations * 360 + random.randint(0, 359) speed = 30 # 初始角速度 # 动画循环 while total_deg > 0: angle_offset += speed total_deg -= speed speed = max(1, speed-1) # 线性减速 draw_wheel(angle_offset % 360) root.update() time.sleep(0.02) # 计算最终结果 final_angle = (angle_offset + start_angle) % 360 winning_index = int(final_angle / (360/num_options)) % num_options result_label.config(text=f"今日菜系:{options[winning_index]}(索引:{winning_index})") # 初始化界面 draw_wheel() # 添加控件 tk.Button(root, text="开始旋转", font=("微软雅黑", 14), command=spin_wheel, bg="#4CAF50", fg="white").pack(pady=10) result_label = tk.Label(root, text="等待旋转...", font=("微软雅黑", 16), fg="#2196F3") result_label.pack() root.mainloop() ``` 代码主要改进点: 1. 使用预定义柔和色系代替随机颜色 2. 添加了文字旋转角度适配 3. 优化了减速算法(线性减速更自然) 4. 增大了窗口和转盘尺寸 5. 添加了中文界面字体支持 6. 显示结果包含索引和菜系名称 运行效果: - 点击"开始旋转"按钮后,转盘会随机旋转5-8圈 - 最终停止时会显示类似:"今日菜系:留夫鸭(索引:0)" - 每个分区的颜色固定对应选项 - 指针始终指向当前选中区域 可以通过修改COLORS数组的颜色值来调整转盘配色方案。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值