After creating the partition and added rows to the table in oracle. NUM_ROWS are not showing the expected answer?
SQL> SELECT num_rows, partition_name,table_name FROM DBA_TAB_PARTITIONS
WHERE table_name='AUDITS';`
NUM_ROWS PARTITION_NAME TABLE_NAME
---------- ------------------------------ ------------------------------
P2 AUDITS
P3 AUDITS
SQL> SELECT count(*) FROM audits;
COUNT(*)
----------
98
SQL>
`
If you view the oracle documentation for ALL_TABLES, you will see this note
Note:
Columns marked with an asterisk (*) are populated only if you collect statistics on the table with the DBMS_STATS package.
and further down,
NUM_ROWS*
There is no such note for DBA_TAB_PARTITIONS, but I think it should be the same.
Related
If you could assist with a query that returns number of end-users accessing the application.
The below query returns only the sessions
SQL> select count(*) from v$session where status='ACTIVE';
COUNT(*)
----------
67
SQL> select count(*) from v$session;
COUNT(*)
----------
140
I want to know when the num_rows is set to null in all_tab_statistics table.
One of my queries is taking a bad plan because the table is having num_rows as blank.
The table is partitioned. So the table is freshly created and ideally, it should have 0 rows. What could be the reason for this and how to avoid this.
Also when we are running the job on the freshly created table which is having blank num_rows(in stats) the rows got inserted in between the process is more than 0 but since we can't analyze the table between the process it took wrong plan (because of blank num_rows)
I have created a table
CREATE table empty
(
dummy NUMBER
);
First time running this query gives
select table_name, num_rows from sys.all_tab_statistics where owner = 'HR' and table_name = 'EMPTY';
TABLE_NAME NUM_ROWS
---------- --------
EMPTY <NULL>
Then I inserted some rows
INSERT INTO empty SELECT SALARY FROM EMPLOYEES;
COMMIT;
And got the same result
TABLE_NAME NUM_ROWS
---------- --------
EMPTY <NULL>
Running this statement populated the row for EMPTY table in all_tab_statistics
ANALYZE TABLE empty COMPUTE STATISTICS;
I got
TABLE_NAME NUM_ROWS
---------- --------
EMPTY 107
I have about 50 tables and I would like to know if there is any way to obtain with a query, which columns of my tables are created in a specific tablespace? can you know? or know what things are created in that tablespace?
I guess this might be what you're looking for.
Create a sample table which contains a CLOB datatype column:
SQL> create table test1
2 (id number,
3 text clob
4 );
Table created.
LOB columns can be stored into a different tablespace than the rest of columns; although I didn't specify storage info (so TEXT column resides in the same tablespace as the rest of the columns), querying USER_LOBS returns info you're interested in:
SQL> select column_name,
2 table_name,
3 tablespace_name --> this column
4 from user_lobs
5 where table_name = 'TEST1';
COLUMN_NAM TABLE_NAME TABLESPACE_NAME
---------- ---------- ------------------------------
TEXT TEST1 USERS
SQL>
Another free hint: when you're unsure of where to look for certain things, try to ask the Dictionary. For example:
SQL> select * From dictionary where lower(table_name) like '%lob%';
TABLE_NAME COMMENTS
------------------------------ --------------------------------------------------
ALL_LOBS Description of LOBs contained in tables accessible
to the user
ALL_LOB_PARTITIONS
<snip>
USER_LOBS Description of the user's own LOBs contained in the
user's own tables
<snip>
15 rows selected.
SQL>
I created first sequence=>
SQL> create sequence sq_001;
Then synonym for sequence=>
SQL> create synonym syn_001 for sq_001;
Then I query the user_synonyms =>
SQL> select*from user_synonyms where synonym_name = 'SYN_001';
SYNONYM_NAME TABLE_OWNER TABLE_NAME DB_LINK ORIGIN_CON_ID
--------------- --------------- --------------- --------------- -------------
SYN_001 RAMIN SQ_001 3
This is confused me, written table_name but this is sequence(SQ_001) and worked =>
TABLE_NAME
---------------
SQ_001
Yes, a sequence can have a synonym as you have demonstrated. The view USER_SYNONYMS is confusing in having a column called TABLE_NAME that can contain other things - it should really be OBJECT_NAME. Presumably when USER_SYNONYMS was first created only tables could have synonyms.
How to create a temporary table in oracle without knowing the number and name of columns.
For example:
Select columnA,columnB,* into temp_table from tableA.
Here,tableA maynot be a simple tablename but maybe derived from many queries.
How can this be achieved?Is there any alternative to this?
In Oracle, you have to first create a table, then insert into it. Or, create it directly (as in my example).
Note that I've created a "normal" table; if it were temporary, you could have chosen between a global or private (depending on database version you use).
For this discussion, I guess that it is just fine:
SQL> create table temp_table as
2 select a.*
3 from (select d.deptno, d.dname, e.ename --> this SELECT is your "tableA"
4 from emp e join dept d
5 on e.deptno = d.deptno
6 where job = 'CLERK'
7 ) a;
Table created.
SQL> select * from temp_table;
DEPTNO DNAME ENAME
---------- -------------------- ----------
10 ACCOUNTING MILLER
20 RESEARCH SMITH
20 RESEARCH ADAMS
30 SALES JAMES
SQL>
Alternatively, create a view and work with it:
SQL> create or replace view v_temp as
2 select d.deptno, d.dname, e.ename
3 from emp e join dept d
4 on e.deptno = d.deptno
5 where job = 'CLERK'
6 ;
View created.
SQL> select * from v_temp;
DEPTNO DNAME ENAME
---------- -------------------- ----------
10 ACCOUNTING MILLER
20 RESEARCH SMITH
20 RESEARCH ADAMS
30 SALES JAMES
SQL>
This statement creates temp_table which contains all columns and data from tableA and two other empty columns, varchar and numeric.
create table temp_table as
select cast (null as varchar2(10)) columnA,
cast (null as number(6)) columnB,
tableA.*
from tableA
If you need only structure, no data, then add:
where 1 = 0
I am not sure why you want to have a temp table as you do not know columns, but you can think of dynamic SQL to create table depending on columns required during your process and then drop it again. From my point of view I think it is not a good design.
I can suggest to think on using collection with 'x' number of columns with datatype as VARCHAR2. During transaction you can populate and process according to you need and it will also remain for that session.