- You should does it in a single SQL statement if at all Possi ble. And believe it or not, it's almost always possible.
- If You can ' t does it in a single SQL statement, does it in PL/SQL – as Little PL/SQL as possible! Follow the saying that goes "more code = more bugs, less code = less bugs."
- If you can ' t do it in PL/SQL, try a Java stored procedure. The itmes is necessary be extremely rare nowadays with oracle9i and above.
- If you can ' t do it in Java, does it in a C external procedure. This was the most frequently of the approach when raw speed or using a Third-parth APIs written in C is needed.
- If you can ' t do it in C external routine, you might want to seriously think on why it's you need to do it.–-or master Funny.
Basically 95% of the problems can be solved by SQL, PL/SQL.
It seems that even developer need to understand Oracle's internal principles, architecture, and implementation. Otherwise, Oracle is a "black box," and when you write SQL statements, problems can occur.
With the database, you need to be aware of what it works but you don ' t has to know everything inside and out.
Bitmap Index:bitmap such as gender, if you use the bitmap index, when you insert a new record, such as the record you inserted in this column value is n, then all of the columns in this table is n the column will be lock, so this time, such as If someone wants to modify N to Y, then when the lock is added, it will be found that the lock is already occupied, thus entering the wait.
Note: I'll use autonomous transactions (autonomous business) throughout (run through) This book to demonstrate locking, blocking, and con Currency issues. It is my firm belief, autonomous transactions is a feature, Oracle should not having exposed to developers. Beyond using them as a demonstration tool, autonomous transactions has exactly one other use-as an error-logging Mechanis M.
It appears that this autonomous transaction can only be used in the following places:
1) error-logging: The error is logged, the error message is saved in a table and commit it, and the commit of an autonomous transaction does not affect other transactions.
2) Demonstrating concept: that is used to demonstrate some concepts, just demo, not in the production library. (for example, a two-thing lock problem)
Bit-map index Issues, note that if you delete the bitmap index, the following experiment is successful
Create table T (test_flag varchar2 (1)); Create Index on into Values (' N '); declarepragma autonomous_transaction; begin into Values (' Y '); end;/
In fact, the above example, we create the B*tree index is very good, there is no problem.
When we created the index, we had to choose between the following approaches:
just create an index on the Processed-flag column.
Create an index-only in the Processed-flag column when the processed flag is N,
That's, only index the values of interest. We typically don ' t want to use an index
When the processed flag was Y since the vast majority of the records in the table has
The value Y. Notice that I do not say "We never want-use ..." You might want to
Very frequently count the number of processed records for some reason, and then
An index on the processed records might well come in very handy.
Above, only the index created when the value of the Test_flag column equals N.
CREATE index T_IDX on T (Decode (Processed_flag, ' n ', ' n '));
80% ~ 90% tuning is at the application level
--Index for functionCreate TableT (ID numberPrimary Key, Test_flag varchar2 (1), Payload varchar2 (20));Create IndexT_idx onT (Decode (Test_flag,' N ',' N '));--if Test_flag = = ' n ', return ' n 'Insert intoTSelectR Case whenMoD (r, 2) = 0 Then ' N ' Else ' Y ' End,' payload '|| R from(Select LevelR fromDualConnect by Level<= 5)/Select* fromTCreate orReplacefunctionGet_first_unlocked_rowreturnT%rowtype asResource_busyException; pragma exception_init (resource_busy,-54); L_rec T%rowtype;begin forXinch(SelectrowID RID fromTwhereDecode (Test_flag,' N ',' N ') =' N ') loopbegin Select* intoL_rec fromTwhererowID = X.rid andTest_flag =' N ' for UpdateNoWaitreturnL_rec;Exception whenResource_busy Then NULL; whenNo_data_found Then NULL;End;EndLoopreturn NULL;End;/--Test functionDeclareL_rec T%rowtype;beginL_rec: = Get_first_unlocked_row; Dbms_output.put_line (' I got row '|| L_rec.id | |', '|| L_rec.payload);Commit;End;/--This statement returns the result that 2DeclareL_rec T%rowtype;cursorC isSelect* fromTwhereDecode (Test_flag,' N ',' N ') =' N ' --Use decode function was for index for UpdateSkip locked;begin OpenCFetchC intoL_rec;if(c%found) ThenDbms_output.put_line (' I got row '|| L_rec.id | |', '|| L_rec.payload);End if;CloseCEnd;/-This statement also returns the result that 2DeclareL_rec T%rowtype;pragma autonomous_transaction;cursorC isSelect* fromTwhereDecode (Test_flag,' N ',' N ') =' N ' --Use decode function was for index for UpdateSkip locked; -If you do not use this statement, the entire process will be suspended, waiting forbegin OpenCFetchC intoL_rec;if(c%found) ThenDbms_output.put_line (' I got row '|| L_rec.id | |', '|| L_rec.payload);End if;CloseCCommit;EndThe result of the/--return is 4.How (and why) to Develop Database Applications1. Understanding Oracle Architecture2. Use of a single Connection in Oracle (multiple queries with 1 connections connnection)
Compare SQL Server,
In SQL Server It was a very common practice to open a connection to the database for each concurrent statement we want to Execute, If you is going to does five queries, you might well see five connections in SQL Server. But in Oracle, if you want to does five queries or five hundred, the maximum number of connections you want to open is one.
3. Use Bind Variables
The contrast between soft parsing and hard parsing, and the possibility of SQL injection (SQL injection) If you do not use bound variables, that is, when programmers write code, they do not judge the legitimacy of user input data, so that the application has a security risk.
Test SQL injection
/* * This program would testSQLInjection whenYou don' t use bind variable. */create or Replace procedure inj (P_date in date) as L_rec All_users%rowtype; c Sys_refcursor; L_query long;begin l_query: = ' Select* fromAll_userswhereCreated ="'' | | | p_date | | '"''; Dbms_output.put_line (L_query); Open C for L_query; For I in 1..5 Loop fetch C into L_rec; Exit when C%notfound; Dbms_output.put_line (L_rec.username | | '.....'); End Loop; Close c;end;/--want to SHOWEXEC inj (SYSDATE);--Show the dangerous part on this INJ procedurecreate table USER_PW ( Uname varchar2 (() primary key, PW VARCHAR2 (); INSERT into USER_PW (uname, PW) VALUES ('Tkyte', ' toSECRET'); commit;--now, some user don 't know USER_PWTableexist, simulation thisTable--+is very important.Grant Execute oninj toScott--So now, Scott connect the database, and do as belowAlter Session SetNls_date_format =' "'' Union Select tname, 0, null from tab--"';execLeon.inj (sysdate); */* theresult is: * ----------------------------------------- Select* fromAll_userswhereCreated ="'Union SelectTname, 0,NULL fromtab--' *-----------------------------------------* We know table information, some important table. */--in the This, SCO TT can see the table USER_PW (very important table) – now they want to try to select this important Table.select * from Leo N.USER_PW; --but they can not, because they don 't have privilege.Alter Session SetNls_date_format =' "'' Union Select Tname | | CNAME, 0, null from col--"';execLeon.inj (sysdate); */* theresult is: * ----------------------------------------- Select* fromAll_userswhereCreated ="'Union SelectTname | | CNAME, 0,NULL fromcol--' *-----------------------------------------* We know the column information in some important table. */--Use Bind Variable to pertect you.create or replace procedure Not_inj (p_date in date) as L_rec All_users%rowtype; s sys_refcursor; L_query long;begin l_query: = ' Select* fromAll_userswhereCreated =: X'; Dbms_output.put_line (L_query); Open C for l_query using P_date; For I in 1..5 Loop fetch C into L_rec; Exit when C%notfound; Dbms_output.put_line (L_rec.username | | '....‘);EndLoopCloseCEnd;/--Test Not_injexecNot_inj (sysdate); */* theresult is: * ----------------------------------------- Select* fromAll_userswhereCreated =: X *-----------------------------------------*/--so fromNow on, you must UseBindvariable. ^^4. Understanding Concurrency Control
Concurrency issues is the hardest to track down; The problem is similar-debugging a multithreaded program.
Locks is the mechanism that allows for concurrency.
If you or the database itself locks data unnecessarily, fewer people would be able to concurrently perform operations. Thus, understanding what locking was and how the it works in your database was vital if you were to develop a scalable, correct a Pplication.
What's also vital is so you understand it each database implements locking differently.
The following points sum up Oracle ' s locking policy:
Oracle Locks data at the row level on modification. There is no lock escalation to a block or table level.
Oracle never locks data just to read it. There is no locks placed on rows of the data by simple reads.
a writer of data does not block A reader of data. Let me repeat:reads is not blocked by writes. This was fundamentally different from many and other databases, where reads was blocked by writes. While the This sounds a extremely positive attribute (and it generally is), the if you does not understand this thoroughly and You attempt to enforce integrity constraints in your application via application logic, is most likely doing it Inco rrectly.
a writer of data is blocked if another writer of data has already locked the row it was going after. A reader of data never blocks a writer of data.
Developing successful Oracle applications