Performance: Subquery or Joining

前端 未结 4 1869
悲哀的现实
悲哀的现实 2021-01-06 03:40

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

INSERT
INTO Original.Person
  (
    PID, Name, Surname, SID
  )
  (
    SELEC         


        
相关标签:
4条回答
  • 2021-01-06 04:08

    Joining would be much faster than a subquery

    0 讨论(0)
  • 2021-01-06 04:12

    The main difference betwen subquery and join is subquery is faster when we have to retrieve data from large number of tables.Because it becomes tedious to join more tables. join is faster to retrieve data from database when we have less number of tables.

    Also, this joins vs subquery can give you some more info

    0 讨论(0)
  • 2021-01-06 04:19

    Instead of focussing on whether to use join or subquery, I would focus on the necessity of doing 1,000,000 executions of that particular insert statement. Especially as Oracle's optimizer -as Marcus Adams already pointed out- will optimize and rewrite your statements under the covers to its most optimal form.

    Are you populating MaTabelle 1,000,000 times with only a few rows and issue that statement? If yes, then the answer is to do it in one shot. Can you provide some more information on your process that is executing this statement so many times?

    EDIT: You indicate that this insert statement is executed for every person. In that case the advice is to populate MATabelle first and then execute once:

    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         = MA.PID_old 
      );
    

    Regards, Rob.

    0 讨论(0)
  • 2021-01-06 04:26

    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.
    0 讨论(0)
提交回复
热议问题