复合索引abc,查询的时候查c a和ac都能用到索引吗

复合索引(也称为联合索引)的使用遵循“最左前缀原则”。这意味着查询条件必须从索引定义的最左边开始,并且连续地匹配索引中的列,才能有效地使用该索引。具体到你提到的复合索引 abc,它意味着索引按照 a、b 和 c 列的顺序进行排序。

根据这一原则,以下情况可以有效利用复合索引 abc:

1,查询条件包含 a,则可以使用索引。
2,查询条件包含 a 和 b,则可以使用索引。
3,查询条件包含 a、b 和 c,则可以使用索引。
但是,以下情况不能有效利用复合索引 abc:
1,如果查询只包含 c,则不能使用索引,因为没有从最左前缀开始。
2,如果查询包含 c 和 a,但由于没有包含 b,也不能使用索引。
对于你提到的查询 c a 和 ac:

c a:这个查询顺序不正确,因为没有从索引的最左前缀 a 开始,所以复合索引 abc 不能被使用。
ac:这个查询也不能使用复合索引 abc,因为尽管它从 a 开始,但在 c 出现之前缺少了 b,打破了连续性。
因此,复合索引 abc 只能在查询条件从 a 开始并连续包含 b 和 c 的情况下被有效使用。如果需要优化涉及 c 和 a 的查询,可能需要创建一个额外的复合索引,如 ca 或者 cba,具体取决于查询模式和表的使用情况。在设计复合索引时,应考虑到最常见的查询模式,以确保索引能够被有效地利用。

二,验证

PostgreSQL 数据库中创建一个学生信息表tbl_student_info ,包含字段id,name,age,sex,height,weight,用name,age,height建立联合索引,并提供创建30个学生信息的sql,用来验证联合索引

CREATE TABLE tbl_student_info (
    id numeric(19) PRIMARY KEY,
    name VARCHAR(100) NOT NULL,
    age INTEGER NOT NULL,
    sex varvhar(4),
    height DECIMAL(4,1),
    weight DECIMAL(4,1)
);

-- 创建联合索引
CREATE INDEX idx_name_age_height ON tbl_student_info (name, age, height);

创建一个自增队列,用来创建id

create sequence tbl_student_info_seq;

创建30条数据:

INSERT INTO tbl_student_info (id,name, age, sex, height, weight) VALUES (tbl_student_info_seq.nextval,'张三', 20, '男', 175.0, 65.0);
INSERT INTO tbl_student_info (id,name, age, sex, height, weight) VALUES (tbl_student_info_seq.nextval,'李四', 21, '女', 162.0, 52.0);
INSERT INTO tbl_student_info (id,name, age, sex, height, weight) VALUES (tbl_student_info_seq.nextval,'王五', 22, '男', 178.0, 70.0);
INSERT INTO tbl_student_info (id,name, age, sex, height, weight) VALUES (tbl_student_info_seq.nextval,'赵六', 19, '女', 165.0, 55.0);
INSERT INTO tbl_student_info (id,name, age, sex, height, weight) VALUES (tbl_student_info_seq.nextval,'周七', 23, '男', 180.0, 75.0);
INSERT INTO tbl_student_info (id,name, age, sex, height, weight) VALUES (tbl_student_info_seq.nextval,'吴八', 20, '女', 160.0, 50.0);
INSERT INTO tbl_student_info (id,name, age, sex, height, weight) VALUES (tbl_student_info_seq.nextval,'郑九', 21, '男', 176.0, 68.0);
INSERT INTO tbl_student_info (id,name, age, sex, height, weight) VALUES (tbl_student_info_seq.nextval,'孙十', 22, '女', 163.0, 53.0);
INSERT INTO tbl_student_info (id,name, age, sex, height, weight) VALUES (tbl_student_info_seq.nextval,'钱十一', 19, '男', 172.0, 63.0);
INSERT INTO tbl_student_info (id,name, age, sex, height, weight) VALUES (tbl_student_info_seq.nextval,'冯十二', 23, '女', 168.0, 57.0);

