Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Performance: Subquery or Joining

Tags:

sql

oracle

I got a little question about performance of a subquery / joining another table

INSERT
INTO Original.Person
  (
    PID, Name, Surname, SID
  )
  (
    SELECT ma.PID_new , TBL.Name , ma.Surname, TBL.SID 
    FROM Copy.Person TBL , original.MATabelle MA
    WHERE TBL.PID         = p_PID_old
      AND TBL.PID         = MA.PID_old
  );

This is my SQL, now this thing runs around 1 million times or more. My question is what would be faster?

  • If I change TBL.SID to (Select new from helptable where old = tbl.sid)

OR

  • If I add the 'HelpTable' to the from and do the joining in the where?

edit1
Well, this script runs only as much as there r persons.

My program has 2 modules one that populates MaTabelle and one that transfers data. This program does merge 2 databases together and coz of this, sometimes the same Key is used.
Now I'm working on a solution that no duplicate Keys exists.

My solution is to make a 'HelpTable'. The owner of the key(SID) generates a new key and writes it into a 'HelpTable'. All other tables that use this key can read it from the 'HelpTable'.

edit2
Just got something in my mind:
if a table as a Key that can be null(foreignkey that is not linked) then this won't work with the from or?

like image 588
domiSchenk Avatar asked May 20 '10 06:05

domiSchenk


People also ask

Which is faster join or subquery?

The advantage of a join includes that it executes faster. The retrieval time of the query using joins almost always will be faster than that of a subquery. By using joins, you can maximize the calculation burden on the database i.e., instead of multiple queries using one join query.

Should I use subquery or join?

If you need to combine related information from different rows within a table, then you can join the table with itself. Use subqueries when the result that you want requires more than one query and each subquery provides a subset of the table involved in the query.

Why subquery is better than join?

The more data tables have, the subqueries are slower. The less data tables have, the subqueries have equivalent speed as joins. The subqueries are simpler, easier to understand, and easier to read.

Does subquery reduce performance?

A Sub-Query Does Not Hurt Performance.


2 Answers

Modern RDBMs, including Oracle, optimize most joins and sub queries down to the same execution plan.

Therefore, I would go ahead and write your query in the way that is simplest for you and focus on ensuring that you've fully optimized your indexes.

If you provide your final query and your database schema, we might be able to offer detailed suggestions, including information regarding potential locking issues.

Edit

Here are some general tips that apply to your query:

  • For joins, ensure that you have an index on the columns that you are joining on. Be sure to apply an index to the joined columns in both tables. You might think you only need the index in one direction, but you should index both, since sometimes the database determines that it's better to join in the opposite direction.
  • For WHERE clauses, ensure that you have indexes on the columns mentioned in the WHERE.
  • For inserting many rows, it's best if you can insert them all in a single query.
  • For inserting on a table with a clustered index, it's best if you insert with incremental values for the clustered index so that the new rows are appended to the end of the data. This avoids rebuilding the index and often avoids locks on the existing records, which would slow down SELECT queries against existing rows. Basically, inserts become less painful to other users of the system.
like image 102
Marcus Adams Avatar answered Sep 29 '22 00:09

Marcus Adams


Joining would be much faster than a subquery

like image 22
ovais.tariq Avatar answered Sep 28 '22 22:09

ovais.tariq