Oracle SQL 查询列出所有活动 users/schemas 并给出对象计数
Oracle SQL query to list all active users/schemas and give object count
我正在尝试编写一个查询,以提供有关 users/schemas 的信息。
需要使用以下内容:
COLUMNS = 用户名,default_tablespace,account_status
TABLE = dba_users
PLUS,结果集中的一个新虚拟列,显示 username/schema 的对象计数。即它拥有的对象数量。
简单的 where 子句应该是 account_status 是开放的,并且用户名不是 X、Y、Z 之一。
因此它将以某种方式组合以下基本查询:
select username, default_tablespace, account_status
from dba_users
where account_status = 'OPEN'
and username not in ('GEORGE','ANNA','BOB')
有了这个:
select owner, count(*) as object_count from dba_objects
group by owner
order by 1
我试着尝试使用在线查询。我能想到的最好的是下面的查询,但这只列出了 dba_objects table 中存在的用户。即仅列出架构(包含至少 1 个对象)而不是普通用户。
select username, account_status, default_tablespace, subquery1.object_count
from dba_users,
(
select owner,count(*) as object_count from dba_objects
group by owner order by 1
) subquery1
where username = owner
and account_status = 'OPEN'
and username not in ('GEORGE','ANNA','BOB')
order by username
我认为我需要执行左外连接,以保留来自 dba_users 的记录来满足查询的其余部分,因此 return 只是一个 "NULL" object_count 列,但是当我如下所示尝试时,我得到了垃圾结果,其中列出了太多,使用随机对象计数重复用户多次。
select username, account_status, default_tablespace, subquery1.object_count
from dba_users,
(
select owner, count(*) as object_count from dba_objects
group by owner order by 1
) subquery1
left join dba_users on username = subquery1.owner
and account_status = 'OPEN'
and username not in ('GEORGE','ANNA','BOB')
order by username
作为奖励,如果有人可以提出一个查询,不仅提供上述内容,而且还提供一个额外的虚拟列,提供大小为 table 的已用空间,那就太好了。所以它会使用类似下面的查询:
select round((sum(bytes)/1024/1024/1024),1) 作为 size_in_gb, 所有者
来自 dba_segments
通过...分组
所有者
非常感谢。
构造此类查询的方法有很多种。如果您只从另一个 table 获得一个值,我很想做一个内联 select 而不是费心做一个显式连接
select username, default_tablespace, account_status,
(select count(1)
from dba_objects o
where o.owner = u.username) cnt_objects_owned,
(select sum(bytes)
from dba_segments s
where s.owner = u.username) total_size_of_segments
from dba_users u
where account_status = 'OPEN'
and username not in ('GEORGE','ANNA','BOB')
这样做的好处是添加新的内联 select 相对容易。但是,如果将来您想从 dba_objects
或 dba_segments
中获取多个值,并且您最终会在其他内联 select 中重复自己,那将是非常低效的。
有趣的是,如果我将查询中的 count(1)
更改为 count(*)
,我会不断收到错误消息
我正在尝试编写一个查询,以提供有关 users/schemas 的信息。
需要使用以下内容:
COLUMNS = 用户名,default_tablespace,account_status TABLE = dba_users
PLUS,结果集中的一个新虚拟列,显示 username/schema 的对象计数。即它拥有的对象数量。 简单的 where 子句应该是 account_status 是开放的,并且用户名不是 X、Y、Z 之一。
因此它将以某种方式组合以下基本查询:
select username, default_tablespace, account_status
from dba_users
where account_status = 'OPEN'
and username not in ('GEORGE','ANNA','BOB')
有了这个:
select owner, count(*) as object_count from dba_objects
group by owner
order by 1
我试着尝试使用在线查询。我能想到的最好的是下面的查询,但这只列出了 dba_objects table 中存在的用户。即仅列出架构(包含至少 1 个对象)而不是普通用户。
select username, account_status, default_tablespace, subquery1.object_count
from dba_users,
(
select owner,count(*) as object_count from dba_objects
group by owner order by 1
) subquery1
where username = owner
and account_status = 'OPEN'
and username not in ('GEORGE','ANNA','BOB')
order by username
我认为我需要执行左外连接,以保留来自 dba_users 的记录来满足查询的其余部分,因此 return 只是一个 "NULL" object_count 列,但是当我如下所示尝试时,我得到了垃圾结果,其中列出了太多,使用随机对象计数重复用户多次。
select username, account_status, default_tablespace, subquery1.object_count
from dba_users,
(
select owner, count(*) as object_count from dba_objects
group by owner order by 1
) subquery1
left join dba_users on username = subquery1.owner
and account_status = 'OPEN'
and username not in ('GEORGE','ANNA','BOB')
order by username
作为奖励,如果有人可以提出一个查询,不仅提供上述内容,而且还提供一个额外的虚拟列,提供大小为 table 的已用空间,那就太好了。所以它会使用类似下面的查询:
select round((sum(bytes)/1024/1024/1024),1) 作为 size_in_gb, 所有者 来自 dba_segments 通过...分组 所有者
非常感谢。
构造此类查询的方法有很多种。如果您只从另一个 table 获得一个值,我很想做一个内联 select 而不是费心做一个显式连接
select username, default_tablespace, account_status,
(select count(1)
from dba_objects o
where o.owner = u.username) cnt_objects_owned,
(select sum(bytes)
from dba_segments s
where s.owner = u.username) total_size_of_segments
from dba_users u
where account_status = 'OPEN'
and username not in ('GEORGE','ANNA','BOB')
这样做的好处是添加新的内联 select 相对容易。但是,如果将来您想从 dba_objects
或 dba_segments
中获取多个值,并且您最终会在其他内联 select 中重复自己,那将是非常低效的。
有趣的是,如果我将查询中的 count(1)
更改为 count(*)