How does a SQL query work

Hi all...
How does a SQL query basically work? Is there any pseudocode for it?
How ro calculate the cost of query?
Can we find out what would be the execution time for a query before it is executed?
Thanks in advance.
Ameya

Hi Avinash,
Look closely mate!!!
You have done set autotrace on... meaning you are executing the statement while i am doing set autotrace traceonly explain meaning the statement would never get executed actually.. oracle optimizer will just select the desired plan and will show the output..
You can even ESTIMATE how many rows the qeury will return before even executing the statement using the same above tech.. i will show you how:
The explain plan gives this -- as long as you are using the CBO (cost based
optimizer). Doesn't matter if you have 1 or 50 tables. The last part of the
plan is the estimate output.
consider:
SQL> set autotrace traceonly explain
SQL> select e1.*, e2.* from emp e1, emp e2;
Execution Plan
0 SELECT STATEMENT Optimizer=CHOOSE (Cost=15 Card=196 Bytes=12544)
1 0 MERGE JOIN (CARTESIAN) (Cost=15 Card=196 Bytes=12544)
2 1 TABLE ACCESS (FULL) OF 'EMP' (Cost=1 Card=14 Bytes=448)
3 1 SORT (JOIN) (Cost=14 Card=14 Bytes=448)
4 3 TABLE ACCESS (FULL) OF 'EMP' (Cost=1 Card=14 Bytes=448)
196 = 14 * 14 -- thats right....
SQL> select emp.ename, mgr.ename from
2 emp, emp mgr
3 where emp.mgr = mgr.empno;
Execution Plan
0 SELECT STATEMENT Optimizer=CHOOSE (Cost=5 Card=13 Bytes=208)
1 0 MERGE JOIN (Cost=5 Card=13 Bytes=208)
2 1 SORT (JOIN) (Cost=3 Card=14 Bytes=112)
3 2 TABLE ACCESS (FULL) OF 'EMP' (Cost=1 Card=14 Bytes=112)
4 1 SORT (JOIN) (Cost=3 Card=14 Bytes=112)
5 4 TABLE ACCESS (FULL) OF 'EMP' (Cost=1 Card=14 Bytes=112)
Here is estimated that about 13 rows would be returned...
SQL> select count(*) from emp;
Execution Plan
0 SELECT STATEMENT Optimizer=CHOOSE (Cost=1 Card=1)
1 0 SORT (AGGREGATE)
2 1 INDEX (FULL SCAN) OF 'EMP_PK' (UNIQUE) (Cost=1 Card=14)
one row...
SQL> select ename, dname
2 from emp, dept where emp.deptno = dept.deptno;
Execution Plan
0 SELECT STATEMENT Optimizer=CHOOSE (Cost=5 Card=14 Bytes=252)
1 0 NESTED LOOPS (Cost=5 Card=14 Bytes=252)
2 1 TABLE ACCESS (FULL) OF 'DEPT' (Cost=1 Card=4 Bytes=44)
3 1 TABLE ACCESS (FULL) OF 'EMP' (Cost=1 Card=14 Bytes=98)
14 rows...
1 select ename, dname
2* from emp, dept where emp.deptno = dept.deptno and emp.ename like '%A%'
SQL> /
Execution Plan
0 SELECT STATEMENT Optimizer=CHOOSE (Cost=2 Card=1 Bytes=18)
1 0 NESTED LOOPS (Cost=2 Card=1 Bytes=18)
2 1 TABLE ACCESS (FULL) OF 'EMP' (Cost=1 Card=1 Bytes=7)
3 1 TABLE ACCESS (BY INDEX ROWID) OF 'DEPT' (Cost=1 Card=4 Bytes=44)
4 3 INDEX (RANGE SCAN) OF 'DEPT_IDX' (NON-UNIQUE)
1 rows.....
and this was all done without executing the query. See EXPLAIN plan and the
PLAN_TABLE.

