DIfferent # of rows return using select * & select count(*)
In debugging some code (OWB generated PL/SQL) I came across the situation where by a query with count(*) returns a value of 600 rows. The same where clause using select * reports only 300 rows selected. There are 600 rows to select.
Any ideas why the difference ???
The code:
SELECT *
FROM "STG_SYSTEM_CUSTOMER_V" "STG_SYSTEM_CUSTOMER_V",
DW_REPRESENTATIVE INGRP8
where "STG_SYSTEM_CUSTOMER_V"."PILLAR_STATE_CD" IN 'VIC'
AND "STG_SYSTEM_CUSTOMER_V"."BUS_PILLAR_CD" = 'IGA'
AND ( "STG_SYSTEM_CUSTOMER_V"."MN_COMP_CD" = "INGRP8"."MN_COMP_CD" (+) )
AND ( "STG_SYSTEM_CUSTOMER_V"."COUNTRY_CD" = "INGRP8"."COUNTRY_CD" (+) )
AND ( "STG_SYSTEM_CUSTOMER_V"."COMP_CD" = "INGRP8"."COMP_CD" (+) )
AND ( "STG_SYSTEM_CUSTOMER_V"."BUS_PILLAR_CD" = "INGRP8"."BUS_PILLAR_CD" (+) )
AND ( "STG_SYSTEM_CUSTOMER_V"."PILLAR_STATE_CD" = "INGRP8"."PILLAR_STATE_CD" (+) )
AND ( "STG_SYSTEM_CUSTOMER_V"."SUPP_DIVISION_CD" = "INGRP8"."DIVISION_CD" (+) )
AND ( "STG_SYSTEM_CUSTOMER_V"."ALM_REP_NUM" = "INGRP8"."REP_NUM" (+) )
Cheers
Pav
You're sure that the only difference between the two statelents is that "*" is replaced with "count(*)"? Assuming there are no transactions going on simultaneously, that would be a bug. Have you installed the latest patchset to your database?
Justin
Distributed Database Consulting, Inc.
www.ddbcinc.com
Similar Messages
-
How to get number of rows return in SELECT query
i'm very new in java, i have a question:
- How to get number of rows return in SELECT query?
(i use SQL Server 2000 Driver for JDBC and everything are done, i only want to know problems above)
Thanks.make the result set scroll insensitve, do rs.last(), get the row num, and call rs.beforeFirst(), then you can process the result set like you currently do.
String sql = "select * from testing";
PreparedStatement ps =
con.prepareStatement(sql,ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_READ_ONLY);
ResultSet rs = ps.executeQuery();
rs.last();
System.out.println("Row count = " + rs.getRow());
rs.beforeFirst();~Tim
NOTE: Ugly, but does the trick. -
Number of rows inserted is different in bulk insert using select statement
I am facing a problem in bulk insert using SELECT statement.
My sql statement is like below.
strQuery :='INSERT INTO TAB3
(SELECT t1.c1,t2.c2
FROM TAB1 t1, TAB2 t2
WHERE t1.c1 = t2.c1
AND t1.c3 between 10 and 15 AND)' ....... some other conditions.
EXECUTE IMMEDIATE strQuery ;
These SQL statements are inside a procedure. And this procedure is called from C#.
The number of rows returned by the "SELECT" query is 70.
On the very first time call of this procedure, the number rows inserted using strQuery is *70*.
But in the next time call (in the same transaction) of the procedure, the number rows inserted is only *50*.
And further if we are repeating calling this procedure, it will insert sometimes 70 or 50 etc. It is showing some inconsistency.
On my initial analysis it is found that, the default optimizer is "ALL_ROWS". When i changed the optimizer mode to "rule", this issue is not coming.
Anybody faced these kind of issues?
Can anyone tell what would be the reason of this issue..? any other work around for this...?
I am using Oracle 10g R2 version.
Edited by: user13339527 on Jun 29, 2010 3:55 AM
Edited by: user13339527 on Jun 29, 2010 3:56 AMYou have very likely concurrent transactions on the database:
>
By default, Oracle Database permits concurrently running transactions to modify, add, or delete rows in the same table, and in the same data block. Changes made by one transaction are not seen by another concurrent transaction until the transaction that made the changes commits.
>
If you want to make sure that the same query always retrieves the same rows in a given transaction you need to use transaction isolation level serializable instead of read committed which is the default in Oracle.
Please read http://download.oracle.com/docs/cd/E11882_01/appdev.112/e10471/adfns_sqlproc.htm#ADFNS00204.
You can try to run your test with:
set transaction isolation level serializable;If the problem is not solved, you need to search possible Oracle bugs on My Oracle Support with keywords
like:
wrong results 10.2Edited by: P. Forstmann on 29 juin 2010 13:46 -
Number of rows returned by SELECT LAST(column)
I have about 50,000 rows in my MS Acess database table so I have used SELECT LAST(column) AS newField FROM table... to retrieve the last data in the column however when I check the number of rows in the resultset using
resultset.last();
int rowcount = rs.getRow();
rowcnt returns the total no.rows (about 50,000) in the table.
I thought it should just return one.
Is it normal?Thanks again dcminta. I'll try with your code.
I just wanted to know why resultset returned the number of all records in that column when I only selected the last.
I had the �Invalid Cursor Position� error with "while(rs.next())" as I (thought I) had only one record in the resultset and I was fiddling with my code.
They are all fine now.
Thanks guys.
Booh1(old lady) -
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,
SumanHi 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é -
Multiple columns and rows update using select staement
i am trying to update the 2 colums of a table based on the cursor result set with key values are matching.
DECLARE
CURSOR update_master
IS
SELECT B.cust_num,c.cust_num,
B.cust_name,B.cust_bal
FROM
Table_one B
JOIN
Table_two C
ON B.cust_num= C.cust_num;
BEGIN
FOR master_rec IN update_master
LOOP
UPDATE Table_three
SET col1 = master_rec.cust_name,
Col2 = master_rec.cust_bal,
Where WHERE Cust_num = master.cust_num;
END LOOP;
COMMIT;
END;
The 3 tables are having around 50000 records individually............
It is taking too much of time to execute......more than 6 minutes......
how can we optimize it?? can we write this update statement directly? instead of writing it in procedure?Here's a general approach:
update some_table s
set (s.col1, s.col2) = (select x.col1, x.col2
from other_table x
where x.key_value = s.key_value
where exists (select 1
from other_table x
where x.key_value = s.key_value
; -
How to do a 'select count' without a cursor
I am trying to do a select which will return exactly one row, such as:
select count(*) from orders;
Since there is inherent overhead in using a cursor, I am trying to find a way to do this without
generating a result set (both executeQuery and execute use result sets to return the data).
What I am looking for is something equivalent to ESQL:
select count(*) from orders INTO integervariable;
Does anyone know how to do this?The reason for the concern is because I am issuing the
SQL many times (thousands), with a slightly different
'where' clause each time, and the platform I am using
doesn't support 'prepare's.
So, even ignoring the overhead question, is there a
way to do this without using a result set, which
has an inherent cursor.No - there is no way of doing this without using a resultset. -
Is it possible to count the rows returned from a query?
Hello,
When using JDBC is there anyway of finding out the number of
rows returned from a query before actually getting each row?
In Forms 4.5 you can use the count_query function, does anyone
know of an equivalent function or work around in JDBC and/or
SQLJ?
Thanks.
nullPasi Hmlinen (guest) wrote:
: Try
: SELECT COUNT(*) FROM the_table WHERE <conditions>;
: Hope this helps,
: Pasi
Thanks for the advice, I'm currently using SELECT COUNT(*) but
I'm looking for a more efficient way of doing it. If I SELECT
COUNT each time then I have to prepare and execute a SQL
statement each time. What I want to do it execute a single SQL
statement to return my results and somehow find out the number
of rows in the resultset without having to go back to the
database.
Gethin.
null -
Alternative for select count('x') or count(*)
Hi Gurus,
I need to use select count(*) multiple times in a SP before and after deletion of rows from multiple tables. As this is an expensive method, do we have some alternative for Count(*), like selecting something from a system table or view instead?Hi
You can know the number of rows affected by the DELETE statement in PL/SQL (if you are using PL/SQL) using sql%rowcount. So you don't really need to count the rows again after the delete took place...
If you are deleting the same rows as selected by the second count condition (older than a particular date), in fact you only need to count rows once for each table.
Luis -
Getting Long time to execute select count(*) statement.
Hi all,
My table have 40 columns and it doesn't have the primary key column. it contain more than 5M records. it's taking long time to execute simple sql statement.
Such as select (*) take 1min and 30 sec. If i use select count(index_colunm) then it finished with in 3s. i did the following workarounds.
Analyzed the table.
created required indexes.
yet getting the same performance issues. please help me to solve this issue
ThanksBlueDiamond wrote:
COUNT(*) counts the number of rows produced by the query, whereas COUNT(1) counts the number of 1 values.Would you care to show details that prove that?
In fact, if you use count(1) then the optimizer actually re-writes that internally as count(*).
Count(*) and Count(1) are have identical executions.
Re: Count(*)/Count(1)
http://asktom.oracle.com/pls/asktom/f?p=100:11:6346014113972638::::P11_QUESTION_ID:1156159920245 -
Select count(x) on a table with many column numbers?
Hi all,
i have a table with physical data with 850 (!!) colums and
~1 Million rows.
The select count(cycle)from test_table Statement is very, very slow
WHY?
The select count(cycle)from test_table is very fast by e.g 10 Colums. WHY?
What has the number of columns, to do with the SELECT count(cyle).... statement?
create test_table(
cycle number primary key,
stamp date,
sensor 1 number,
sensor 2 number,
sensor_849 number,
sensor_850 number);
on W2K Oracle 9i Enterprise Edition Release 9.2.0.4.0 Production
Can anybody help me?
Many Thanks
Achimhi lennert, hi all,
many thanks for all the answers. I�m not an Oracle expert.
Sorry for my english.
Hi Lennert,
you are right, what must i do to use the index in the
query? Can you give me a pointer of direction, please?
Many greetings
Achim
select count(*) from w4t.v_tfmc_3_blocktime;
COUNT(*) ==> Table with 3 columns (very fast)
306057
Ausf�hrungsplan
0 SELECT STATEMENT Optimizer=CHOOSE
1 0 SORT (AGGREGATE)
2 1 TABLE ACCESS (FULL) OF 'V_TFMC_3 _BLOCKTIME'
Statistiken
0 recursive calls
0 db block gets
801 consistent gets
794 physical reads
0 redo size
388 bytes sent via SQL*Net to client
499 bytes received via SQL*Net from client
2 SQL*Net roundtrips to/from client
0 sorts (memory)
0 sorts (disk)
1 rows processed
select count(*) from w4t.v_tfmc_3_value;
COUNT(*)==> Table with 850 columns (very slow)
64000
Ausf�hrungsplan
0 SELECT STATEMENT Optimizer=CHOOSE
1 0 SORT (AGGREGATE)
2 1 TABLE ACCESS (FULL) OF 'V_TFMC_3 _VALUE'
Statistiken
1 recursive calls
1 db block gets
48410 consistent gets
38791 physical reads
13068 redo size
387 bytes sent via SQL*Net to client
499 bytes received via SQL*Net from client
2 SQL*Net roundtrips to/from client
0 sorts (memory)
0 sorts (disk)
1 rows processed -
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,
MiltonMilton, 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 -
Wrong results from "Select count(*) from tbl"
I am the user of an Oracle DB and I am getting what
look like wrong counts using "Select count(*)" statments.
It first occurred when I tried to get monthly record counts with
Select count(*), month
from tbl
group by month
(Note this is the query I used with the exception
of a change in the table name. The month field is numeric)
All the monthly counts but one looked like it was 2x what it should be.
Oddly enough if I tried
Select count(*), month
from tbl
where month = 200302
group by month
OR
Select count(*), month
from tbl
where month in (200301 200302)
group by month
I got what looked correct.
I even got the 2X problem when I asked for a count on
the entire table using
Select count(*)
from tbl
One of our DBAs suggested the problem has somehting to do with
the indexes and not running analyze on the table recently.
I'll note also that the table is partitioned.
I think it's Oracle version 8.1.7.4.0 running on a Sun box.
Has anyone seen this before or does anyone have any
suggestions on the cause of the problem?
I have been told it has to do with indexes
and I know there are many indexes on this table.
The table is also partitioned.
Thanks,
- johnplz copy and paste here your tests
i am running 8.1.7.4.0 too and all is fine here...
(i heard about a count(*) problem but on a lite version of an old 8.0.x...)
Fred -
How to get total number of rows return by query
hi all
i am using forms 6i with oracle 10G in windows environment....
i have a tabular form now i want to know that how many rows return by the query...like when user click on enter query and then give any search criteria and then execute query..it displays the records but i want to count the records that how many rows are return.............
hope you understant what i mean
Thanks in advance
RegardsU can use
"Select count(*) from <table> where <condition>"
<condition> is the where clause being used during "Execute Query"
this will give u the no of records in query.
And second option is to take a summary column. whose function is set "COUNT" and
"Summarized item" should contain a column that will never be blank. After execute query this column will give u no of records in block.
Regards.... -
"select count(*)" and "select single *" returns different result
Good day!
product version SAP ECC 6.0
oracle10
data transfers from external oracle db into customer tables using direct oracle db link
sometimes I get case with different results from 2 statements
*mytable has 10 rows
*1st statement
data: cnt type I value 0.
select count( * ) into cnt from mytable WHERE myfield_0 = 123 and myfield_1 = '123'.
*cnt returns 10 - correct
*2nd statement
select single * from mytable WHERE myfield_0 = 123 and myfield_1 = '123'.
*sy-dbcnt returns 0
*sy-subrc returns 4 - incorrect, 10 rows are "invisible"
but
1. se16 shows correct row number
2. I update just one row from "invisible" rows using se16 and 2nd statement returns correct result after that
can not understand why
thank you in advance.Thank you, Vishal
but,
general problem is that
1. both statements have the same WHERE conditions
2. 1st return resultset with data (sy-dbcnt=10), 2nd return empty dataset, but must return 1 in sy-dbcnt
Yes, different meaning, you are right, but must 2nd must return 1, because of "select single *" construction, not 0.
Dataset to process is the same, WHERE conditions are equal...
I think the problem is that how ABAP interperets select count(*) and "select single *".
Maybe "select count (*)" scans only PK from index page(s)? and "select single *" scans data pages? and something is wrong with that?
I'm new in SAP and didn't find any SAP tool to trace dump of data and indexes pages with Native SQL.
se16 shows all records.
And why after simple manual update of just one record using se16 "select single *" returns 1?
I've just marked one row to update, didn't change any data, then pressed "save".
Maybe you are looking for
-
Invalid URL error when displaying Work Item Attached URL Object in UWL
Hi, After upgraded from EP6 to EP7 the user can no longer display the attached URL object of work items in the UWL Inbox. It works fine in the SAPGUI Inbox so the URL should be okay. We found below error messages in UWL log file: #1.5#0003BA5D298B0
-
How Do I Check If Phone # has Been used Before
My father is on my sister's Friends and family plan and got a phone..his phone number is (973) ***-**** and has been getting phone calls from people looking for various others and was wondering how do i check if the phone number has been properly can
-
"No photos were copied" to iPhone 4 despite plenty of space
I have only 12 (5MP) photos to sync to iPhone, and 600 MB free (the iPhone and iTunes both agree on this). I had just deleted MANY more photos than 12, so adding a few back should be no problem. In fact, they'd just come from the phone's own camera r
-
Was wondering if anyone can help me. We've lost signal on certain channels today. The BBC channels as well as Pick TV, Dave, Really, E4+1, Movie Mix, Create & Craft, Sky News have no signal. Haven't checked EVERY other channel but they appear to be w
-
After Effects CS5 rendering with 48 cores and 96 GB of RAM
My company is trying to put together some new machines for our render farm. Currently we are considering building a system with 4 12-core CPUs and 96 GB of RAM. Theoretically this would mean that After Effects could render 48 frames simultaneously, b