English 中文(简体)
使用子类的绩效考虑
原标题:Performance considerations of using subqueries

页: 1

 ------------------------------
|           employee           |
 ------------------------------
| employee_id  | employee_name |  
 ------------------------------
|     1        |     one       |
|     2        |     two       |
|     3        |     three     |
 ------------------------------

以及

 -------------------------------------------
|                feedback                   |
 -------------------------------------------
| employee_id (FK)  |       comments        |  
 -------------------------------------------
|         2         |     comment two       |
 -------------------------------------------

遣返所有雇员的最佳(业绩)方式是: 他们是否得到反馈?

我想到的是以下表格,但由于它使用分类,我不清楚这两个表格的记录数量在增加时会如何加快。

SELECT * FROM employee WHERE employee_id NOT IN (SELECT employee_id FROM feedback)

The database is Oracle 以及 all key columns have indexes.

Update

感谢大家,我希望我接受不止一个答案! 这是我在最后使用的(我与其他几张表格一样,我的表结构非常简单。

SELECT 
    e.name, m.name, a.postcode 
FROM 
    employee LEFT OUTER JOIN feedback f on (e.employee_id = f.employee_id),
    address a, manager m 
WHERE a.address_id = e.address_id
AND m.manager_id = e.manager_id
AND f.employee_id IS NULL
最佳回答

You could use:

SELECT e.* 
  FROM employee e 
  LEFT OUTER JOIN feedback f ON (e.employee_id = f.employee_id)
 WHERE f.employee_id IS NULL 

这样做应当非常好。 我假定ESMPLOYEE-ID栏的索引为......

Try it and see what your explain plan looks like.

EDIT: As you have said you do not have the PLAN table then this article from Tom Kyte (Oracle VP) is useful: http://asktom.oracle.com/pls/asktom/f?p=100:11:0::::P11_QUESTION_ID:313616750808

它建议说明为什么每一种解决办法(NOT IN, NOT EXISTS, OUTER JOIN)在当时情况下可能更好。

There is also this from the prolific Don Burleson: http://www.dba-oracle.com/oracle_tips_subq_rewrite.htm

问题回答

Try to use different queries and compare theirs plans. one more possibility to implement youe query:

select EMPLOYEE_ID from employee
MINUS
select EMPLOYEE_ID from FEEDBACK

The best way to be certain is to try each method with the relevant data - I suggest using not exists:

select * from EMPLOYEE e 
where not exists
(select null from FEEDBACK f where e.EMPLOYEE_ID = f.EMPLOYEE_ID)

明显:

SELECT e.* FROM EMPLOYEE e
LEFT OUTER JOIN FEEDBACK f ON e.EMPLOYEE_ID = f.EMPLOYEE_ID
WHERE f.EMPLOYEE_ID IS NULL

But you can read THIS article about it!





相关问题
Export tables from SQL Server to be imported to Oracle 10g

I m trying to export some tables from SQL Server 2005 and then create those tables and populate them in Oracle. I have about 10 tables, varying from 4 columns up to 25. I m not using any constraints/...

Connecting to Oracle 10g with ODBC from Excel VBA

The following code works. the connection opens fine but recordset.recordCount always returns -1 when there is data in the table. ANd If I try to call any methods/properties on recordset it crashes ...

How to make a one to one left outer join?

I was wondering, is there a way to make a kind of one to one left outer join: I need a join that matches say table A with table B, for each record on table A it must search for its pair on table B, ...

Insert if not exists Oracle

I need to be able to run an Oracle query which goes to insert a number of rows, but it also checks to see if a primary key exists and if it does, then it skips that insert. Something like: INSERT ALL ...

How can I store NULLs in NOT NULL field?

I just came across NULL values in NOT-NULL fields in our test database. How could they get there? I know that NOT-NULL constraints can be altered with NOVALIDATE clause, but that would change table s ...

Type reference scope

I m studying databases and am currently working on a object-relational DB project and I ve encountered a small problem with the number of possible constraints in an object table. I m using "Database ...

OracleParameter and DBNull.Value

we have a table in an Oracle Database which contains a column with the type Char(3 Byte). Now we use a parameterized sql to select some rows with a DBNull.Value and it doesn t work: OracleCommand ...

热门标签