DB2基礎知識三

 

<!--[if !supportLists]-->13.    <!--[endif]-->表關聯(關聯、外連接)

<?XML:NAMESPACE PREFIX = ST1 />Union

――交集運算:INTERSECTINTERSECT ALL

SELECT * FROM AA_WEEK X WHERE X.CODE IN (1,2,3) -- 集合A

INTERSECT -- 集合B

SELECT * FROM AA_WEEK X WHERE X.CODE IN (2,3,4);

---------------------------------

2 'Tuesday' 'Tues'

3 'Wednesday' 'Wed'

說明:INTERSECTINTERSECT ALL是等效的。

    ――差集運算:EXCEPTEXCEPT ALL

SELECT * FROM AA_WEEK X WHERE X.CODE IN (1,2,3) -- 集合A

EXCEPT -- 集合B

SELECT * FROM AA_WEEK w WHERE w.CODE IN (2,3,4);

說明:EXCEPTEXCEPT ALL是等效的。

---------------------------------

1 'Monday' 'Mon'

       ――合集運算:UNIONUNION ALL

<!--[if !supportLists]-->n       <!--[endif]-->求合集:A+B(不消除重複行)

SELECT * FROM AA_WEEK X WHERE X.CODE IN (1,2,3) -- 集合A

UNION ALL -- 集合B

SELECT * FROM AA_WEEK X WHERE X.CODE IN (2,3,4);

<!--[if !supportLists]-->n       <!--[endif]-->求合集:A+B(消除重複行)

SELECT * FROM AA_WEEK X WHERE X.CODE IN (1,2,3) -- 集合A

UNION -- 集合B

SELECT * FROM AA_WEEK X WHERE X.CODE IN (2,3,4);

---------------------------------

       Join

<!--[if !supportLists]-->n       <!--[endif]-->內連接:inner join在兩表共有的行纔會輸出,內連接沒有左右之分。

              select * from t1 inner join t2 on t1.c3=t2.c1

<!--[if !supportLists]-->n      <!--[endif]-->左外連接:left outer join左連接保留前面表的所有記錄,後表中沒有的補null

select * from t1 left outer join t2 on t1.c3=t2.c1

<!--[if !supportLists]-->n      <!--[endif]-->右外連接:right outer join右連接保留後表的所有記錄,前表中沒有的補null。在DB2的內部機制中,會把右外連接重寫成左外連接.故我們在寫sql語句時儘量使用左外連接。

select * from t1 right outer join t2 on t1.c3=t2.c1

<!--[if !supportLists]-->n      <!--[endif]-->全外連接:full outer join全外連接會輸出兩表的所有的數據,包括內連接和左外連接和右外連接的行。

select * from t1 full outer join t2 on t1.c3=t2.c1

<!--[if !supportLists]-->n      <!--[endif]-->總結:內連接,全有才有;左外連接,左有就有;右外連接,右有就有;全外連接,全都有。

<!--[if !supportLists]-->14.    <!--[endif]-->檢查LOCKTIMEOUT 的值

The default value for LOCKTIMEOUT is -1, which means that there will be no lock timeouts - a situation that can be catastrophic for OLTP applications. Nevertheless, I all too frequently find many DB2 users with LOCKTIMEOUT = -1. Set LOCKTIMEOUT to a very short value, such as 10 or 15 seconds. Waiting on locks for extended periods of time can have an avalanche effect on locks.

First, check the value of LOCKTIMEOUT with this command:

db2 "get db cfg for DBNAME"

and look for the line containing this text:

Lock timeout (sec) (LOCKTIMEOUT) = -1

If the value is -1, consider changing it to 15 seconds by using the following command (be sure to consult with the application developers or your vendor first to make sure the application is prepared to handle lock timeouts):

db2 "update db cfg for DBNAME using LOCKTIMEOUT 15"

You should also monitor the number of lock waits, lock wait time, and amount of lock list memory in use. Issue the command:

db2 "get snapshot for database on DBNAME"

Look for the following lines:

Locks held currently= 0  

Lock waits= 0  

Time database waited on locks (ms)= 0  

Lock list memory in use (Bytes)= 576  

Deadlocks detected= 0  

Lock escalations= 0  

Exclusive lock escalations= 0  

Agents currently waiting on locks= 0  

Lock Timeouts= 0

If the Lock list memory in use (Bytes) exceeds 50 percent of the defined LOCKLIST size, then increase the number of 4K pages in the LOCKLIST database configuration.

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章