Select count(*) never ever ends

Hi,
I loaded a 22 million row table and added an index (non-unique) in a 10.2 database.
I then ran a 'Select count(*) from ' this table and it never ends. Same result for counts on any column. SQL Developer just hangs and other gui(s) never stop churning. Memory and disk are ok. The Explain Plan shows parallel aggregate steps and a full table scan. I used a no_parallel hint and it just did a full table scan but still ran forever. An index hint didn't help. Re-running stats had no effect either.
Any ideas?
Thanks,
Victor

And just to throw some real-world proof to that statement of John's, here is a copy-and-paste from a SQL*Plus session doing a select count against the largest table in a data warehouse - on 5+ years old server h/w by the way.
SQL> set timing on
SQL> select count(*) from vlt;
COUNT(*)
1316394373
Elapsed: 00:00:26.92
SQL>
1.3 billion rows counted in under 30 seconds.

Similar Messages

  • "select count(*) from dba_jobs_running" takes 5 minutes to return

    Hi,
    I login as sys or system from sql*plus to run query "select count(*) from dba_jobs_running". This query takes about 5 minutes to run.
    Querying other DBA_* views returns instantly, only this dba_jobs_running view is troubling.
    I have 11 records in dba_jobs, and 18 records in v$lock as usual.
    No error or warning in logfile, no trace file.
    What's possibly wrong with the server?
    Thanks,
    Harry

    Hi,
    The following are the exact statements I used to create the trace file:
    alter session set timed_statistics=true;
    alter session set max_dump_file_size=unlimited;
    alter session set events '10046 trace name context forever, level 12';
    select count(*) from dba_jobs_running;
    exit;
    The content of the trace file is attached at the end.
    If it is CPU bound, I have no activity on the db host. I am solely using it to analyze this problem.
    load averages: 0.02, 0.02, 0.07 22:16:38
    48 processes: 47 sleeping, 1 on cpu
    CPU states: 98.4% idle, 1.6% user, 0.0% kernel, 0.0% iowait, 0.0% swap
    Memory: 1792M real, 983M free, 517M swap in use, 2937M swap free
    PID USERNAME THR PRI NICE SIZE RES STATE TIME CPU COMMAND
    655 oracle 1 59 0 0K 0K sleep 1:14 0.32% oracle
    865 oracle 1 59 0 2072K 1184K cpu 0:00 0.14% top
    667 oracle 1 59 0 0K 0K sleep 0:13 0.08% oracle
    663 oracle 1 59 0 0K 0K sleep 0:27 0.07% oracle
    665 oracle 1 59 0 0K 0K sleep 0:15 0.02% oracle
    671 oracle 1 59 0 0K 0K sleep 0:00 0.02% oracle
    645 oracle 18 59 0 0K 0K sleep 0:23 0.01% oracle
    657 oracle 1 59 0 0K 0K sleep 0:14 0.00% oracle
    649 oracle 1 59 0 0K 0K sleep 0:02 0.00% oracle
    659 oracle 1 59 0 0K 0K sleep 0:01 0.00% oracle
    251 root 20 59 0 3256K 2512K sleep 0:00 0.00% nscd
    70 root 5 59 0 2864K 2096K sleep 0:00 0.00% picld
    224 root 3 59 0 3912K 2008K sleep 0:00 0.00% automountd
    271 root 1 59 0 4408K 1896K sleep 0:00 0.00% sendmail
    588 oracle 1 59 0 2600K 1864K sleep 0:00 0.00% bash
    Any help is greatly appreciated.
    <-- TRACE FILE CONTENT -->
    *** 2006-11-21 21:34:54.413
    *** SESSION ID:(21.832) 2006-11-21 21:34:54.412
    APPNAME mod='[email protected] (TNS V1-V3)' mh=0 act='' ah=0
    =====================
    PARSING IN CURSOR #1 len=69 dep=0 uid=0 oct=42 lid=0 tim=107123626140 hv=2004533713 ad='97219d80'
    alter session set events '10046 trace name context forever, level 12'
    END OF STMT
    EXEC #1:c=0,e=346,p=0,cr=0,cu=0,mis=1,r=0,dep=0,og=4,tim=107123625473
    WAIT #1: nam='SQL*Net message to client' ela= 11 p1=1650815232 p2=1 p3=0
    WAIT #1: nam='SQL*Net message from client' ela= 6476024 p1=1650815232 p2=1 p3=0
    =====================
    PARSING IN CURSOR #1 len=37 dep=0 uid=0 oct=3 lid=0 tim=107130104260 hv=2246554324 ad='97c21958'
    select count(*) from dba_jobs_running
    END OF STMT
    PARSE #1:c=0,e=548,p=0,cr=0,cu=0,mis=0,r=0,dep=0,og=4,tim=107130104237
    BINDS #1:
    EXEC #1:c=0,e=1412,p=0,cr=0,cu=0,mis=0,r=0,dep=0,og=4,tim=107130106034
    WAIT #1: nam='SQL*Net message to client' ela= 12 p1=1650815232 p2=1 p3=0
    *** 2006-11-21 21:40:53.931
    FETCH #1:c=350760000,e=344612113,p=0,cr=2,cu=16652,mis=0,r=1,dep=0,og=4,tim=107474718456
    WAIT #1: nam='SQL*Net message from client' ela= 1936 p1=1650815232 p2=1 p3=0
    FETCH #1:c=0,e=6,p=0,cr=0,cu=0,mis=0,r=0,dep=0,og=0,tim=107474721309
    WAIT #1: nam='SQL*Net message to client' ela= 8 p1=1650815232 p2=1 p3=0
    *** 2006-11-21 21:56:30.972
    WAIT #1: nam='SQL*Net message from client' ela= 915075702 p1=1650815232 p2=1 p3=0
    XCTEND rlbk=0, rd_only=1
    STAT #1 id=1 cnt=1 pid=0 pos=1 obj=0 op='SORT AGGREGATE (cr=2 r=0 w=0 time=100535896 us)'
    STAT #1 id=2 cnt=0 pid=1 pos=1 obj=0 op='NESTED LOOPS (cr=2 r=0 w=0 time=100535876 us)'
    STAT #1 id=3 cnt=5550 pid=2 pos=1 obj=0 op='MERGE JOIN CARTESIAN (cr=2 r=0 w=0 time=128639 us)'
    STAT #1 id=4 cnt=10 pid=3 pos=1 obj=0 op='NESTED LOOPS OUTER (cr=2 r=0 w=0 time=5839 us)'
    STAT #1 id=5 cnt=10 pid=4 pos=1 obj=69 op='FIXED TABLE FULL X$KSQRS (cr=0 r=0 w=0 time=4790 us)'
    STAT #1 id=6 cnt=10 pid=4 pos=2 obj=202 op='INDEX UNIQUE SCAN OBJ#(202) (cr=2 r=0 w=0 time=561 us)'
    STAT #1 id=7 cnt=5550 pid=3 pos=2 obj=0 op='BUFFER SORT (cr=0 r=0 w=0 time=90039 us)'
    STAT #1 id=8 cnt=555 pid=7 pos=1 obj=16 op='FIXED TABLE FULL X$KSUSE (cr=0 r=0 w=0 time=1277 us)'
    STAT #1 id=9 cnt=0 pid=2 pos=2 obj=0 op='VIEW (cr=0 r=0 w=0 time=344148954 us)'
    STAT #1 id=10 cnt=105477 pid=9 pos=1 obj=0 op='UNION-ALL (cr=0 r=0 w=0 time=343814331 us)'
    STAT #1 id=11 cnt=105476 pid=10 pos=1 obj=0 op='VIEW (cr=0 r=0 w=0 time=203487737 us)'
    STAT #1 id=12 cnt=105476 pid=11 pos=1 obj=0 op='UNION-ALL (cr=0 r=0 w=0 time=202961626 us)'
    STAT #1 id=13 cnt=0 pid=12 pos=1 obj=272 op='FIXED TABLE FULL X$KDNSSF (cr=0 r=0 w=0 time=13633786 us)'
    STAT #1 id=14 cnt=105476 pid=12 pos=2 obj=72 op='FIXED TABLE FULL X$KSQEQ (cr=0 r=0 w=0 time=188317145 us)'
    STAT #1 id=15 cnt=0 pid=10 pos=2 obj=253 op='FIXED TABLE FULL X$KTADM (cr=0 r=0 w=0 time=76301426 us)'
    STAT #1 id=16 cnt=1 pid=10 pos=3 obj=254 op='FIXED TABLE FULL X$KTCXB (cr=0 r=0 w=0 time=62693462 us)'
    STAT #1 id=16 cnt=1 pid=10 pos=3 obj=254 op='FIXED TABLE FULL X$KTCXB (cr=0 r=0 w=0 time=62693462 us)'
    <---- END of TRACE FILE -->

  • Select count(Key_Field) from table_name never returns

    I am doing some scalability tests and am building a db w/table that will contain 400Million records. I am trying to see how far along I am in the process, and am experiencing this problem.
    The select count(ID) never returns.
    Does anyone have any suggestions for me as to how i might make it perform better? I am not too familiar with such large scale implementations, so any advice would be appreciated, and you can feel free to tell me the most basic info... (I do read docs, etc, but there might be a place I have not yet searched..)
    The hardware is not an issue, at least for my testing, as it is as follows, and I see the IO on the arrays is busy, but not too busy, and that is to be expected, and we are neither CPU or RAM bound.
    SUN E4800
    12 CPUs
    48GB RAM
    2 T3 Arrays (fiber cards on different IO boards)
    4 internal drives (2 on each IO board), not mirrored or RAIDed

    This is always a troublesome type of query.
    Are you trying to count the number of all rows in a table? You can try to ANALYZE the table, then from user_tables select the NUM_ROWS
    I am doing some scalability tests and am building a db w/table that will contain 400Million records. I am trying to see how far along I am in the process, and am experiencing this problem.
    The select count(ID) never returns.
    Does anyone have any suggestions for me as to how i might make it perform better? I am not too familiar with such large scale implementations, so any advice would be appreciated, and you can feel free to tell me the most basic info... (I do read docs, etc, but there might be a place I have not yet searched..)
    The hardware is not an issue, at least for my testing, as it is as follows, and I see the IO on the arrays is busy, but not too busy, and that is to be expected, and we are neither CPU or RAM bound.
    SUN E4800
    12 CPUs
    48GB RAM
    2 T3 Arrays (fiber cards on different IO boards)
    4 internal drives (2 on each IO board), not mirrored or RAIDed

  • Performance issue when using select count on large tables

    Hello Experts,
    I have a requirement where i need to get count of data  from a database table.Later on i need to display the count in ALV format.
    As per my requirement, I have to use this select count inside a nested loops.
    Below is the count snippet:
    LOOP at systems assigning <fs_sc_systems>.
    LOOP at date assigning <fs_sc_date>.
    SELECT COUNT( DISTINCT crmd_orderadm_i~header )
       FROM crmd_orderadm_i
       INNER JOIN bbp_pdigp
           ON crmd_orderadm_iclient EQ bbp_pdigpclient               "MANDT is referred as client
         AND crmd_orderadm_iguid  EQ bbp_pdigpguid
         INTO w_sc_count
    WHERE crmd_orderadm_i~created_at BETWEEN <fs_sc_date>-start_timestamp
         AND <fs_sc_date>-end_timestamp
         AND bbp_pdigp~zz_scsys   EQ <fs_sc_systems>-sys_name.
    endloop.
    endloop.
    In the above code snippet,
    <fs_sc_systems>-sys_name is having the system name,
    <fs_sc_date>-start_timestamp is having the start date of month
    and <fs_sc_date>-end_timestamp is the end date of month.
    Also the data in tables crmd_orderadm_i and bbp_pdigp is very large and it increases every day.
    Now,the above select query is taking a lot of time to give the count due to which i am facing performance issues.
    Can any one pls help me out to optimize this code.
    Thanks,
    Suman

    Hi Choudhary Suman ,
    Try this:
    SELECT crmd_orderadm_i~header
      INTO it_header                 " interna table
      FROM crmd_orderadm_i
    INNER JOIN bbp_pdigp
        ON crmd_orderadm_iclient EQ bbp_pdigpclient
       AND crmd_orderadm_iguid   EQ bbp_pdigpguid
       FOR ALL ENTRIES IN date
    WHERE crmd_orderadm_i~created_at BETWEEN date-start_timestamp
                                          AND date-end_timestamp
       AND bbp_pdigp~zz_scsys EQ date-sys_name.
        SORT it_header BY header.
        DELETE ADJACENT DUPLICATES FROM it_header
        COMPARING header.
        describe table it_header lines v_lines.
    Hope this information is help to you.
    Regards,
    José

  • Sub-Select Count query breaking TOAD

    Oracle 10.2.0.4.0
    Running TOAD 9.1
    I am running some SQL on our eBusiness Suite:
    SELECT pha.segment1
         , pha.type_lookup_code
         , (SELECT COUNT(DISTINCT pha2.po_header_id)
              FROM po.po_headers_all pha2
                 , po.po_lines_all pla
             WHERE pha2.po_header_id = pla.po_header_id
               AND pla.contract_id = pha.po_header_id) po_count
         , (SELECT MAX(pha2.creation_date)
              FROM po.po_headers_all pha2
                 , po.po_lines_all pla
             WHERE pha2.po_header_id = pla.po_header_id
               AND pla.contract_id = pha.po_header_id) latest_cpa_po
      FROM po.po_headers_all pha
         , po.po_vendors pv
         , po.po_vendor_sites_all pvsa
    WHERE pha.vendor_id = pv.vendor_id
       AND pha.vendor_site_id = pvsa.vendor_site_id
    --   AND pv.VENDOR_NAME LIKE 'H%'
       AND pha.vendor_id = 98
       AND pha.type_lookup_code = 'CONTRACT'
       AND pha.org_id IN(7041, 7042);The above query runs quicky (approx. 1 second). If I take out the AND pha.vendor_id = 98 then the query takes a few minutes to run.
    When I try to export it, or scroll down to view > 500 rows, TOAD crashes.
    I know this isn't a TOAD forum, but I think that this is probably an issue with my no doubt rubbish SQL.
    If I take out this sub-select, then the problem doesn't happen:
         , (SELECT COUNT(DISTINCT pha2.po_header_id)
              FROM po.po_headers_all pha2
                 , po.po_lines_all pla
             WHERE pha2.po_header_id = pla.po_header_id
               AND pla.contract_id = pha.po_header_id) po_countHowever, I can't work out a better way of getting the data I need.
    The sub-select counts POs which have been raised where the contractID on the PO line is the same as the PO Header ID from the main query.
    Any advice please, on what I could do to sort this out would be much appreciated.
    Thanks!

    Hi,
    It looks like you can replace both scalar sub-queries with a join, like this:
    WITH     header_lines_summary     AS
         SELECT    pla.contract_id
              ,       COUNT (DISTINCT pha2.po_header_id)     AS po_count
              ,       MAX (pha2.creation_date)          AS latest_cpa_po
              FROM        po.po_headers_all pha2
                 ,        po.po_lines_all   pla
             WHERE        pha2.po_header_id = pla.po_header_id
          GROUP BY       pla.contract_id
    )                                        -- Everything up to this line is new
    SELECT pha.segment1
         , pha.type_lookup_code
         , hls.po_count                              -- Changed
         , hls.latest_cpa_po                         -- Changed
      FROM po.po_headers_all     pha
         , po.po_vendors           pv
         , po.po_vendor_sites_all      pvsa
         , header_lines_summary     hls                    -- New
    WHERE pha.vendor_id          = pv.vendor_id
       AND pha.vendor_site_id     = pvsa.vendor_site_id
       AND pha.po_header_id          = hls.contract_id (+)          -- New
    --   AND pv.VENDOR_NAME      LIKE 'H%'
       AND pha.vendor_id           = 98
       AND pha.type_lookup_code      = 'CONTRACT'
       AND pha.org_id           IN (7041, 7042);Aside from the sub-query (which is entirely new), the query above is just what you posted, with 2 lines changed and 2 lines added, as marked.
    This should be more efficient, but I don't know for certain that it will solve the Toad problem.
    I hope this answers your question.
    If not, post a little sample data (CREATE TABLE and INSERT statements, relevant columns only) for all tables, and also post the results you want from that data.
    It never hurts to say what version of Oracle you're using.

  • Select count(*) returns double?

    I have executed a 'select count(*) from ...' statement through ODBC against Oracle9i and I am very supprised that the returned ODBC type is a 'double' and not an 'int' (or a 'numeric').
    Is this really correct?

    Did you ever get a response or figure this out?
    My problem is fairly simliar, but specific to an 11.1.0.7 and onward release of the Oracle Client.
    Current C++ Application using ODBC API calling Oracle Client 10g (all versions), 11gR1 (v11.1.0.6) returns a LONG (4-byte integer) for "SELECT COUNT(*) from tablename"
    Change Oracle client to 11gR1 (v11.1.0.7) or higher (11.2.x.y etc) and that same statement returns a DOUBLE (8-byte floating value equivalent to oracle NUMBER data type).
    Since the backend DB is the same version, there must be a client side setting (I presume) that has changed the default behaviour of this Aggregate SQL function to return a double rather than a long. I'm hoping to find such a setting that I can either set programmatically through an ODBC API call, or in the Oracle Client configuration itself.
    I even tried using SQL CAST to make it an INT SELECT CAST(COUNT(*) AS INT) FROM tablename but that still returns a DOUBLE (8-byte floating NUMBER).
    Note: given that I use ODBC, I've written alot of generic C++ code supporting the return value as LONG, as that is how it's been for 10 years via ODBC. I'd like to maintain that if possible without having to write ORACLE specific code within my applications.

  • Select count(*) not working in Apex

    Hello,
    The following sql works in both SQL*Plus and Toad but not in apex.  The variable seems always to be 0 no matter what data I have on the table.
    Basically I try to flip the status from "Inactive" to "Active" only when there is no "Inactive" status left on the table.  Say there are 2 inactive statuses.
    If I delete one inactive status, the overall status should still be "Inactive".  However, with this code, it flips to "Active" status regardless. 
    I tried manually assign the variable to 1 and the status was not flipped.  Therefore, it sounds to me that the count(*) is not working in APEX.
    Does anyone experience this before? 
    Thanks in advance,
    Ning
    ===================================
    DECLARE
    v_status_count NUMBER;
    BEGIN
    UPDATE LGR_APP_STATUSES
    SET DELETED = 'Y'
    WHERE LGR_APP_STATUS_ID = :P42_LGR_APP_STATUS_ID;
    commit;
    select count(LGR_STATUS_ID) into v_status_count from LGR_APP_STATUSES
    where DELETED = 'N'
    and LGR_APPLICATION_ID = :p42_application_id
    and LGR_STATUS_ID in (3,8);
    IF (v_status_count = 0) THEN
    update lgr_applications
      set lgr_application_status = 'ACTIVE'
      where LGR_APPLICATION_ID = :P42_LGR_APPLICATION_ID;
    commit;
    END IF;
    END;

    Hi,
    In query you have used p42_application_id.
    Other statements use P42_LGR_APP_STATUS_ID
    Do you have that item? What is session state for that?
    Regards,
    Jari

  • What's wrong with my SELECT/COUNT(*) statement?

    I am trying to get a total count of records related to a single master record within one table, and if I test this in the DW recordset wizard, I get the results I need. By testing it, I mean if I enter something other than the default -1, where there are results related to another 'master record', then I end up with the correct amount of rows, but when I test it in a browser, all I am getting is '0' for all records.
    This bit of code is setting the 'master record ID' to which others will relate to:
    <?php $_GET['trcount'] = $row_rs_replycount['fld_fID'] ?>
    This is my recordset which is inline with the code which is inside a repeat region:
    <?php
    $threadreplys_rs_replycount = "3";
    if (isset($_GET['trcount'])) {
      $threadreplys_rs_replycount = $_GET['trcount'];
    mysql_select_db($database_conn_mrs, $conn_mrs);
    $query_rs_replycount = sprintf("SELECT *, COUNT(*) AS countTOT FROM tbl_forumPOSTS WHERE fld_fTHREADID = %s ORDER BY fld_fID DESC", GetSQLValueString($threadreplys_rs_replycount, "int"));
    $rs_replycount = mysql_query($query_rs_replycount, $conn_mrs) or die(mysql_error());
    $row_rs_replycount = mysql_fetch_assoc($rs_replycount);
    $totalRows_rs_replycount = mysql_num_rows($rs_replycount);
    ?>                       
    And this is the code to display the total number of records that are related
    <?php echo $row_rs_replycount['countTOT']; ?>
    As I say, I have it working when testing different default values in the recordset wizard, but not live on a page.
    Thanks.

    >"SELECT *, COUNT(*) AS countTOT FROM tbl_forumPOSTS WHERE....
    That SQL is not valid - at least not by SQL standards. If your query contains an aggregate, then all columns that are not aggregated must be in a group by clause. It's possible that MySQL implicitly adds all non-aggreated columns to a group by, but you should probably fix it anyway. If you don't need to use the other columns from that table in the recordset, then just remove the * from the select list.
    "SELECT COUNT(*) AS countTOT FROM tbl_forumPOSTS WHERE....
    If you still have a problem, then output the value in $totalRows_rs_replycount to see if you are getting any rows returned from your query.
    Next, comment out these lines:
    if (isset($_GET['trcount'])) {
      $threadreplys_rs_replycount = $_GET['trcount'];
    to make sure any value in trcount is not interfering with the default value of '3' and see if that returns any results.

  • Select count(*) cause time out error

    I invoke the following statement:
    select count(*) as total from table1
    where table1 is large(30000 rows), total size of the database is more than 20GB.
    The above statement will cause time out error. How to solve the problem?

    Hallo chcw,
    a timeout in a table with 30.000 records is very unusual (for a simple SELECT COUNT). From my point of view Dan is on the right way and you will have a typical blocking scenario.
    1. if you have LOBs they won't be part of a SELECT COUNT (either its a heap of a clustered index)
    2. Microosft SQL Server will ALWAYS use the smallest index for such a simple SELECT
    3. if it is a heap you will run into a blocking scenario if someone is updating records and transaction has not committed.
    4. Check the isolation level of the transactions - but I expect the standard which is "read committed".
    Just a demonstration of "SELECT COUNT" will work whether it is a clustered index or a heap. The next script will create a heap with a LOB and additional attributes:
    CREATE TABLE dbo.foo
    Id INT NOT NULL IDENTITY(1,1),
    n1 INT NOT NULL,
    n2 SMALLINT NULL,
    c1 CHAR(250) NOT NULL,
    c2 CHAR(250) NULL,
    c3 VARCHAR(MAX) NOT NULL DEFAULT (REPLICATE('A', 15000))
    GO
    Now we enter 30,000 records into the table
    SET NOCOUNT ON;
    DECLARE @i INT = 1;
    WHILE @i <= 30000
    BEGIN
    INSERT INTO dbo.foo (n1, c1) VALUES (@i, 'Just a filler');
    SET @i += 1;
    END
    GO
    Keep in mind that we have a HEAP and NO indexes! To check the IO i use the following command befor any of the following examples:
    SET STATISTICS IO ON;
    GO
    Let's start with a first try and you will check as a result the produced IO depending on the affected table partitions:
    -- USE SELECT in a HEAP
    SELECT COUNT(*) FROM dbo.foo;
    GO
    Output:
    Table 'foo'. Scan count 1, logical reads 2508, ..., lob logical reads 0,
    As you can see from the result the LOB-data won't be attached only the "in-row-data" are affected (which are stored in 2508 pages. The reason is a quite simple one: Microsoft SQL Server uses different allocation units for in-row-data and LOB
    SELECT p.object_id, p.index_id, p.rows, au.total_pages
    FROM sys.partitions AS P INNER JOIN sys.allocation_units AS AU
    ON(p.partition_id = au.container_id)
    WHERE object_id = OBJECT_ID('dbo.foo');
    Now I create a simple index on the column n1 which is an INT-datatype
    SELECT p.object_id, p.index_id, p.rows, au.total_pages
    FROM sys.partitions AS P INNER JOIN sys.allocation_units AS AU
    ON(p.partition_id = au.container_id)
    WHERE object_id = OBJECT_ID('dbo.foo');
    and run the SELECT COUNT again with the following IO-output:
    Table 'foo'. Scan count 1, logical reads 69, ...
    WOW - only 69 pages have to be read. The explanation is a quite simple one - now we have an index which is quite small. Microsoft SQL Server WILL use the smallest index of a table to scan the number of records. Let's try it again with n2 which is a smallint
    (2 bytes)
    CREATE NONCLUSTERED INDEX ix_foo_n2 ON dbo.foo (n2);
    GO
    The IO will be 62 (or less) because MORE index records a fitting to one data page!
    Conclusion is that either you have small record size or long record size - The query optimizer will always use the smallest index for the execution (that's maybe why Visahk has asked for the execution plan).
    I don't believe it is because of the "huge amount of data" but - as Dan has pointed out - it HAS TO BE a blocking scenario which can have multiple reasons. For demonstration of a blocking scenario open SSMS and start in the query windows with the following
    command(s):
    BEGIN TRANSACTION
    UPDATE dbo.foo
    SET c1 = 'This is my name'
    WHERE Id = 10000;
    -- What locks do we have now in the database
    SELECT resource_description,
    resource_associated_entity_id,
    request_mode,
    request_type
    FROM sys.dm_tran_locks AS DTL
    WHERE resource_database_id = DB_ID() AND
    resource_type != 'DATABASE';
    Your result should look like that (with differences in the entity_id and resource descriptions :)
    The above pic demonstrates the "locking chain". As you can see the slot 7 on PAGE 82400 has an exclusive lock. This means that NO OTHER request can currently obtain another lock to it!
    Now open a second query window and run a SELECT COUNT... - it will not finish!
    The reason is that the second requests has to wait for the release of the locked resources by the first transaction! Leave the statement and change back to the first transaction and finish the transaction by cancellation of the query. In the moment the locks
    have been released the second query will finish immediate.
    So - from my point of view - Dan has given the correct answer. Against wide spreaded statements a heap won't block the whole table exclusivly but - as in a cluster, too - only the row itself (see the 7 which is the slot where the record exists).
    MCM - SQL Server 2008
    MCSE - SQL Server 2012
    db Berater GmbH
    SQL Server Blog (german only)

  • Get the select count(*)including Zero '0'

    Hi Gurus,
    I gotta problem here, I have this query:
    SELECT COUNT(t.column1) AS mycount,
    TO_CHAR(t.creation_date,'MONTH') AS themonth
    FROM table1 t
    WHERE t.creation_date BETWEEN TO_DATE('01/01/2011') AND TO_DATE('09/07/2011') AND t.column1 > 0
    GROUP BY TO_CHAR(t.creation_date,'MONTH')
    ORDER BY TO_DATE(TO_CHAR(t.creation_date,'MONTH'),'MONTH')
    then I get this result from query
    MYCOUNT | THEMONTH
    JANUARY | 3
    MARCH | 6
    APRIL | 5
    MAY | 9
    JULY | 2
    and what I need its kind of this
    MYCOUNT | THEMONTH
    JANUARY | 3
    FEBRAURY | 0
    MARCH | 6
    APRIL | 5
    MAY | 9
    JUNE | 0
    JULY | 2
    what I'm doing wrong ? or what can I add to the code? please I hope you can help with this one.
    Best Regards,
    Mentor

    mentor wrote:
    Hey and when I dont have a range of dates, and only have many id's how can I get the result ?
    SELECT  NVL(COUNT(column1),0) mycount,
            TO_CHAR(TRUNC(creation_date,'MM'),'MONTH') themonth
      FROM  (
              SELECT  column1,
                      creation_date
                FROM  table1
                WHERE column_id IN (2,3,12,67,49,182)
                  AND column1 > 0
             UNION ALL
              SELECT  NULL column1,
                      ADD_MONTHS(min_creation_date,LEVEL - 1) creation_date
                FROM  (
                       SELECT  TRUNC(MIN(creation_date),'MM') min_creation_date,
                               TRUNC(MAX(creation_date),'MM') max_creation_date
                         FROM  table1
                         WHERE column_id IN (2,3,12,67,49,182)
                           AND column1 > 0
                CONNECT BY ADD_MONTHS(min_creation_date,LEVEL - 1) <= LAST_DAY(max_creation_date)
      GROUP BY TRUNC(creation_date,'MM')
      ORDER BY TRUNC(creation_date,'MM')
    / >
    and another question, how can I get two counts of the same column where the condition its diferent ?
    SELECT  NVL(COUNT(CASE column2 WHEN 'JOHN' THEN 1 END),0) count_a,
            NVL(COUNT(CASE column2 WHEN 'MICHAEL' THEN 1 END),0) count_b,
            TO_CHAR(TRUNC(creation_date,'MM'),'MONTH') themonth
      FROM  (
              SELECT  column2,
                      creation_date
                FROM  table1
                WHERE column2 IN ('JOHN','MICHAEL')
             UNION ALL
              SELECT  NULL column2,
                      ADD_MONTHS(min_creation_date,LEVEL - 1) creation_date
                FROM  (
                       SELECT  TRUNC(MIN(creation_date),'MM') min_creation_date,
                               TRUNC(MAX(creation_date),'MM') max_creation_date
                         FROM  table1
                         WHERE column2 IN ('JOHN','MICHAEL')
                CONNECT BY ADD_MONTHS(min_creation_date,LEVEL - 1) <= LAST_DAY(max_creation_date)
      GROUP BY TRUNC(creation_date,'MM')
      ORDER BY TRUNC(creation_date,'MM')
    / SY.

  • Select count(*) Vs select count(1)

    Hi.
    I need your help.
    I always talk with the developers and said "use select count(1), never user select count(*)", the reason is reduce the read of data on the datafiles.
    I did test with this and all is ok.
    Somebody have this policy?
    Is this correct?
    Regards,
    Milton

    Milton, on one query you will not see difference,
    The main problem is about recursive statement on * that should be consulted on dictionary and "converted" to real column names.
    So if you run a thousands time , you will see the difference.
    Regards
    Hélio Dias
    http://heliodias.com

  • Select count(*) failure

    I'm trying to determine if select count(*) on an existing table could ever fail without there being worse problems that have involved the db and sys admins.
    Instinct says no, it can't fail without databases/systems being unavailable.
    But I've learned NOT to trust instinct when it comes to IT.
    Can anyone relate any situations where they've seen select count(*) fail?

    Hi,
    user639265 wrote:
    I'm trying to determine if select count(*) on an existing table could ever fail without there being worse problems that have involved the db and sys admins.
    Instinct says no, it can't fail without databases/systems being unavailable.In this case, your instinct is right.
    If the table really exists, and you have the necessary privileges, then "SELECT COUNT (*) FROM table_x;" will return exactly one row.
    The only time it would raise an error would be some serious problem (like database or network failure).
    If there is row-level security or fine-grained auditing on the table, then all bets are off: the function that is run in those cases could raise any kind of error.

  • Select count( * ) command at internal tables

    Hi,
    it is not possible to use a select quiry at internal tables.
    I would like determine the number of person with a where
    condition at an internal table.  How does it work ?
    I cant say :
    select count( * ) from xxx into xxxxxxx
                      where xx= xx.
    What else can I do ?
    Regards
    ertas

    hi check this..
    PARAMETERS: p_carrid TYPE sbook-carrid,
                p_connid TYPE sbook-connid.
    TYPES: BEGIN OF sbook_type,
             fldate  TYPE sbook-fldate,
             smoker  TYPE sbook-smoker,
             smk_cnt TYPE i,
           END OF sbook_type.
    DATA sbook_tab TYPE TABLE OF sbook_type.
    SELECT fldate smoker COUNT( * ) AS smk_cnt
           FROM sbook
           INTO CORRESPONDING FIELDS OF TABLE sbook_tab
           WHERE connid = p_connid
           GROUP BY carrid fldate smoker
           HAVING carrid = p_carrid
           ORDER BY fldate smoker
    regards,
    venkat

  • Is it necessary that 'we should select count(*)' before deleting the recor

    hai..
    i need to delete a record in my application for that i wrote a pl/sql process shown below....
    DECLARE
    Symbol varchar2(20);
    ctn number(1);
    BEGIN
    Symbol := :P1_HIDDEN_FLAG;
    select count(*) into ctn from emp1 where EMPNO=Symbol;
    if ctn > 0 then
    Delete from emp1 where EMPNO=Symbol;
    End if;
    End;
    why we need to select count(*) from table.... if we are trying to delete a record that is not available in table... how can we handle that Exception???? Also is it necessary that 'we should select count() before deleting the record from table.....*
    plz clarify me....
    anoo..

    Hello Anoo,
    You can modify the foreign key constraint to ON DELETE CASCADE. Then the child automagically is deleted as well. Otherwise you need two (or more - depending on the number of childs ...and childs of the childs etc) statements. Remember that SQL%ROWCOUNT only returns the number of rows of the last SQL statement issued!
    BTW : You are allowed to mark answers as 'Correct' or 'Helpful'.... ;-)
    Greetings,
    Roel
    http://roelhartman.blogspot.com/
    http://www.bloggingaboutoracle.org/
    http://www.logica.com/

  • Is there any way I can recover deleted pictures from Camera Roll and iCloud? Please please help me. All of my precious pictures are gone that I can never EVER get back. I dont have any iCloud or Camera Roll backup; and my PC can't download more software.

    Hi everyone! I'm in need of extreme help. You see, today, two months ago I went out of the country from the United States of America. I took about 1500 pictures ONLY on my iPod touch. When I returned to the United States to my home, I accidentally deleted a little more than 100 pictures. Then, thinking I could get ALL of the pictures back at once, I restored my iPod touch from an iCloud backup I had a couple of days before. Instead of getting all of my pics back, they all got erased. The pictures that I did delete were deleted from Camera Roll AND Photo Stream (iCloud). Then, I was only able to get about 500 pictures back from iCloud. So about 1000 are still gone. When i did the restore, I thought all my pictures would be there, but they weren't and I couldn't possibly figure out why; because a restore from iTunes or iCloud is supposed to restore your device to the settings of the time you backed up your device. All of my other settings were just the way they were supposed tone for the time being, all except my pictures, which contained not one picture in any folder. So EVERYTHING from Cameta Roll was gone, and only about 500 pictures survived. I kept retoring and restoring, hoping they would come back but unfortunately they didn't. Then one time a restored again, I saw all of them back!! But then, within a blink of an eye, they disappeared as soon as I tapped on one picture. Then, I knew they were still in reach, but I just had to find them. Where could those 1000 pictures go? I looked up how to recover deleted photos, but I have to install a software. My computer CANNOT possibly take anymore software on it, my PC is very sensitive and I cannot delete anything off of it. Please please help me. What can I do to get my pictures back? They are memories that I can never EVER EVER get back. Thank you so much.

    Please please help me, if you know how.

Maybe you are looking for

  • Swipe between pages feature is freezing with magic mouse and magic trackpad in Mavericks

    I am experiencing a bug in Mavericks with my Magic trackpad and the Magic mouse. This bug comes and goes and is very sporadic. When it happens I am usually in a browser like Firefox or Safari, or in iPhoto. Anything that uses the Swipe between pages

  • ICal Calendar colors: changing

    The "Help" file gives the wrong information. It says to click the "Info" button in the bottom right hand corner, etc., (see text below), but there is no such thing. By chance I discovered that if you click on a calendar and then use "Get Info" a wind

  • S2S between Cisco ASA 5505 and Sonicwall TZ-170 but not able to ping across

    Hi, I am helping out a friend of mine with his Site-to-Site VPN between his companies Cisco ASA another company's SonicWall TZ-170.  I have checked the screenshots proivded by the other end and tried to match with ours.  The Tunnel shows but we are n

  • Upgrade to OS X Lion Server

    I am currently running a Mac Mini server setup in a RAID-1 configuration.  I wanted to upgrade to OS X Lion Server.  I went  to the App Store, and found out that I need to purchase OS X Lion and OS X Lion Server to upgrade.  I made the purchase, and

  • Url link with target="_blank" hangs in Internet Explorer 7 BUT works fine in IE6, FF, Safari?

    Okay, so I have a SWF embedded on a page, which has a link to a target="_blank" url, and it works in every other browser, INCLUDING the notorious ie6, but NOT in IE7? IE7 just opens a blank page (without the address in the address field) and displays