INSERT INTO tbl_student_info (id,name, age, sex, height, weight) VALUES (tbl_student_info_seq.nextval,'陈十三', 20, '男', 177.0, 67.0);
INSERT INTO tbl_student_info (id,name, age, sex, height, weight) VALUES (tbl_student_info_seq.nextval,'沈十四', 21, '女', 161.0, 51.0);
INSERT INTO tbl_student_info (id,name, age, sex, height, weight) VALUES (tbl_student_info_seq.nextval,'韩十五', 22, '男', 179.0, 71.0);
INSERT INTO tbl_student_info (id,name, age, sex, height, weight) VALUES (tbl_student_info_seq.nextval,'杨十六', 19, '女', 164.0, 54.0);
INSERT INTO tbl_student_info (id,name, age, sex, height, weight) VALUES (tbl_student_info_seq.nextval,'周十七', 23, '男', 181.0, 76.0);
INSERT INTO tbl_student_info (id,name, age, sex, height, weight) VALUES (tbl_student_info_seq.nextval,'吴十八', 20, '女', 166.0, 56.0);
INSERT INTO tbl_student_info (id,name, age, sex, height, weight) VALUES (tbl_student_info_seq.nextval,'郑十九', 21, '男', 174.0, 66.0);
INSERT INTO tbl_student_info (id,name, age, sex, height, weight) VALUES (tbl_student_info_seq.nextval,'王二十', 22, '女', 167.0, 58.0);
INSERT INTO tbl_student_info (id,name, age, sex, height, weight) VALUES (tbl_student_info_seq.nextval,'赵二十一', 19, '男', 173.0, 64.0);
INSERT INTO tbl_student_info (id,name, age, sex, height, weight) VALUES (tbl_student_info_seq.nextval,'李二十二', 23, '女', 169.0, 59.0);

INSERT INTO tbl_student_info (id,name, age, sex, height, weight) VALUES (tbl_student_info_seq.nextval,'刘二十三', 20, '男', 176.0, 69.0);
INSERT INTO tbl_student_info (id,name, age, sex, height, weight) VALUES (tbl_student_info_seq.nextval,'陈二十四', 21, '女', 162.0, 52.0);
INSERT INTO tbl_student_info (id,name, age, sex, height, weight) VALUES (tbl_student_info_seq.nextval,'杨二十五', 22, '男', 178.0, 72.0);
INSERT INTO tbl_student_info (id,name, age, sex, height, weight) VALUES (tbl_student_info_seq.nextval,'周二十六', 19, '女', 165.0, 55.0);
INSERT INTO tbl_student_info (id,name, age, sex, height, weight) VALUES (tbl_student_info_seq.nextval,'吴二十七', 23, '男', 180.0, 75.0);
INSERT INTO tbl_student_info (id,name, age, sex, height, weight) VALUES (tbl_student_info_seq.nextval,'郑二十八', 20, '女', 160.0, 50.0);
INSERT INTO tbl_student_info (id,name, age, sex, height, weight) VALUES (tbl_student_info_seq.nextval,'王二十九', 21, '男', 176.0, 68.0);
INSERT INTO tbl_student_info (id,name, age, sex, height, weight) VALUES (tbl_student_info_seq.nextval,'李三十', 22, '女', 163.0, 53.0);

验证联合索引

为了验证联合索引是否被正确使用,你可以执行一些查询,比如:

-- 查询1: 使用name, age和height
EXPLAIN (ANALYZE, BUFFERS) 
SELECT * FROM tbl_student_info WHERE name='张三' AND age=20 AND height=165.0;

-- 查询2: 使用name和age
EXPLAIN (ANALYZE, BUFFERS) 
SELECT * FROM tbl_student_info WHERE name='李四' AND age=20;

-- 查询3: 只使用name
EXPLAIN (ANALYZE, BUFFERS) 
SELECT * FROM tbl_student_info WHERE name='王五';

-- 查询4: 只使用height
EXPLAIN (ANALYZE, BUFFERS) 
SELECT * FROM tbl_student_info WHERE height=165.0;

2024年7月12号

尴尬了:我使用的TDsql,只查height,也使用了索引

在这里插入图片描述

### PyCharm 打开文件显示不全的解决方案 当遇到PyCharm打开文件显示不全的情况时,可以尝试以下几种方法来解决问题。 #### 方法一:清理缓存并重启IDE 有时IDE内部缓存可能导致文件加载异常。通过清除缓存再启动程序能够有效改善此状况。具体操作路径为`File -> Invalidate Caches / Restart...`,之后按照提示完成相应动作即可[^1]。 #### 方法二:调整编辑器字体设置 如果是因为字体原因造成的内容显示问题,则可以通过修改编辑区内的文字样式来进行修复。进入`Settings/Preferences | Editor | Font`选项卡内更改合适的字号大小以及启用抗锯齿功能等参数配置[^2]。 #### 方法三:检项目结构配置 对于某些特定场景下的源码视图缺失现象,可能是由于当前工作空间未能正确识别全部模块所引起。此时应该核Project Structure里的Content Roots设定项是否涵盖了整个工程根目录;必要时可手动添加遗漏部分,并保存变更生效[^3]。 ```python # 示例代码用于展示如何获取当前项目的根路径,在实际应用中可根据需求调用该函数辅助排问题 import os def get_project_root(): current_file = os.path.abspath(__file__) project_dir = os.path.dirname(current_file) while not os.path.exists(os.path.join(project_dir, '.idea')): parent_dir = os.path.dirname(project_dir) if parent_dir == project_dir: break project_dir = parent_dir return project_dir print(f"Current Project Root Directory is {get_project_root()}") ```
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

见未见过的风景

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值