当前位置:网站首页>Some suggestions on Oracle SQL query return optimization

Some suggestions on Oracle SQL query return optimization

2022-06-22 07:59:00 CodeStorys

A friend asked : If a query returns data too slowly , Batch query is required , It should be checked !

Now let's talk about... Based on experience sql Some experience of query , There are no special reference books , Where do you want to write , If you don't , Please correct me !
stay Oracle Up operation SQL when , First of all, you should know how to analyze the execution plan . It's usually F5 You can enter the execution plan window . For simple queries , We can easily tell where COST more ,COST Larger steps , That is, the steps that affect query efficiency .
But for a beginner , I don't know what the implementation plan is , Then how to start learning to write better sql Well ? Theory we don't have , Is there any way to try bailing ?
In general , Optimize data storage , Improving execution efficiency is a common method , The general methods are as follows :

1、 Crop partition table

Partition large tables , The most common is to partition by time . When querying data , First confirm whether the queried table is a partitioned table , If it is , Just use the partition field as the partition clipping condition , That is to say where clause , Using partitioned fields , You can automatically crop the partition . such , Only query the data of one or more partitions , The amount of data returned is less , Improve query efficiency . About partition clipping , There are also assignments partition The way , If you are interested, just Baidu .

2、 Use index

Index a table , Is the key to improve query efficiency . For queries , The more indexes, the better , For write , The fewer indexes, the better . Building an index is like building a directory of dictionaries . With the index , Then you need to look at the table structure , See which fields are index fields . Without affecting the query results , Use index as where Condition of clause , Can improve index performance .
Be careful , Index and partition cropping , To ensure that the index where The conditional character type on the clause is consistent with it , And no conversion , Otherwise, neither index nor partition pruning will take effect .
Whether the execution process is indexed , You can know by viewing the execution plan .

3、 Limit the amount of data returned

In the case of low requirements for data return , The required data can be returned randomly , You can use the rownum Limit the number of data returned .rownum You can use Baidu .

4、 utilize hint Specify parallelism ( Or prompt to execute the plan )

hint It can be used as sql Execution plan prompter . It's written in :

select /*+ parallel(8)*/  *  from table_name  ;

among ,parallel It means parallelism , That is, how many CPU The kernel runs this sql
Use here 8 Is a common number , It can also be used. 2 ,4 ,16 , It is best to 2 Power of power , Don't use odd numbers .

thus , When you look up data , The return result will be much faster . So simple and rude , It's more suitable for beginners . And for optimization , In fact, the most important thing is business optimization , Reducing the repeated scanning of tables has a great gain for large tables .
As for what some people have said exists Instead of not in , use union all Instead of or ,where The order in which clauses are written , These are all CBO I have already made it for you . It should be said that it is a means of low income .
I believe the above 4 A way , Should be able to walk a flat Oracle The road to learning .
Poorly written , Not a professional document , If there is something wrong, please correct it , thank !

==============================================

A day later , The friend said , Still don't understand .

I don't understand , She said :
still select The process of , Then he will remind me that I have reached the maximum amount , Do you want to continue
 Insert picture description here
In fact, this is not because the data return is slow , But the memory burst .
Now , Should be able to use swap Swap partitions .
We need to know , What is shown to us , Are provided by memory , All data must be read into memory , And then through CPU Calculate , Reuse GPU Show us .
If the amount of data is too large , You want to stack it in memory at one time , How can memory stand .
The server memory may be a total of 128G, Sometimes a result set may contain hundreds of millions of data , Press 0.3KB One , The calculator calculates , One hundred million of them 28GB, Even if all the memory is for you , You can also export 5 A table of billion pieces of data stays in memory .
It's almost unthinkable .
therefore , Don't expect all the data to be presented in the tool , It doesn't make any sense .
If you need to look at specific data , Limit the corresponding conditions .

=================================================
however , I need to export to Excel Send it to someone else .
There are three suggestions :
1) If the amount of data is too large (5w More than ), It is not recommended to export to at one time Excel, Although I have seen 8 Ten thousand pieces sheet Of Excel surface , But it is already too laggy and too laggy to use . Now , Can be displayed rownum Return quantity , Export in batches .
2) Do you want all the columns ? Or just a few columns , You write down the columns you need , Do not use * Replace all columns , This reduces the number of data block returns .
3) Use ROWNUM Limit the amount of data returned . Are you sure you want all the data , Or do you only need some data for field analysis , Use rownum <= 100 The way , Allows you to export a specified number of rows of data .

Refer to the following code , Used row_number Perform windowing sorting , Then by sorting the results , Export the specified data row number range .

select * from (
SELECT Ffv.Flex_Value
      ,Ffv.Description
	  ,row_number()over(partition by 1 order by 1 ) rn 
  FROM apps.table111     Fifs
      ,apps.table222      Ffv
      ,apps.table333     Fvs
 WHERE Fifs.Application_Column_Name = 'SEGMENT1'
   AND Fifs.Id_Flex_Code = 'GL#'
   AND Fifs.Flex_Value_Set_Id = Ffv.Flex_Value_Set_Id
   AND Ffv.Flex_Value_Set_Id = Fvs.Flex_Value_Set_Id(+)
   AND Ffv.Enabled_Flag = 'Y'
   AND Fvs.Flex_Value_Set_Name = Fifs.Segment_Name
   AND Nvl(Trunc(Ffv.Start_Date_Active)
          ,SYSDATE - 1) <= SYSDATE
   AND Nvl(Trunc(Ffv.End_Date_Active)
          ,SYSDATE + 1) >= SYSDATE ) 
		  where rn between 1 and  10000

Just limit where rn between 1 and 10000 The article number , You can export data at a time .

原网站

版权声明
本文为[CodeStorys]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/02/202202220530548114.html