Similar Messages

  • How does the follwoing query work?

    Want to know how does the follwoign query work??
    For every row in flsp
    the subquery is going to get executed or is it just one time execution of the subquery??
    UPDATE flsp
    SET (DURATION, sdr, inr) =
    (SELECT SUM (tot_charges), SUM (tot_charges_sdr),
    SUM (tot_duration)
    FROM t
    WHERE t.sender_pmn = flsp.sender_pmn
    AND t.recipient_pmn = flsp.recipient_pmn
    AND t.imsi_min = flsp.imsi
    AND TRUNC (t.call_date) = TRUNC (flsp.call_date)
    AND t.call_type = flsp.call_type
    AND t.service_code = flsp.service_code
    GROUP BY sender_pmn,
    recipient_pmn,
    imsi_min,
    TRUNC (t.call_date),
    call_type);
    Edited by: user8731258 on Sep 27, 2010 2:54 AM

    user8731258 wrote:
    the global session table is going to hold data for one sessions where as the table flsp is going to hold data for the entire day.During a session there could be say 10 thousand records.Sounds like a bad idea.
    user8731258 wrote:
    ANd there are goign to aroung 100 such sessions.Continues to sound bad.
    user8731258 wrote:
    I have to update the flsp continually.Just got worse.
    user8731258 wrote:
    How do i make this fetch fast??This still makes no sense to me.
    Why are you moving all this data around and utilizing temporary tables instead of directly querying the underlying data on an 'as needed' basis?
    I think you have undertaken entirely the wrong technical approach to whatever business problem you are attempting to solve (though it's hard to say for sure as we still know little of what you've got going on).
    So back to step number one like i asked before.
    Can you explain (from the very beginning) WHY you are attempting this type of set up?

  • How does this LINQ query work with Option Infer Off?

    How should value and filtered be declared?  It doesn't work when I turn Option Infer Off.  Originally, it worked with Option Infer On. There was no Dim statement on the first line, but the Dim keyword was placed before the
    filtered = line.  As soon as I turned Option Infer Off, all sorts of errors showed up.  How should the code be adjusted?  (Option Strict is On.)
    Dim element, value, filtered As Integer
    Dim values() As Integer = {2, 9, 5, 0, 3, 7, 1, 4, 8, 6}
    filtered =
    From value In values
    Where (value > 4)
    Select value
    For Each element In filtered
    Console.Write(element & " ")
    Next
    Solitaire

    Hi Solitaire,
    the Option Infer Off version would be:
    Option Strict On
    Option Infer Off
    Imports System
    Imports System.Collections.Generic
    Imports System.Linq
    Public Class Class1
    Public Shared Sub LinqWithNoInfer()
    Dim values() As Integer = {2, 9, 5, 0, 3, 7, 1, 4, 8, 6}
    ' compiled to
    ' filtered = values.Where(Function(value As Integer) value > 4).Select(Function(value As Integer) value)
    value)
    Dim filtered As IEnumerable(Of Integer) =
    From value In values
    Where (value > 4)
    Select value
    For Each element As Integer In filtered
    Console.Write(element & " ")
    Next
    End Sub
    End Class
    The type for filtered is determined by the Select statement, that is
    Enumerable.Select, and will return an
    IEnumerable(Of T) and T is an Integer here.
    Option Infer looks for the return value of Select and will give the same result.
    Note: For Lambda Expressions Option Infer Off is ignored and the type is inferred - both are valid:
    filtered = values.Where(Function(value) value > 4).Select(Function(value) value)
    filtered = values.Where(Function(value As Integer) value > 4).Select(Function(value As Integer) value)
    Regards, Elmar

  • How does business catalyst really work?

    So i've tried to reach this and cant seem to find answers in plain english.... How does Business Catalyst really work?.. functionally.
    1) I have a hosting service with godaddy and jumpline, I have a lot of space and do all of my client's websites through the space I have with those two plans, do they offer the business catalyst format? Just buy that platform with them or have it added?
    2) I have CC, business catalyst "comes with" it.... how does that work? Am I given space on someone elses server somewhere or do I just have the ability to upload the BC software onto a server I have... in which case could it be uploaded to godaddy or jumpline?
    3) The new MUSE features can only be done with business catalyst right?

    Hi,
    Business Catalyst is a web hosting platform that lets web designers host, build, and sell websites to clients. You use Muse to create and update a website. You use Business Catalyst to host it. A Muse subscription includes free hosting on Business Catalyst for one site. A Creative Cloud membership includes free hosting for five sites.
    Regarding your Creative Cloud Subscription, with BC. You must install and use Muse/Dreamweaver to publish your free sites. For More details, Please refer to the article below
    http://helpx.adobe.com/business-catalyst/using/business-catalyst-muse-users.html
    The new Muse feature, like in Browser editing is compatible only with Business Catalyst, but the other feature like Parallex Scrolling works on all the other hosting as well, like Godaddy.
    If you still have any query, please let us know and we will try to resolve it.
    Thanks.

  • How to write sql query to parse xml?

    Hi, I am new to this parsing xml. I need to extract value from xml using sql query. Please find the sample xml file. Kindly help me.
    <?xml version="1.0" encoding="UTF-8"?>
    <MaterialUpdates>
      <Materials>
         <SalesOrg>
           <ID>KAR12</ID>
         </SalesOrg>
    </Materials>
    </MaterialUpdates>
    I need to extratct ID value : /MaterialUpdates/Materials/SalesOrg/ID

    Thanks once again, it is working as per your suggestion. One last question .. how about in case of there is multiple nodes.
    Please find sample xml below:
    <?xml version="1.0" encoding="UTF-8"?>
    <MaterialUpdates>
      <Materials>
         <SalesOrg>
           <ID>KAR12</ID>
         </SalesOrg>
         <SalesOrg>
           <ID>KAR13</ID>
         </SalesOrg>
         <SalesOrg>
           <ID>KAR14</ID>
         </SalesOrg>
    </Materials>
    </MaterialUpdates>
    The above mentioned sql query works fine when there is one occurance of SalesOrg - ID, but fails when there is multiple occurance.

  • How to write sql query for counting pairs from below table??

    Below is my SQL table structure.
    user_id | Name | join_side | left_leg | right_leg | Parent_id
    100001 Tinku Left 100002 100003 0
    100002 Harish Left 100004 100005 100001
    100003 Gorav Right 100006 100007 100001
    100004 Prince Left 100008 NULL 100002
    100005 Ajay Right NULL NULL 100002
    100006 Simran Left NULL NULL 100003
    100007 Raman Right NULL NULL 100003
    100008 Vijay Left NULL NULL 100004
    It is a binary table structure.. Every user has to add two per id under him, one is left_leg and second is right_leg... Parent_id is under which user current user is added.. Hope you will be understand..
    I have to write sql query for counting pairs under id "100001". i know there will be important role of parent_id for counting pairs. * what is pair( suppose if any user contains  both left_leg and right_leg id, then it is called pair.)
    I know there are three pairs under id "100001" :-
    1.  100002 and 100003
    2.  100004 and 100005
    3.  100006 and 100007
        100008 will not be counted as pair because it does not have right leg..
     But i dont know how to write sql query for this... Any help will be appreciated... This is my college project... And tommorow is the last date of submission.... Hope anyone will help me...
    Suppose i have to count pair for id '100002'. Then there is only one pair under id '100002'. i.e 100004 and 100005

    Sounds like this to me
    DECLARE @ID int
    SET @ID = 100001--your passed value
    SELECT left_leg,right_leg
    FROM table
    WHERE (user_id = @ID
    OR parent_id = @ID)
    AND left_leg IS NOT NULL
    AND right_leg IS NOT NULL
    Please Mark This As Answer if it helps to solve the issue Visakh ---------------------------- http://visakhm.blogspot.com/ https://www.facebook.com/VmBlogs

  • How to transport SQL Query from SQVI Tcode?

    Dear Friends,
    Can any one tell me how to transport SQL Query from <b>SQVI</b> Tcode.
    Full Points will be rewarded.
    Thanks & Reagrds
    Ravi

    go to sqvi tool .....
    select u r query name ......in the menubar ....quickview....> additianal functions.......>generate program
    after doing generate program ....go to again same menu path as i have mentioned above [quickview....> additianal functions.......>display report name it will give the report name of the select query ..........copy the report name and give it abap editor[se38] u will get u r query program......with all authority checks.....
    in start of selection event of u r program u will find u r select query.
    reward points if helpful

  • How will write SQL query to fetch data from  each Sub-partition..

    Hi All,
    Anyone does have any idea about How to write SQL query to fetch data from Sub-partition.
    Actually i have one table having composite paritition(Range+list)
    Now if i want to fetch data from main partition(Range) the query will be
    SELECT * FROM emp PARTITION(q1_2005);
    Now i want to fetch data at sub-partition level(List) .But i am not able to get any SQL query for that.
    Pls help me to sort out.
    Thanks in Advance.
    Anwar

    SELECT * FROM emp SUBPARTITION(sp1);

  • Is it possible to have your whole family on one apple id or is it better to have each person have there own? If each has their own does each id have to buy their own music and apps? How does find my iphone work with one apple id or two?

    Is it possible to have your whole family on one apple id or is it better to have each person have there own? If each has their own does each id have to buy their own music and apps? How does find my iphone work with one apple id or two? also I am going to be going off to college soon should I make an itunes id for my self and how will I get all the music from the old id?

    Is it possible to have your whole family on one apple id or is it better to have each person have there own?
    Yes, it is possible. 1 apple ID can be associated with up to 10 devices.
    If each has their own does each id have to buy their own music and apps?
    Yes, all purchases are non-transferable.
    How does find my iphone work with one apple id or two?
    Every device associated with one apple ID through Find my iPhone is tied to that Apple ID; Find my iPhone will work in the same way with up to ten devices associated with one apple ID. You cannot enable Find my iPhone for one device across two apple IDs
    I am going to be going off to college soon should I make an itunes id for my self and how will I get all the music from the old id?
    If you have authorized a computer with the old apple ID, you can transfer old media purchased through the old to other devices via iTunes. This doesn't mean the media purchases through the old apple ID it transferred to the new account. If you plan to make future purchases and don't wish to share them with others, make your own apple ID.

  • How does this IMPORT statement works

    can any one tell me how does this statement will work..
    I am wokring on Solution manager system , where in there is a function module SSF_FIELD_LIST
    to which system passes form name.
      import fields to fieldlist
             from database stxfcontr(sf) id l_fullname.
    stxfcontr is a table which contains value of in  a diff
    Regards,
    mayank

    It will import data object stored in memory under named fields to your custom data object fieldlist .
    The table which it is stored is name stxfcontr under RELID (memory area) SF .
    Cluster key (key of the entry) in that table is behind var l_fullname
    Anyhow I think [this link|http://help.sap.com/saphelp_nw04/helpdata/en/fc/eb3bf8358411d1829f0000e829fbfe/frameset.htm] is more that any explanation. Once you study it, you will understand the above.
    Regards
    Marcin

  • How to use sql query in java ?

    i don't know how to use sql query in java code.
    who can give me some advice?
    thanks

    http://java.sun.com/developer/onlineTraining/Database/JDBC20Intro/

  • How does the event structure work & ...

    How does the event structure work & and how to modify the case example if you want to change the name of the case? I haven't a look at the manual but nothing about event structure is mentioned.

    I know how it works.. =P

  • How to Use SQL Query having IN Clause With DB Adapter

    Hi,
    I am using 11.1.1.5 want to find out how to Use SQL Query having IN Clause With DB Adapter. I want to pass the IN values dynamically. Any ideas.
    Thanks

    invoke a stored procedure, it's safer than trying to put together an arbitrary SQL statement in the JCA adapter

  • How does "notify me" actually work in mail app

    Might be a daft question, but how does "notify me" actually work in iPhone email app?
    I get the principal but surely when you get a reply thats already noticed you?
    Could someone advise me how it actually notifies and is it in addition to receiving the reply to the mail?

    I have the columns in place. But I have a few questions -
    1. Iis there some kind of audit trail report on the updates to my record to the table? If so how do I get to see?
    2. Am I expected to create a <entity name>_HISTORY table for this to work? I see some such recommendation  here .

  • How does mix and match works in ECC6.0?

    How does mix and match works in ECC 6.0?
    Can some shared on this topic?
    Thanks in advance

    It;s a term used in Retail for combination of sales item eg buy 3 get 1 any other  @ xx price.

Maybe you are looking for

  • TS3694 Problem in update OS 5 to OS 6 after update to Itunes 10.7 and Mac OS 10.8.2

    I updated the Mac OS to the 8.2 and now when I tried to update the OS 5 to the OS 6 I'm without my 2 devices the Iphone and the iPad. ITunes is asking me to restore the devices... Is anybody with the same problem?

  • Final Cut Pro 4 & Tiger

    I have Final Cut Pro 4 and was using OS X 3. I upgraded to OSX 4.2 Tiger, I didn't do anything else or remove anything, and now I can't use Final Cut Pro. Everytime I click on the program I get a configuration error which reads: "This software requir

  • Modeling for reporting sales

    There is a requirement to report how much a sales representative sell? What are some of the approaches to do this in BI?

  • Pages is corrupting my files

    I saved a file, simple text recently. When I tried to open it a few months later it states that the file cannot be opened and may be corrupted or in a different format. I cannot preview the file or open it in textedit or any other program. Any sugges

  • Hai any one can help by give infromation on abap-hr

    WHAT IS Payroll and ESS/MSS? PLZ POST INFROMATION AS SOON AS POSSIBLE. ADV THANKS.