Subquery v/s inner join in sql server

后端 未结 6 1482
伪装坚强ぢ
伪装坚强ぢ 2020-11-30 06:15

I have following queries

First one using inner join

SELECT item_ID,item_Code,item_Name 
FROM [Pharmacy].[tblitemHdr] I 
    INNER JOIN  EMR.tblFavour         


        
相关标签:
6条回答
  • 2020-11-30 06:51

    Sub-query Vs Join

    Table one 20 rows,2 cols

    Table two 20 rows,2 cols

    sub-query 20*20

    join 20*2

    logical, rectify

    Detailed

    The scan count indicates multiplication effect as the system will have to go through again and again to fetch data, for your performance measure, just look at the time

    0 讨论(0)
  • 2020-11-30 06:51

    First query is better than second query.. because first query we are joining both table. and also check the explain plan for both queries...

    0 讨论(0)
  • 2020-11-30 06:52

    Usually joins will work faster than inner queries, but in reality it will depend on the execution plan generated by SQL Server. No matter how you write your query, SQL Server will always transform it on an execution plan. If it is "smart" enough to generate the same plan from both queries, you will get the same result.

    Here and here some links to help.

    0 讨论(0)
  • 2020-11-30 06:59

    join is faster than subquery.

    subquery makes for busy disk access, think of hard disk's read-write needle(head?) that goes back and forth when it access: User, SearchExpression, PageSize, DrilldownPageSize, User, SearchExpression, PageSize, DrilldownPageSize, User... and so on.

    join works by concentrating the operation on the result of the first two tables, any subsequent joins would concentrate joining on the in-memory(or cached to disk) result of the first joined tables, and so on. less read-write needle movement, thus faster

    Source: Here

    0 讨论(0)
  • 2020-11-30 07:01

    It all depends on data and relational mapping between tables. If RDBMS rules are not followed then even the first query will be slow on execution and data fetching.

    0 讨论(0)
  • 2020-11-30 07:09

    In Sql Server Management Studio you can enable "Client Statistics" and also Include Actual Execution Plan. This will give you the ability to know precisely the execution time and load of each request.

    Also between each request clean the cache to avoid cache side effect on performance

    USE <YOURDATABASENAME>;
    GO
    CHECKPOINT;
    GO
    DBCC DROPCLEANBUFFERS;
    GO
    

    I think it's always best to see with our own eyes than relying on theory !

    0 讨论(0)
提交回复
热议问题