当前位置:网站首页>Oracle hint understanding
Oracle hint understanding
2022-07-05 22:02:00 【The sea of waves】
hint know
The correct grammar is :
select /*+ index(x idx_t) */ * from t x where x.object_id=123
/*+ */ It's like a comment , There is one more comment “+”, This is it. Hint
Above this hint It means to make Oracle Execute this SQL Forced index .
If hint There is a syntax error in ,Oracle No mistake , Just put /* */ The content in is just a comment .
Unreasonable use Hint Hazards of :
Because the data in the table will change , Generally, it cannot be in the program sql In the use Hint, If it's like the one above Hint The same forced index . In case one day object_id=123 The return result of takes up of the whole table 50% above , At this time, the index will be slower than the full table scanning . Therefore, we should not force all cases to follow the index .Hint Generally used for one-time execution , For example, do data extraction . And it's average Oracle stay 99% Will judge whether it is correct to go to the index , We don't need to specify .Hint Just to cope 1% Under the circumstances .
Append Use
append It's another kind of Hint, General usage :
insert /*+ append */ into b select * from a;
such insert Than ordinary insert It's going to be faster , But it's also costly .
1、 When the data in the table is delete in the future , Table spaces leave gaps , The next time insert Will go to fill the gap . however append Of insert Won't look for space , And directly added to the new space . If you use it all the time append, It will make the table space larger and larger .
2、 This point is more deadly , Namely use append When , Will lock the whole watch , Other users even insert Other data will also be blocked .
Therefore, the production environment must not be used append,append It is also generally used for data extraction .
In fact, most of the time , use append Not much efficiency .
because append The reason why it is fast , It is because the log generation is reduced .
Only the following scenarios append It will reduce log generation :
1、 In non Archive Mode
2、 In archive mode , The status of the table is nologging
First of all, non archive status is generally impossible , Slightly more important systems must be archived .
In archive mode nologging It's not usually , No log , This means that the recovered data will be incomplete in the future , It's risky .
Index Use
Use index Directly match the index to query data and improve the query speed
/+INDEX(TABLE INDEX_NAME)/
select /*+ index(x idx_t) */ * from t x where x.object_id=123
leading and use_nl Use
Use leading and use_nl To set the query order of the table , To speed up the query , Generally, the small table is set as the first table .
/+LEADING(TABLE)/
Take the specified table as the first table in the join order .
/+USE_NL(TABLE)/
Connect the specified table to the nested connected row source , And use the specified table as the internal table .
When making a multi table join query , [ The driver table ] For the definition of :
1) When filter conditions are specified , The table with fewer rows of records that meet the query criteria is **[ The driver table ]**
2) When no filter condition is specified , The table with fewer rows is The driver table
Why Small tables drive large tables ??
Generally speaking , No matter Oracle still Mysql, The goal of optimization is to reduce correlation as much as possible cycles , Ensure that the small table drives the large table
example : user surface 10000 Data ,class surface 20 Data
select * from user u left join class c u.userid=c.userid
This requires user Table cycle 10000 Times to find , And if used class Table driven user Tables only need to cycle 20 You can find it once
analysis : No matter whether the large table is the driving table or the small table is the driving table , The number of comparisons is always 10000*20 Time ???
Advantages of small table driving large table
Advantage lies in :
1. Large tables have indexes : The time to query the large table is O(Log n)
- A full scan of a large watch : Fast disk block query
3 Exists On in The principle of big table driving small table in
Cost calculation method :
Set a small table 100 That's ok , The big table 100000 That's ok .
Both tables have indexes :
If the small watch is included , Big watch out ( The driver table ) Words , Then the number of scans is :
100000+100000*2 ( among 2 Express IO frequency , An index , One time data )
If the big watch is included , The small watch is outside ( The driver table ) Words , Then the number of scans is :
100+100*2.
Both tables have no index :
If the small watch is included , If the big watch is outside , Then the number of scans is :
100000+100*100000
If the big watch is included , If the watch is outside , Then the number of scans is :
100+100000*100
Be careful : If a table has an index , A table has no index ,ORACLE The table without index will be used as the driving table . If both tables have indexes , The outer surface is used as the driving table . If both are not indexed , Is also the appearance of the driving table .
Parallel Use
One 、 Parallel queries
Parallel queries allow a sql select The statement is divided into several smaller queries , Each part of the query runs concurrently , Then combine the results of each part , Provide the final result , Mostly used for full table scanning , Index full scan, etc , Scanning and linking of large tables 、 Create a large index 、 Partition index scan 、 Mass insert updates and deletions .
Enable parallel queries
SQL> ALTER TABLE T1 PARALLEL;
inform oracle, Yes T1 Enable parallel Inquire about , But the parallelism should be determined by referring to the resource load of the system .
utilize hints Tips , Enable parallel , At the same time, it can also tell the clear degree of parallelism , otherwise oracle Decide the parallelism you want to enable , These tips are only for this sql Statement is valid .
SQL> select /+ parallel(t1 8) / count(*) from t1;
SQL> select degree from user_tables where table_name='T1';
DEGREE
DEFAULT
The degree of parallelism is Default, Its value is given by 2 A parameter determines
SQL> show parameter cpu
NAME TYPE VALUE
cpu_count integer 2
parallel_threads_per_cpu integer 2
cpu_count Express cpu Count
parallel_threads_per_cpu Represent each cpu Number of parallel processes allowed
default Under the circumstances , The number of parallels is cpu_count*parallel_threads_per_cpu
Cancel the parallel setting
SQL> alter table t1 noparallel;
SQL> select degree from user_tables wheretable_name='T1';
DEGREE
1
Data dictionary view
v$px_session
sid: For each parallel session sid
qcsid:query coordinator sid, Query coordinator sid
Two 、 parallel dml
parallel dml Include insert,update,delete,merge, stay dml period ,oracle Multiple parallel execution servers can be used to execute insert,update,delete,merge, Multiple sessions execute simultaneously , At the same time, every session ( Concurrent processes ) All have their own undo paragraph , Are independent transactions , These matters are either handled by pdml Coordinator process commit , Or both rollback.
In one there is plenty I/o More bandwidth cpu In the host , For large scale dml, The speed may be greatly improved , Especially in large-scale data warehouse environment .
parallel dml The activation that needs to be displayed
SQL> alter session enable parallel dml;
Disable parallel dml
SQL> alter session disable parallel dml;
3、 ... and 、 parallel ddl
parallel ddl Provides dba The ability to use all machine resources , frequently-used ddl Yes
create table as select ……
create index
alter index rebuild
alter table move
alter table split
In these sql Add... After the statement parallel An example of a clause
SQL> alter table t1 move parallel;
Table altered
SQL> create index T1_IDX on T1 (OWNER,OBJECT_TYPE) tablespace SYSTEM parallel;
purpose
Force parallelism to execute the current SQL. This is in Oracle 9i Later versions can use , The previous version now has no environment for testing . in other words , Add this description , You can forcibly enable Oracle Multithreading function of . For example , It's like a multi-core computer CPU, But in most cases, it will not be fully enabled at the same time (2 More obvious above the nucleus ), Use parallel explain , Multiple cores will work at the same time , To improve efficiency .
But start this function by itself , It also consumes resources and performance . all , Generally, the number of records returned is greater than 100 Ten thousand hour use , The effect will also be obvious .
grammar
/+parallel(table_short_name,cash_number)/
This can be added to insert、delete、update、select To use ( and rule It's almost the same , Have a chance to share rule Usage of )
Turn on parallel The function statement is :
alter session enable parallel dml;
This sentence is DML Sentences , If used in the program , use execute Method on .
Parallel It can also be used for multiple tables
If there are many tables , It's behind the first , Just add others . The specific writing method is as follows :
/+parallel(t,10) (b,10)/
Summary
About execution efficiency , It is suggested to follow index Methods to improve the effect .Oracle It has its own explan road Methods , Before execution , Take a look at the implementation plan route first , Yes, written SQL tuned We'll do it later . There is no way out , Reuse parallel Method .Parallel More evil , For developers , It's not a good thing , Will form bad habits , Lead to many bad SQL It won't leak ,SQL Tuning Your ability cannot be improved . I have met some people create table after , never create index or primary key, Think of writing SQL Time plus parallel That's all right. .
There is something wrong , Welcome to the discussion .
Last , Welcome to pay attention to my wechat , What do you like , Collection , Forwarding is my greatest encouragement .
边栏推荐
猜你喜欢
数博会精彩回顾 | 彰显科研实力,中创算力荣获数字化影响力企业奖
Create a virtual machine on VMware (system not installed)
The American Championship is about to start. Are you ready?
MMAP学习
华为游戏多媒体服务调用屏蔽指定玩家语音方法,返回错误码3010
华为联机对战如何提升玩家匹配成功几率
Bitbucket installation configuration
Database tuning solution
PIP install beatifulsoup4 installation failed
Sentinel production environment practice (I)
随机推荐
Implementing Lmax disruptor queue from scratch (IV) principle analysis of multithreaded producer multiproducersequencer
Learning of mall permission module
华为游戏多媒体调用切换房间方法出现异常Internal system error. Reason:90000017
U盘的文件无法删除文件怎么办?Win11无法删除U盘文件解决教程
Pointer parameter passing vs reference parameter passing vs value parameter passing
Did you brush the real title of the blue bridge cup over the years? Come here and teach you to counter attack!
Sentinel production environment practice (I)
Reptile practice
Shell script, awk condition judgment and logic comparison &||
华为联机对战如何提升玩家匹配成功几率
Emotional analysis of wechat chat records on Valentine's day based on Text Mining
Summarize the reasons for 2XX, 3xx, 4xx, 5xx status codes
大约SQL现场“这包括”与“包括在”字符串的写法
Two stage locking protocol for concurrency control
Oracle checkpoint queue - Analysis of the principle of instance crash recovery
Database tuning solution
Create a virtual machine on VMware (system not installed)
PyGame practical project: write Snake games with 300 lines of code
Implementing Lmax disruptor queue from scratch (IV) principle analysis of multithreaded producer multiproducersequencer
ICMP introduction