473,387 Members | 3,810 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Basic Sql performance question

If I have two tables
create table tab1(col1 number, col2 varchar2(50));
create table tab2(col1 number, col2 varchar2(50));

insert into tab1 values (1, 'One');
insert into tab1 values (2, 'Two');
insert into tab1 values (3, 'Three');

insert into tab2 values (3, 'Three');
insert into tab2 values (4, 'Four');
insert into tab2 values (5, 'Five');

I want all tuples that are in tab1 but not in tab2. That is,
1, 'One' and 2, 'Two'
The ways I can do this are
1.
Select * from tab1 minus select * from tab2;
2.
Select t1.*
from tab1 t1
where not exists
(select col1 from tab2 where col1 = t1.col1);
3.
Select *
from tab1 t1
where col1 not in (select col1 from tab2);
4. Select t1.*
from tab1 t1, tab2 t2
where t1.col1 = t2.col1(+)
and t2.col1 is null

Which of these four methods is the fastest and in which circumstances?
Could someone rank them for me?
Is there a fifth, still better way?
Thanks,
DA Singh
Jul 19 '05 #1
2 3886
Hi...

It is not so simple to say which is the fastest.
How many rows are there on your tables?
Which is the average row lenght?
Are the tables indexed with some primary key or other indexes?
Is there a partitioning?

In this example the better is the first one, as the only access
possible is a FULL TABLE SCAN. In the first statement you get only 2
table scans, in other statements you get about (rows tab1)*(rows tab2)
table scans; hash join help you but the cost is expensive.

If you give me some more data i can try to send you a better answer.
Bye.

da*****@hotmail.com (DA Singh) wrote in message news:<78**************************@posting.google. com>...
If I have two tables
create table tab1(col1 number, col2 varchar2(50));
create table tab2(col1 number, col2 varchar2(50));

insert into tab1 values (1, 'One');
insert into tab1 values (2, 'Two');
insert into tab1 values (3, 'Three');

insert into tab2 values (3, 'Three');
insert into tab2 values (4, 'Four');
insert into tab2 values (5, 'Five');

I want all tuples that are in tab1 but not in tab2. That is,
1, 'One' and 2, 'Two'
The ways I can do this are
1.
Select * from tab1 minus select * from tab2;
2.
Select t1.*
from tab1 t1
where not exists
(select col1 from tab2 where col1 = t1.col1);
3.
Select *
from tab1 t1
where col1 not in (select col1 from tab2);
4. Select t1.*
from tab1 t1, tab2 t2
where t1.col1 = t2.col1(+)
and t2.col1 is null

Which of these four methods is the fastest and in which circumstances?
Could someone rank them for me?
Is there a fifth, still better way?
Thanks,
DA Singh

Jul 19 '05 #2
Thanks Mauro,
I was asked this in an interview. I think your answer is the answer
that guy was looking for.
Singh
mj***@libero.it (Mauro) wrote in message news:<a2**************************@posting.google. com>...
Hi...

It is not so simple to say which is the fastest.
How many rows are there on your tables?
Which is the average row lenght?
Are the tables indexed with some primary key or other indexes?
Is there a partitioning?

In this example the better is the first one, as the only access
possible is a FULL TABLE SCAN. In the first statement you get only 2
table scans, in other statements you get about (rows tab1)*(rows tab2)
table scans; hash join help you but the cost is expensive.

If you give me some more data i can try to send you a better answer.
Bye.

da*****@hotmail.com (DA Singh) wrote in message news:<78**************************@posting.google. com>...
If I have two tables
create table tab1(col1 number, col2 varchar2(50));
create table tab2(col1 number, col2 varchar2(50));

insert into tab1 values (1, 'One');
insert into tab1 values (2, 'Two');
insert into tab1 values (3, 'Three');

insert into tab2 values (3, 'Three');
insert into tab2 values (4, 'Four');
insert into tab2 values (5, 'Five');

I want all tuples that are in tab1 but not in tab2. That is,
1, 'One' and 2, 'Two'
The ways I can do this are
1.
Select * from tab1 minus select * from tab2;
2.
Select t1.*
from tab1 t1
where not exists
(select col1 from tab2 where col1 = t1.col1);
3.
Select *
from tab1 t1
where col1 not in (select col1 from tab2);
4. Select t1.*
from tab1 t1, tab2 t2
where t1.col1 = t2.col1(+)
and t2.col1 is null

Which of these four methods is the fastest and in which circumstances?
Could someone rank them for me?
Is there a fifth, still better way?
Thanks,
DA Singh

Jul 19 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Craig Stadler | last post by:
I have a fairly large database on my (2 million records) WIN32 mySQL 4.0.20 The question is: I am adding large amounts of data to it (300,000 to 500,000 at a time) with the standard (INSERT...
3
by: nick | last post by:
Hi, I'm pretty much a database beginner and have what I think is a basic question: If I have a table which has a 'status' column and I can have say three statuses: "active", "pending",...
2
by: Joel Vazquez | last post by:
Visual Basic.NET Application RunTime Crashes and Stalls Im a newbie if you could say in .NET ive been working with it the past 3 months and have done lots of things with it, without any prior...
2
by: Danny A. | last post by:
I am in the process of writing the middle tier for an application. This application will grab records, runn them through a process and save them in a table. In going through this process I'll have...
3
by: Sally Sally | last post by:
I have a very basic question on the two parameters shared buffers and effective cache size. I have read articles on what each is about etc. But I still think I don't quite grasp what these settings...
25
by: Jhon | last post by:
Hi every one, I got very basic question, here i go: Say i have 11001 11010 bits which are infact 10 bits. Now i want to address every bit so if it is zero i would add one and if it is one...
17
by: blueapricot416 | last post by:
This is a very basic question -- but I can't find the answer after looking for 20 minutes. If you code something like: function set_It() { setTimeout('Request_Complete("apple", -72)',5000) }...
4
by: sqlguy | last post by:
Why do we have to contact MS for a problem that has been with this compiler from at least the beta of VS 20005. I am so sick and tired of the 30 - 40 clicks it takes to dismiss VS when there is a...
19
by: frankiespark | last post by:
Hello all, I was perusing the internet for information on threading when I came across this group. Since there seems to be a lot of good ideas and useful info I thought I'd pose a question. ...
6
by: Ken Fine | last post by:
This is a basic question. What is the difference between casting and using the Convert.ToXXX methods, from the standpoint of the compiler, in terms of performance, and in other ways? e.g. ...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.