1、select * from table where a=a and b=b and c=c and d=d; 这里我建立了一个a,b,c,d的复合索引,但如果使用: select * from table where a=a and b=b; 进行查询时第一次很慢,第二次之后再查就快了,应该是使用了数据库的缓冲区进行的查询,那这个abcd的复合索引对于where a=a and b=b;就不起作用了么?如果有若干个SQL: select * from table where a=a; select * from table where a=a and b=b; select * from table where a=a and b=b and c=c; select * from table where a=a and b=b and c=c and d=d; 等等,总不能对每个where条件建立一个索引?
2、有没有SQL可以查询某个SQL执行时是否使用索引了?
谢谢! where a=变量a; where a=变量a and b=变量b; where a=变量a and b=变量b and c=变量c; where a=变量a and b=变量b and c=变量c and d=变量d; 这些都可以用abcd这个复合索引? 但从现在执行的时间来看,a=变量a这里是没有用到索引的,不知道有什么方法可以让它走索引么?
方法如下: Oracle中建立索引,会提高查询速度: create index 索引名 on 表名(列名); 例如: create index index_userid on tbl_detail(userid); 如何找数据库表的主键字段的名称? SELECT * FROM user_constraints WHERE CONSTRAINT_TYPE='P' and table_name='AAA'; select * from dba_cons_columns where CONSTRAINT_NAME='SYS_AAA'; Oracle 在创建主键(可以不加constraint SYS_AAA),会为库表自动创建索引, 索引的列为主键列。 并且当库表某些列名或者库表名改变时候, Oracle自动创建的索引SYS_AAA,中的索引列也会自动更新(类似于视图),并且SYS_AAA会与名字更改后的库表还是保持索引关系。 关键系统库表: desc dba_constraints desc dba_cons_columns desc dba_indexes desc dba_ind_columns desc DBA_TAB_COLUMNS 例子1:更改库表的列名 ALTER TABLE AAA RENAME COLUMN ID TO AAA_ID; create table AAA ( ID NUMBER(8), NAME CHAR(20), constraint SYS_AAA primary key(ID) ); //查找约束名字 select c.CONSTRAINT_NAME,c.table_name,cc.COLUMN_NAME from user_constraints c, user_cons_columns cc where c.constraint_name=cc.constraint_name and c.table_name ='AAA' AND C.CONSTRAINT_TYPE='P'; //查找索引 select index_name,index_type,uniqueness from user_indexes where table_name='AAA'; INDEX_NAME INDEX_TYPE UNIQUENES
伤-蔓延
2023-03-30 04:28:11
....个人认为貌似和索引的关系不是很大. 因为,系统要解析新的SQL语句肯定要花时间的. 相关内容 我觉得可能要看下 Oracle DB 里面关于 硬解析和软解析的描述.