AskOverflow.Dev

AskOverflow.Dev Logo AskOverflow.Dev Logo

AskOverflow.Dev Navigation

  • 主页
  • 系统&网络
  • Ubuntu
  • Unix
  • DBA
  • Computer
  • Coding
  • LangChain

Mobile menu

Close
  • 主页
  • 系统&网络
    • 最新
    • 热门
    • 标签
  • Ubuntu
    • 最新
    • 热门
    • 标签
  • Unix
    • 最新
    • 标签
  • DBA
    • 最新
    • 标签
  • Computer
    • 最新
    • 标签
  • Coding
    • 最新
    • 标签
主页 / user-88154

SHR's questions

Martin Hope
SHR
Asked: 2021-01-28 08:40:07 +0800 CST

是否可以为过程提供 PDB 名称并从中进行选择?

  • 0

以下示例不起作用,我只是想展示我正在尝试做的事情。

给定表:

-- this table is located at the CDB
create table t1 (tab_name varchar2(35))
/
-- this proc is located at the CDB
Create or replace procedure FindTables(vPdb in varchar2, vOwner in varchar2)
BEGIN
  insert into t1(tab_name) select TABLE_NAME from ALL_TABLES@vPdb where OWNER=vOwner;
END;
/

有没有办法创建一个临时数据库链接或类似的东西?或任何其他方式?

编辑

我还需要用户表的解决方案。

oracle oracle-19c
  • 1 个回答
  • 126 Views
Martin Hope
SHR
Asked: 2021-01-25 07:15:15 +0800 CST

如何使用 cdb 用户从 pdbs 中进行选择?

  • 0

我有几个存储过程,它们使用 log miner 分析 oracle 日志。这些程序在为此方式创建的 CDB 用户 C##ADMIN 上运行。

现在我想将记录的值与实时值进行比较,因此我需要能够从 CDB 访问每个 pdb。

经过几次搜索,我发现了database link一个可能的答案。但是当我试图创建一个数据库链接时,它会说ORA-02011: duplicate database link name。当我试图放弃它时,我得到了ORA-65230: internal database link cannot be altered or dropped.

我尝试运行以下查询,但失败了:(即使连接为/ as sysdba:)

SELECT * FROM ALL_TABLES@testpdb; 

ORA-12541: TNS:no listener
*Cause: The connection request could not be completed because the listener 
      is not running.
*Action: Ensure that the supplied destination address matches one of 
      the addresses used by the listener - compare the TNSNAMES.ORA entry with 
      the appropriate LISTENER.ORA file (or TNSNAV.ORA if the connection is to 
      go by way of an Interchange). Start the listener on the remote machine.

SELECT * FROM test.TABLE1@testpdb;   

ORA-12541: TNS:no listener
*Cause: The connection request could not be completed because the listener 
      is not running.
*Action: Ensure that the supplied destination address matches one of 
      the addresses used by the listener - compare the TNSNAMES.ORA entry with 
      the appropriate LISTENER.ORA file (or TNSNAV.ORA if the connection is to 
      go by way of an Interchange). Start the listener on the remote machine.

所以我的问题是:

  • 使用数据库链接是正确的方法吗?
  • 如果是,如何配置?
  • 我该如何使用它?
  • 我有其他选择来实现我的目标吗?
oracle oracle-19c
  • 1 个回答
  • 150 Views
Martin Hope
SHR
Asked: 2021-01-20 03:48:51 +0800 CST

从过程调用时从 V$LOGMNR_CONTENTS 中选择失败

  • 1

假设以下用户被授予这些权限:

-- create admin user on CDB
CREATE USER c##myadmin IDENTIFIED BY myadmin DEFAULT TABLESPACE system QUOTA UNLIMITED ON system ACCOUNT UNLOCK
/
-- allow access to all PDBs to the admin user
ALTER USER c##myadmin SET CONTAINER_DATA=ALL CONTAINER=CURRENT
/ 
-- grant needed permissions
GRANT DBA to c##myadmin                            ;
GRANT CREATE SESSION TO c##myadmin                 ;
GRANT CREATE TABLE TO c##myadmin                   ;
GRANT EXECUTE_CATALOG_ROLE TO c##myadmin           ;
GRANT EXECUTE ON DBMS_LOGMNR TO c##myadmin         ;
GRANT SELECT ON V_$DATABASE TO c##myadmin          ;
GRANT SELECT ON V_$LOGMNR_CONTENTS TO c##myadmin   ;
GRANT SELECT ON V_$ARCHIVED_LOG TO c##myadmin      ;
GRANT SELECT ON V_$LOG TO c##myadmin               ;
GRANT SELECT ON V_$LOGFILE TO c##myadmin           ;
GRANT RESOURCE, CONNECT TO c##myadmin              ;

现在,当我以我的管理员身份连接时,我可以运行以下命令:

BEGIN 
  DECLARE v NUMBER := 0;
BEGIN
  DBMS_LOGMNR.ADD_LOGFILE(LogFileName=>'/path/to/archive/log/arc0000013.0001', Options=>DBMS_LOGMNR.new);
  DBMS_LOGMNR.START_LOGMNR(StartScn=>23456789, EndScn=>23567890,  Options=>DBMS_LOGMNR.DICT_FROM_ONLINE_CATALOG+DBMS_LOGMNR.NO_ROWID_IN_STMT);
  select count(*) into v from v$logmnr_contents;
END;
END;
/
PL/SQL procedure successfully completed.

但是当它作为一个过程创建时,它因权限不足而失败:

Create Or Replace Procedure Test AS
 v NUMBER:=0;
BEGIN
 DBMS_LOGMNR.ADD_LOGFILE(LogFileName=>'/path/to/archive/log/arc0000013.0001', Options=>DBMS_LOGMNR.new);
 DBMS_LOGMNR.START_LOGMNR(StartScn=>23456789, EndScn=>23567890,  Options=>DBMS_LOGMNR.DICT_FROM_ONLINE_CATALOG+DBMS_LOGMNR.NO_ROWID_IN_STMT); 
 Select Count(*) into v from v$logmnr_contents;
END;
/
Exec Test
/
Procedure Test compiled


Error starting at line 9 in command -
BEGIN Test; END;
Error report - 
ORA-01031: insufficient privileges
ORA-06512: at "C##MYADMIN.TEST", line 6
ORA-06512: at line 1
01031. 00000 -  "insufficient privileges"
*Cause:    An attempt was made to perform a database operation without
           the necessary privileges.
*Action:   Ask your database administrator or designated security
           administrator to grant you the necessary privileges

如果我注释掉select程序成功。

是否有额外的权限使其能够select从过程中运行?

oracle permissions
  • 2 个回答
  • 162 Views
Martin Hope
SHR
Asked: 2021-01-19 06:36:29 +0800 CST

如何将仅 CDB 的表空间配额分配给 CDB 用户?

  • 0
  • 我可以像这样创建一个 CDB 表空间:(全部使用sqlplus / as sysdba:)
-- create unique table space for admin
CREATE TABLESPACE admints DATAFILE '/path/to/admints.dbf' SIZE 20M AUTOEXTEND ON;
  • 我可以创建一个具有默认表空间“系统”的 CDB 用户,因为所有 PDB 都有一个“系统”表空间:
-- create admin user on CDB (the defaut tablespace is "system")
CREATE USER C##admin IDENTIFIED BY P@ssw0rd DEFAULT TABLESPACE system QUOTA UNLIMITED ON system ACCOUNT UNLOCK;
  • 我无法使用默认表空间“admints”创建我的用户:
CREATE USER C##admin IDENTIFIED BY P@ssw0rd DEFAULT TABLESPACE admints QUOTA UNLIMITED ON admints ACCOUNT UNLOCK
*
ERROR at line1:
ORA-65048: error encountered when processing the current DDL statement in
pluggable database ORCLPDB
ORA-00959: tablespace 'ADMINTS' does not exist
  • 而且我不能在我创建的表空间上为我创建的用户提供配额,因为它只在 CDB 上并且不是为每个 PDB 创建的。那是对的吗?
ALTER USER  C##admin quota unlimited on admints unlimited
*
ERROR at line1:
ORA-65048: error encountered when processing the current DDL statement in
pluggable database ORCLPDB
ORA-00959: tablespace 'ADMINTS' does not exist
  • “admits”表空间只为 CDB 创建一次,而不是为每个 PDB 创建一次。
  • 我想在 CDB 的“admits”表空间上创建 C##admin 用户的表,并在其上拥有无限配额。
  • 我希望 C#admin 至少对所有 PDB 具有读取权限,因此我无法卸载它们。
  • 可能吗?
oracle users
  • 1 个回答
  • 253 Views
Martin Hope
SHR
Asked: 2021-01-18 02:39:15 +0800 CST

调用 DBMS_LOGMNR.START_LOGMNR 时出现“用户不存在”

  • 0

我有一个名为“MYADMIN”的 CDB 用户,我正在尝试让它连接到日志挖掘器。

-- enable calling admin username on CDB 
ALTER SESSION set "_ORACLE_SCRIPT"=true
/
-- create unique table space for admin
CREATE TABLESPACE myadmints DATAFILE '/path/to/admints.dbf' SIZE 20M AUTOEXTEND ON
/
-- create admin user on CDB
CREATE USER myadmin IDENTIFIED BY P@ssw0rd DEFAULT TABLESPACE myadmints QUOTA UNLIMITED ON myadmints ACCOUNT UNLOCK
/
-- allow access to all PDBs to the admin user
ALTER USER myadmin SET CONTAINER_DATA=ALL CONTAINER=CURRENT
/ 
-- grant needed permissions
GRANT DBA to myadmin 
GRANT CREATE SESSION TO myadmin 
GRANT CREATE TABLE TO myadmin 
GRANT EXECUTE_CATALOG_ROLE TO myadmin 
GRANT EXECUTE ON DBMS_LOGMNR TO myadmin 
GRANT SELECT ON V_$DATABASE TO myadmin 
GRANT SELECT ON V_$LOGMNR_CONTENTS TO myadmin 
GRANT SELECT ON V_$ARCHIVED_LOG TO myadmin 
GRANT SELECT ON V_$LOG TO myadmin 
GRANT SELECT ON V_$LOGFILE TO myadmin 
GRANT RESOURCE, CONNECT TO myadmin 

我从“v$archived_log”中选择了一行并尝试加载文件。

BEGIN
  DBMS_LOGMNR.ADD_LOGFILE(LogFileName=>'/path/to/archive/ARC000011_1061542581',Options=>DBMS_LOGMNR.new);
  DBMS_LOGMNR.START_LOGMNR(StartScn=>3083464, EndScn=>3388245, Options=>DBMS_LOGMNR.DICT_FROM_ONLINECATALOG+DBMS_LOGMNR.NO_ROW_ID_IN_STMT);
END;

我可以从sys as sysdba用户运行它,但是当我从“myadmin”用户运行它时,我得到:

Error report - 
ORA-01435: user does not exist
ORA-06512: at "SYS.DBMS_LOGMNR", line 72
ORA-06512: at line 3
01435. 00000 - "user does not exist"
*Cause:
*Action:

当我删除它时,错误是关于该START_LOGMNR行的,没有错误。

我缺少哪个特权?

oracle logs
  • 2 个回答
  • 441 Views
Martin Hope
SHR
Asked: 2019-08-06 00:58:55 +0800 CST

如何防止 SELECT 查询死锁?

  • 4

我有一个多会话数据库。很少有会话向数据库插入许多行。并且从该表中选择一个会话。

有时我在 SELECT 查询上遇到死锁,我不明白为什么。

例如:假设这个表

Create Table t1([id] int identity(1,1), [column] varchar(max))

插入是这样的:(我是从“后”触发器开始的)

Begin Transaction
Insert into t1 WITH (TABLOCKX) ([column]) values ('value 1'), ('value 2') ... ('value N')
Commit Transaction

选择是这样的:

Select TOP 10 [id],[column] from t1 order by id

插入锁定意味着保持数据库中会话插入的顺序。

每次我在事务中只锁定一个表。我试图通过插入一个会话并选择第二个会话来恢复死锁,但我失败了。在添加 TABLOCKX 之前,我从未遇到过死锁。

所以我的问题是:

  1. 怎么可能出现僵局?(也许在嵌套事务中?)
  2. MSSQL 中是否有一个选项可以以不阻止选择的方式执行写锁定?
  3. 有什么想法可以防止死锁吗?(但在数据库中保持会话插入序列不变......)
sql-server deadlock
  • 1 个回答
  • 4332 Views
Martin Hope
SHR
Asked: 2019-07-23 00:41:44 +0800 CST

(会话)锁定表的最快方法是什么?

  • 1

我有一些触发器来记录表上的更改Log table.

在插入和删除时,我将行添加到Log table更新中,我添加了两行。

日志表包含标识列,我希望 2 个更新行是连续的(通过 id = identity)

例如:假设下表:

Create table t1 ([name] nvarchar(40) primary key, [value] nvarchar(max))

日志表是:

Create table t1_log 
([log_id] identity(1,1),[log_ts] DateTime default GETDATE(),
  [log_action] varchar(20), log_session_id int default @@SPID,
  [name] nvarchar(40), value nvarchar(max))

我有 3 个触发器来更新日志:

Create trigger t1_ins on t1 After Insert as
begin
    Insert into t1_log([log_action],[name],[value]) select 'insert', [name], [value] from inserted 
end 
Go
create trigger t1_del on t1 After delete as
begin
    Insert into t1_log([log_action],[name],[value]) select 'delete', [name], [value] from deleted 
end 
Go
create trigger t1_upd on t1 After update as
begin
    Insert into t1_log([log_action],[name],[value]) 
       select [log_action], [name], [value] from (
          (select ROW_NUMBER() OVER (ORDER BY (SELECT 0)) As ROW_ID, 'update from' as [log_action], [name], [value] from deleted)
          UNION 
          (select ROW_NUMBER() OVER (ORDER BY (SELECT 0)) As ROW_ID, 'update to' as [log_action], [name], [value] from inserted)
        ) as temp_tbl
     Order By [temp_tbl].ROW_ID, [temp_tbl].[log_action]
end 
Go

在此解决方案中,当我从多个会话进行更新时,有机会同时进行多个更新,这会破坏更新顺序。我可以看到 2 个“更新自”行,然后是两个“更新到”行,我想阻止它。

我能想到的唯一解决方案是使用以下方法将 t1_log 表锁定在更新触发器中:

Select * from t1_log with (TABLOCKX)

但是如果 t1_log 有很多行呢?我猜select *会很慢,每次更新都会返回选中的 *.

所以我正在使用以下内容:

create trigger t1_upd on t1 After update as
begin
    declare @tt
    Begin transaction

    select @tt=1 from t1_log with (TABLOCKX)

    Insert into t1_log([log_action],[name],[value]) 
       select [log_action], [name], [value] from (
          (select ROW_NUMBER() OVER (ORDER BY (SELECT 0)) As ROW_ID, 'update from' as [log_action], [name], [value] from deleted)
          UNION 
          (select ROW_NUMBER() OVER (ORDER BY (SELECT 0)) As ROW_ID, 'update to' as [log_action], [name], [value] from inserted)
        ) as temp_tbl
     Order By [temp_tbl].ROW_ID, [temp_tbl].[log_action]

     Commit trasaction
end 

这效果更好,但我仍然想知道是否有最快的方法来锁定表?

sql-server trigger
  • 1 个回答
  • 81 Views
Martin Hope
SHR
Asked: 2018-10-04 07:51:42 +0800 CST

如何绕过触发器?

  • 1

假设我有下表:

Create Table [dbo].[Test_IP]
(
  [IP] varchar(40),
  [IPType] varchar(6)
)

这张表上有更多的触发器,但它们都是 AFTER 触发器,它是一个包含许多表、视图和存储过程的大型数据库,并且被多个进程使用。

我添加了一个日志表和这样的触发器

Create Table [dbo].[Log_Test_IP]
(
  [Action] varchar(10),
  [IP] varchar(40),
  [IPType] varchar(6)
)
GO
Create Trigger MYTR_Log_Test_IP_INS On [dbo].[Test_IP] After insert AS
BEGIN 
   insert into  [dbo].[Log_Test_IP]([Action],[IP],[IPType]) 
       select 'Insert', [IP], [IPType] from inserted 
END
GO
Create Trigger MYTR_Log_Test_IP_DEL On [dbo].[Test_IP] After delete AS
BEGIN
   insert into  [dbo].[Log_Test_IP]([Action],[IP],[IPType]) 
       select 'Delete', [IP], [IPType] from deleted  
END
GO

现在我进入了有趣的部分:当我直接插入Test_IP表格时,我可以看到插入触发器正在工作,但是当它从应用程序或服务正常工作时(我不知道它做了什么),我不知道在日志表中看到任何插入记录,我只能看到“删除”记录,即使它在开始时是空的。

我的结论是有一些方法可以绕过触发器,数据库中有很多触发器和存储过程,我不知道去哪里找。

所以,我的问题是如何绕过触发器?

sql-server trigger
  • 3 个回答
  • 1519 Views
Martin Hope
SHR
Asked: 2018-08-24 06:39:27 +0800 CST

使用 OBJECT_ID() 的“如果不存在”不适用于视图和触发器。为什么?

  • 6

对于表,我可以如下实现“如果不存在”和“如果存在”:

--if table exists - drop
If OBJECT_ID('A','U') is not null
Drop Table [A]
--if table not exists - Create
If OBJECT_ID('A','U') is null
Create Table A([key] varchar(20), [value] varchar(max))

但它在视图和触发器上的工作方式并不完全相同

我可以:

-- if exists - drop
If OBJECT_ID('VA','V') is not null
Drop view [VA]

但是当我尝试相反时:

-- if not exists - create
If OBJECT_ID('VA','V') is null
Create view [VA] as Select * from [A] 

我收到以下错误:

关键字“view”附近的语法不正确

触发器也是如此。当我做:

-- if not exists - create
If OBJECT_ID('Trigger_A_ins','TR') is null
Create trigger [Trigger_A_ins] On [A] instead of insert As 
   insert into A select * from inserted

我收到错误:

关键字“触发器”附近的语法不正确

但:

-- if exists - drop
If OBJECT_ID('Trigger_A_ins','TR') is not null
Drop Trigger Trigger_A_ins

正在工作中。

我错过了什么吗?

谁能解释表与触发器和视图之间的这种区别?

注意:我使用的是 sql server 2012

sql-server trigger
  • 2 个回答
  • 9656 Views
Martin Hope
SHR
Asked: 2018-07-20 06:14:02 +0800 CST

在 SQL SERVER 中插入多表视图

  • 4

我问这个问题的方式是试图了解我遇到的类似情况,并试图了解插入查看是如何工作的。

我有这些表:

Create table A([id] int primary key not null, value nvarchar(50) NULL)
Create table B([id] int primary key not null, value nvarchar(50) NULL)

我从两个表中创建视图,如下所示:

create View V as (select * from A) UNION ALL (select * from B)

我在 V 视图上有这个触发器:

Create trigger v_trig on V instead of insert AS
  insert into v (id,value) select id,value from inserted

当我尝试插入我的视图时,我收到以下错误:

消息 4436,级别 16,状态 12,过程 v_trig,第 2 行

UNION ALL 视图“db.dbo.V”不可更新,因为未找到分区列

我有一个具有相似视图(带有union all)的数据库,出于某种原因,我可以毫无问题地插入其中,我试图了解原因。

我应该怎么做才能允许插入到诸如视图中?有没有办法决定(不更改触发器)哪个是视图的默认表以插入其中?

sql-server view
  • 1 个回答
  • 4023 Views
Martin Hope
SHR
Asked: 2018-07-19 06:43:22 +0800 CST

这个触发器的目的是什么?

  • 7

我在尝试分析某些数据库时偶然发现了这个触发器:

Create trigger [tbl_Details_Trigger] on [tbl_Details]
    Instead of Insert
    As
      Insert into [tbl_Details]
      Select * from inserted

在我看来,这个触发器并没有做任何特别的事情,即使它被丢弃,结果也是一样的。我对吗?我错过了什么吗?

sql-server trigger
  • 1 个回答
  • 73 Views
Martin Hope
SHR
Asked: 2018-07-17 08:29:38 +0800 CST

SQL Server:未更改数据时捕获插入更新和删除的触发器?

  • 6

我已将一个简单的表添加到一个名为:aaa_log 的数据库中,其中包含列:( id, name, op))

CREATE TABLE aaa_log (
   [id] [int] IDENTITY(1,1) PRIMARY KEY NOT NULL,
   [name] [varchar](50) NOT NULL,
   [op] [varchar](50) NULL)

id列用于保持顺序的方式。

我for insert, update, delete使用以下脚本向数据库中的所有其他表添加了触发器 ():

declare @cmd varchar(max)
declare trigger_create cursor for
select 'Create trigger ['+TABLE_SCHEMA+'].[xxtr_'+TABLE_NAME+'_auto]
  on ['+TABLE_SCHEMA+'].['+TABLE_NAME+'] fro insert,update,delete as
  BEGIN
   declare @op varchar(20)
   if exists(SELECT * from inserted)  and exists(SELECT * from deleted)
   begin
     set @op = ''update''
   end
   if exists(SELECT * from inserted)  and not exists(SELECT * from deleted)
   begin
     set @op = ''insert''
   end
   if not exists(SELECT * from inserted)  and exists(SELECT * from deleted)
   begin
     set @op = ''delete''
   end
   insert into aaa_log([name],[op]) values('+TABLE_SCHEMA+'.'+TABLE_NAME+', @op)
  END
'
from information_schema.tables
where table_type='BASE TABLE' AND table_name <> 'aaa_log'

open trigger_create
fetch next from trigger_create into @sql
while @@FETCH_STATUS =0
BEGIN 
  exec(@sql)
  fetch next from trigger_create into @sql
END
close trigger_create
deallocate trigger_create

没有其他人更新aaa_log表,只有这些触发器,但是当我检查 aaa_log表时,我看到一些op为 NULL 的行。

我能想到的唯一选择是插入和更新都是空的,那么触发器是如何被激活的?

有什么解释吗?

sql-server trigger
  • 2 个回答
  • 7615 Views
Martin Hope
SHR
Asked: 2018-07-06 06:18:25 +0800 CST

TRUNCATE TABLE 可以在 SQL Server 上重载吗?

  • 5

我知道这可能看起来像一个奇怪的问题......

我有一个第 3 方数据库和服务,我想通过每个表上INSERT的DML 触发器来监视它。UPDATEDELETE

但是,唉,我在TRUNCATE TABLE调用时遇到了问题,因为它没有激活DELETE触发器。

我正在寻找一种“超载”的方法,TRUNCATE TABLE就像:

DELETE FROM X; --remove all rows
TRUNCATE TABLE X; --truncate the empty table

或者也许在截断之前激活一些 DDL 触发器,这样我就可以检查哪些行将被截断。

如果还有其他建议,我会很高兴。

这样的事情可能吗?

编辑

我不想禁用truncate我只想监视数据。

它是第 3 方数据库,它被我无法控制的服务使用,所以我无法阻止truncate.

我只是想监控变化,目前无法跟踪由truncate命令引起的记录删除。

谢谢!

sql-server truncate
  • 1 个回答
  • 1313 Views
Martin Hope
SHR
Asked: 2018-03-14 06:01:01 +0800 CST

慢速选择索引。如何改进?

  • 2

我在单个表 (Engine=InnoDB) 上运行 select,不太复杂但有很多行。

第一次选择 id 比较慢,9M 行需要几秒钟,下一次选择要快得多,即使我更改查询也是如此。

我在 Windows 上尝试过 mysql,在 Linux 上尝试过 mariadb。

我像这样运行选择命令:

select `id`,count(*), sum(`counts`) from reference
    where `id`=848
      and `started`<= '2000-01-04 00:00:00'
      and `ended`  >= '2000-01-03 00:00:00';

或者像这样:

 select min(`counts`),max(`counts`) from reference where `id`=848 ;

哪个查询先出现并不重要,第一个查询较慢。

当我在 linux 上的 mariadb 上运行时,结果 id 有时很快,但在 windows 上的 mysql 上它第一次也很慢。这让我觉得也许我错过了什么。

测试、结果和时间测量,可以在下面找到。

谢谢你的帮助!

这是我的数据库:

create database my_test_db default char set utf8 ;
use my_test_db;
create table items (
    `id` int(11) not null auto_increment, 
    `name` varchar(50), 
    `description` varchar(250) default '', 
    primary key (`id`), 
    unique key item_name_unique(`name`)
);
create table reference (
    `id` int(11) not null,
    `started` datetime not null,
    `ended` datetime not null,
    `counts` int(11) not null,
    key fk_item_id_idx (`id`),
    key idx_started (`started`),
    key idx_ended (`ended`),
    constraint fk_item_id foreign key (`id`) references items(`id`)
              on delete no action on update no action
);

图形显示:

MariaDB [my_test_db]> describe items;
+-------------+--------------+------+-----+---------+----------------+
| Field       | Type         | Null | Key | Default | Extra          |
+-------------+--------------+------+-----+---------+----------------+
| id          | int(11)      | NO   | PRI | NULL    | auto_increment |
| name        | varchar(50)  | YES  | UNI | NULL    |                |
| description | varchar(250) | YES  |     |         |                |
+-------------+--------------+------+-----+---------+----------------+
3 rows in set (0.00 sec)

MariaDB [my_test_db]> describe reference;
+---------+----------+------+-----+---------+-------+
| Field   | Type     | Null | Key | Default | Extra |
+---------+----------+------+-----+---------+-------+
| id      | int(11)  | NO   | MUL | NULL    |       |
| started | datetime | NO   | MUL | NULL    |       |
| ended   | datetime | NO   | MUL | NULL    |       |
| counts  | int(11)  | NO   |     | NULL    |       |
+---------+----------+------+-----+---------+-------+
4 rows in set (0.00 sec)

我使用以下程序创建了 2 个 infile:

#include <iostream>
#include <fstream>
#include <string>
#include <ctime>
#include <sstream>
#include <cstring>
#include <random>
#define TM_BUF_SIZE 32

#ifndef WIN32
#define localtime_s(PTM,PTIME_T) localtime_r(PTIME_T,PTM)
#endif
int main(int argc, char** argv)
{
    int id_max, count_iterations, time_frame;
    if(argc!=4)
    {
        std::cerr<<"Missing Arguments!!"<<std::endl;
        std::cerr<<"Usage: DataGen item_count time_iteration time_frame"<<std::endl;
        return -1;
    }
    id_max  = (int)strtol(argv[1],nullptr,0);
    count_iterations = (int)strtol(argv[2],nullptr,0);
    time_frame = (int)strtol(argv[3],nullptr,0);

    std::random_device r;
    std::default_random_engine re(r());
    std::uniform_int_distribution<int> uni_dist(0, 15);
    std::tm temp, tmStart,tmEnd;
    char bufStart[TM_BUF_SIZE], bufEnd[TM_BUF_SIZE];
    std::memset(&temp, 0, sizeof(tm));

    std::ofstream fitems("items.dat");
    for (int id = 1; id <= id_max; id++)
    {
        fitems << id << "\tid-" << id << "\titem.number." << id << std::endl;
    }
    temp.tm_year = 100;
    temp.tm_mday = 1;
    time_t ts_start = mktime(&temp);
    time_t ts_end;
    int iteration_left = count_iterations;
    std::ofstream frefs("references.dat");
    while(iteration_left--)
    {
        ts_end = ts_start + time_frame;
        localtime_s(&tmStart, &ts_start);
        localtime_s(&tmEnd, &ts_end);
        std::strftime(bufStart, TM_BUF_SIZE, "%Y-%m-%d %H:%M:%S.0", &tmStart);
        std::strftime(bufEnd, TM_BUF_SIZE, "%Y-%m-%d %H:%M:%S.0", &tmEnd);
        for (int id = 1; id <= id_max; id++)
        {
            int count = uni_dist(re);
            frefs << id << "\t" << bufStart << "\t" << bufEnd << "\t"<<count<< std::endl;
        }
        ts_start = ts_end;
        if(iteration_left && 0 == iteration_left % 100)
        {
            std::cout<<iteration_left<<" iterations left"<<std::endl;
        }
    }
    std::cout<<"Done!"<<std::endl;
        return 0;
}

使用以下命令在 Linux 上编译它:

g++ -std=c++0x dataGen.cpp -o DataGen

像这样运行 DataGen 程序:

DataGen 3000 3000 60

该程序创建 2 个文件:“items.dat”和“references.dat”

将数据加载到数据库中:

use my_test_db;
load data infile '/root/items.dat' into table items;
load data infile '/root/references.dat' into table reference;

所以我用很多行填充了表格:项目有 3K 行,引用有 900 万行。

现在我在reference桌子上运行选择:

这是结果:

#first time for this id: 
MariaDB [my_test_db]> select `id`,count(*), sum(`counts`) from reference where `id`=848 and `started`<= '2000-01-03 00:00:00' and `ended`>='2000-01-02 00:00:00';
+------+----------+---------------+
| id   | count(*) | sum(`counts`) |
+------+----------+---------------+
|  848 |     1442 |         10640 |
+------+----------+---------------+
1 row in set (3.31 sec)

#next query for same id change time filters:
MariaDB [my_test_db]> select `id`,count(*), sum(`counts`) from reference where `id`=848 and `started`<= '2000-01-04 00:00:00' and `ended`>='2000-01-03 00:00:00';
+------+----------+---------------+
| id   | count(*) | sum(`counts`) |
+------+----------+---------------+
|  848 |      121 |           944 |
+------+----------+---------------+
1 row in set (0.03 sec)

#next query for same id change time filters again:
MariaDB [my_test_db]> select `id`,count(*), sum(`counts`) from reference
    where `id`=848
      and `started`<= '2000-01-02 00:00:00'
      and `ended`  >= '2000-01-01 00:00:00';
+------+----------+---------------+
| id   | count(*) | sum(`counts`) |
+------+----------+---------------+
|  848 |     1441 |         10848 |
+------+----------+---------------+
1 row in set (0.06 sec)

-- 只改变 id:

MariaDB [my_test_db]> select `id`,count(*), sum(`counts`) from reference
    where `id`=1848
      and `started`<= '2000-01-02 00:00:00'
      and `ended`  >= '2000-01-01 00:00:00';
+------+----------+---------------+
| id   | count(*) | sum(`counts`) |
+------+----------+---------------+
| 1848 |     1441 |         10576 |
+------+----------+---------------+
1 row in set (2.63 sec)

#use same id change time filters: 
MariaDB [my_test_db]> select `id`,count(*), sum(`counts`) from reference
    where `id`=1848
      and `started`<= '2000-01-02 12:00:00'
      and `ended`  >= '2000-01-01 12:00:00';
+------+----------+---------------+
| id   | count(*) | sum(`counts`) |
+------+----------+---------------+
| 1848 |     1442 |         10780 |
+------+----------+---------------+
1 row in set (0.03 sec)

#use consequent id is also fast:
MariaDB [my_test_db]> select `id`,count(*), sum(`counts`) from reference
    where `id`=1849
      and `started`<= '2000-01-02 12:00:00'
      and `ended`  >= '2000-01-01 12:00:00';
+------+----------+---------------+
| id   | count(*) | sum(`counts`) |
+------+----------+---------------+
| 1849 |     1442 |         11001 |
+------+----------+---------------+
1 row in set (0.11 sec)

-- 其他查询 - 相同的 id - 快速

MariaDB [my_test_db]> select min(counts),max(counts) from reference where `id`=1849 ;
+-------------+-------------+
| min(counts) | max(counts) |
+-------------+-------------+
|           0 |          15 |
+-------------+-------------+
1 row in set (0.03 sec)

#again it is slow for other id
MariaDB [my_test_db]> select min(counts),max(counts) from reference where `id`=1800 ;
+-------------+-------------+
| min(counts) | max(counts) |
+-------------+-------------+
|           0 |          15 |
+-------------+-------------+
1 row in set (2.36 sec)

-- 描述查询:

MariaDB [my_test_db]> describe select `id`,count(*), sum(`counts`) from reference where `id`=1849 and `started`<= '2000-01-02 12:00:00' and `ended`>='2000-01-01 12:00:00';
+------+-------------+-----------+------+--------------------------------------+----------------+---------+-------+------+-------------+
| id   | select_type | table     | type | possible_keys                        | key            | key_len | ref   | rows | Extra       |
+------+-------------+-----------+------+--------------------------------------+----------------+---------+-------+------+-------------+
|    1 | SIMPLE      | reference | ref  | fk_item_id_idx,idx_started,idx_ended | fk_item_id_idx | 4       | const | 2999 | Using where |
+------+-------------+-----------+------+--------------------------------------+----------------+---------+-------+------+-------------+
1 row in set (0.00 sec)
mysql performance
  • 2 个回答
  • 92 Views
Martin Hope
SHR
Asked: 2016-06-01 10:14:54 +0800 CST

如何在命令行中不使用密码从 sqlplus 连接到 oracle 12c

  • 6

这似乎是一个奇怪的问题:

如果我有用户demo使用密码 Pass1234 调用

连接到 oracle 11g 时,我可以sqlplus在命令行中使用密码运行:在 DB 主机上:

C:\> sqlplus demo/Pass1234 

或远程机器

C:\> sqlplus demo/Pass1234@<ip>:1521/orcl 

我可以这样做而无需在命令行中提供密码并像这样公开它:

C:\> sqlplus demo

然后我得到:

SQL*Plus: Release 11.1.0.7.0 bla bla bla
Copyright (c) bla bla bla
Enter password:  

然后我可以手动输入密码。

连接到 Oracle 12c 时,我可以从任何机器上这样连接(使用密码):

C:\> sqlplus demo/Pass1234@<ip>:1521/pdborcl

我的问题是:如何在不tnsnames.ora编辑且不在命令行中写入密码的情况下连接到 Oracle 12c?

我还没有弄清楚是否可以从数据库主机上的 sqlplus 连接到 Oracle 12c,而无需提供 IP 或本地主机,也无需编辑 tnsnames.ora,就像我在 Oracle 11g 中可以做的那样?

更重要的是,我可以在不使用完全连接的情况下从远程机器(甚至是 Oracle 11g)这样做吗?

(意味着在命令行和密码中手动给用户和数据库连接,不像/NOLOG在命令中使用然后SQL> connect demo/Pass1234@<ip>:1521/pdborcl:)

谢谢!

oracle oracle-12c
  • 1 个回答
  • 55512 Views
Martin Hope
SHR
Asked: 2016-05-30 10:28:27 +0800 CST

oracle 12c如何配置sql developer?

  • 0

我正在尝试将 sql developer 连接到全新安装的 oracle:

我可以通过 sqlplus 连接到 sys 用户 当我选择连接类型“Local\Bequeath”时,我可以通过 sql developer 连接到 sys 用户。但我猜它没有连接到 PDB,只是连接到容器。

我使用以下命令创建了名为 demo 的用户:

alter session set container=pdborcl;
create user demo identified by password QUOTA unlimited on users account unlock;  

我正在尝试使用用户名和密码连接到数据库,就像我在 oracle 11g 中所做的那样。To user: sys as sysdba, 和新创建的用户demo。

我不关心 pdb,但据我所知,oracle 12c 中没有其他选项......

我尝试在 tnsnames.ora 中设置名为 PDBORCL 的条目,我也尝试使用服务名称 PDBORCL。正如我在互联网上的一些手册中看到的那样,但它对我不起作用。

我收到以下错误:

ORA-12514: TNS:listener does not currently know of service requested in connect descriptor
ORA-12505, TNS:listener does not currently know of SID given in connect descriptor

同样,我使用了默认安装的 oracle 12c,除了设置密码和选择服务器类型,我没有做太多更改。

我正在寻找的是一个简单的“待办事项列表”,用于在 sql 开发人员中配置连接,甚至从远程机器连接到 oracle 12c DB。

顺便说一句:(如果有的话)

  1. 我有 2 个版本的 sql developer:4.1.2.20,以及来自安装的原始版本:3.2.20.10。

  2. 我目前正在使用 Windows Server 2008 操作系统。

谢谢,

编辑:

询问:

SQL> select name, open_mode from v$pdbs where name='PDBORCL';

结果:

NAME      OPEN_MODE
--------  ----------
PDBORCL   READ WRITE

命令行:

lsnrctl service

结果:

LSNRCTL for 64-bit Windows: Version 12.1.0.2.0 - Production on 30-MAY-2016 13:26:20

Copyright (c) 1991, 2014, Oracle.  All rights reserved.
Connecting to (DESCRIPTION=(ADDRESS=PROTOCOL=TCP)(HOST=192.168.19.58)(PORT=1521)))
Services Summary...
Service "CLRExtProc" has 1 instance(s).
  Instance "CLRExtProc", status UNKNOWN, has 1 handler(s) for this service...
    Handler(s):
      "DEDICATED" established:0 refused:0
        LOCAL SERVER
The command completed successfully

我尝试使用服务名称 CLRExtProc。现在我有以下错误:

Status: Failure -Test failed: The Network Adapter could not established the connection

listener.ora 文件:

# listener.ora Network Configuration File: C:\app\Administrator\product\12.1.0\dbhome_1\network\admin\listener.ora
# Generated by Oracle configuration tools.

SID_LIST_LISTENER =
  (SID_LIST =
    (SID_DESC =
      (SID_NAME = CLRExtProc)
      (ORACLE_HOME = C:\app\Administrator\product\12.1.0\dbhome_1)
      (PROGRAM = extproc)
      (ENVS = "EXTPROC_DLLS=ONLY:C:\app\Administrator\product\12.1.0\dbhome_1\bin\oraclr12.dll")
    )
  )

LISTENER =
  (DESCRIPTION_LIST =
    (DESCRIPTION =
      (ADDRESS = (PROTOCOL = TCP)(HOST = 192.168.19.58)(PORT = 1521))
      (ADDRESS = (PROTOCOL = IPC)(KEY = EXTPROC1521))
    )
  )

tnsnames.ora

# tnsnames.ora Network Configuration File: C:\app\Administrator\product\12.1.0\dbhome_1\network\admin\tnsnames.ora
# Generated by Oracle configuration tools.

ORACLR_CONNECTION_DATA =
  (DESCRIPTION =
    (ADDRESS_LIST =
      (ADDRESS = (PROTOCOL = IPC)(KEY = EXTPROC1521))
    )
    (CONNECT_DATA =
      (SID = CLRExtProc)
      (PRESENTATION = RO)
    )
  )

ORCL =
  (DESCRIPTION =
    (ADDRESS = (PROTOCOL = TCP)(HOST = 192.168.19.58)(PORT = 1521))
    (CONNECT_DATA =
      (SERVER = DEDICATED)
      (SERVICE_NAME = orcl)
    )
  )
#this is my addition I guess it not written properly
PDBORCL =
  (DESCRIPTION =
    (ADDRESS = (PROTOCOL = TCP)(HOST = 192.168.19.58)(PORT = 1521))
    (CONNECT_DATA =
      (SERVER = DEDICATED)
      (SERVICE_NAME = pdborcl)
    )
  )

编辑:

命令:lsnrctl 状态监听器

LSNRCTL for 64-bit Windows: Version 12.1.0.2.0 - Production on 30-MAY-2016 14:22
:08

Copyright (c) 1991, 2014, Oracle.  All rights reserved.

Connecting to (DESCRIPTION=(ADDRESS=(PROTOCOL=TCP)(HOST=192.168.19.58)(PORT=1521)))
STATUS of the LISTENER
------------------------
Alias                     LISTENER
Version                   TNSLSNR for 64-bit Windows: Version 12.1.0.2.0 - Produ
ction
Start Date                29-MAY-2016 17:33:36
Uptime                    0 days 20 hr. 48 min. 36 sec
Trace Level               off
Security                  ON: Local OS Authentication
SNMP                      OFF
Listener Parameter File   C:\app\Administrator\product\12.1.0\dbhome_1\network\admin\listener.ora
Listener Log File         C:\app\Administrator\diag\tnslsnr\DevOraRX\listener\alert\log.xml
Listening Endpoints Summary...
  (DESCRIPTION=(ADDRESS=(PROTOCOL=tcp)(HOST=192.168.19.58)(PORT=1521)))
  (DESCRIPTION=(ADDRESS=(PROTOCOL=ipc)(PIPENAME=\\.\pipe\EXTPROC1521ipc)))
Services Summary...
Service "CLRExtProc" has 1 instance(s).
  Instance "CLRExtProc", status UNKNOWN, has 1 handler(s) for this service...
The command completed successfully

编辑:完整解决方案

这是它的工作原理:

  1. 安装 oracle 12c 的新副本(我进行了桌面安装)。
  2. 的变化listener.ora:
    • 在 SID_LIST_LISTENER 我添加了以下部分:(SID_DESC =(SID_NAME = pdborcl)(ORACLE_HOME = C:\app\oracle\product\12.1.0\dbhome_1))
    • 在 LISTENER 而不是HOST=127.0.0.1,我将其设置为 HOST=0.0.0.0
  3. 重启oracle监听服务。

现在在 sql 开发人员中:

以 sys 用户身份登录:

Username: sys
Password: ****
Connection type: Basic, Role: SYSDBA
Hostname: localhost
Port: 1521
SID: orcl

现在检查 pdb 是否已启动:

SQL> select name, open_mode from v$pdbs where name='PDBORCL';

如果它处于挂载模式,那么,

SQL> alter pluggable database pdborcl open;

创建用户+授予权限:

alter session set container=pdborcl;
create user demo identified by password QUOTA unlimited on users account unlock;
grant create session to demo;
grant create table to demo;

在 sql developer 中以演示用户身份连接:

Username: demo
Password: ****
Connection type: Basic, Role: default
Hostname: localhost
Port: 1521
Service name: pdborcl

而且,再次感谢 JSapkota。

现在我正在尝试找出 pdbs 的 sqlplus 和即时客户端连接字符串...

oracle oracle-12c
  • 1 个回答
  • 9323 Views
Martin Hope
SHR
Asked: 2016-05-10 10:52:16 +0800 CST

Oracle log miner 权限:哪个权限授权用户使用 logMiner 作为计划的 JOB?

  • 0

我正在尝试将用户配置为定期从计划的作业中运行 logMiner。

我的用户名为 admin 我已授予 admin 用户以下权限:

 grant select on v_$database to admin
 grant execute on DBMS_LOGMNR to admin
 grant select on v_$logmnr_contents to admin

但是,当我尝试从 v$logmnr_contents 中进行选择时,我得到 - ORA-01031 权限不足。

为了测试,我正在运行以下脚本:

create or replace procedure Test_LogMiner AS
first_scn NUMBER(8);
last_scn NUMBER(8);
log_count NUMBER(8); 
BEGIN
  Select current_scn into last_scn from V$DATABASE;
  --Select max(mined_scn) into first_scn from my_table;
  first_scn := last_scn-100;
  DBMS_LOGMNR.START_LOGMNR(startScn=>first_scn, endScn=>last_scn,
     options=>DBMS_LOGMNR.DICT_FROM_ONLINE_CATALOG+DBMS_LOGMNR.CONTINUOUS_MINE);
  WRITE_LOG('LOG_DIR', 'After START_LOGMNR');
  -- get error on this select - ORA-01031 insufficient privileges-- 
  select count(*) into log_count from v$logmnr_contents;
  WRITE_LOG('LOG_DIR', 'select count(*)='||log_count);
  DBMS_LOGMNR.END_LOGMNR();
  WRITE_LOG('LOG_DIR', 'After END_LOGMNR');
EXCEPTION WHEN OTHERS THEN
  WRITE_LOG('LOG_DIR', 'Error: '||SQLERRM);
END; 

我可以运行相同的场景,意味着启动 logMiner 并从 v$logmnr_contents 中选择,当它不作为过程运行时,从 SQL 开发人员作为管理员用户(意味着:删除创建过程并将其余代码放入另一个begin/ end)

当我从 'sys as dba' 用户运行 proc 时,它也会运行。

我的问题是:我还应该在程序工作的方式上授予什么?

oracle log
  • 1 个回答
  • 3729 Views
Martin Hope
SHR
Asked: 2016-03-22 05:55:19 +0800 CST

触发器可以捕获由 select for update + lob.write() 完成的更新吗?

  • 0

这可能是一个愚蠢的问题,但我没有找到明确的答案......

我想在我的 oracle 数据库上捕获任何更新,所以我想使用触发器,但是在使用SELECT FOR UPDATE、 thenlob.write(...)和时触发器没有被激活COMMIT。(我已经使用 occi 使用 c++ 对其进行了测试)

有没有办法在更新(或任何其他)触发器中捕获它?

我发现很难相信有任何触发器都无法监控的命令......

如果这很重要,我使用的是 11g 版。

谢谢!

编辑

触发器例如:

CREATE OR REPLACE TRIGGER TR1
BEFORE DELETE OR INSERT OR UPDATE ON TEST_CLOB 
FOR EACH ROW
BEGIN
IF DELETING THEN
  WRITE_LOG('LOG_DIR','deleting:'||:old.filename);
  goto endproc;
END IF;
IF INSERTING THEN
  WRITE_LOG('LOG_DIR','inserting:'||:new.filename);
  goto endproc;
END IF;
IF UPDATING THEN
  WRITE_LOG('LOG_DIR','updating:'||:old.filename||'-'||:new.filename);
  goto endproc;
END IF;
WRITE_LOG('LOG_DIR','other:'||:old.filename||'-'||:new.filename);
<<endproc>>
NULL;
END;

现在在我的日志中我看到了插入和删除,但我从未看到任何更新。

oracle trigger
  • 2 个回答
  • 840 Views

Sidebar

Stats

  • 问题 205573
  • 回答 270741
  • 最佳答案 135370
  • 用户 68524
  • 热门
  • 回答
  • Marko Smith

    连接到 PostgreSQL 服务器:致命:主机没有 pg_hba.conf 条目

    • 12 个回答
  • Marko Smith

    如何让sqlplus的输出出现在一行中?

    • 3 个回答
  • Marko Smith

    选择具有最大日期或最晚日期的日期

    • 3 个回答
  • Marko Smith

    如何列出 PostgreSQL 中的所有模式?

    • 4 个回答
  • Marko Smith

    列出指定表的所有列

    • 5 个回答
  • Marko Smith

    如何在不修改我自己的 tnsnames.ora 的情况下使用 sqlplus 连接到位于另一台主机上的 Oracle 数据库

    • 4 个回答
  • Marko Smith

    你如何mysqldump特定的表?

    • 4 个回答
  • Marko Smith

    使用 psql 列出数据库权限

    • 10 个回答
  • Marko Smith

    如何从 PostgreSQL 中的选择查询中将值插入表中?

    • 4 个回答
  • Marko Smith

    如何使用 psql 列出所有数据库和表?

    • 7 个回答
  • Martin Hope
    Jin 连接到 PostgreSQL 服务器:致命:主机没有 pg_hba.conf 条目 2014-12-02 02:54:58 +0800 CST
  • Martin Hope
    Stéphane 如何列出 PostgreSQL 中的所有模式? 2013-04-16 11:19:16 +0800 CST
  • Martin Hope
    Mike Walsh 为什么事务日志不断增长或空间不足? 2012-12-05 18:11:22 +0800 CST
  • Martin Hope
    Stephane Rolland 列出指定表的所有列 2012-08-14 04:44:44 +0800 CST
  • Martin Hope
    haxney MySQL 能否合理地对数十亿行执行查询? 2012-07-03 11:36:13 +0800 CST
  • Martin Hope
    qazwsx 如何监控大型 .sql 文件的导入进度? 2012-05-03 08:54:41 +0800 CST
  • Martin Hope
    markdorison 你如何mysqldump特定的表? 2011-12-17 12:39:37 +0800 CST
  • Martin Hope
    Jonas 如何使用 psql 对 SQL 查询进行计时? 2011-06-04 02:22:54 +0800 CST
  • Martin Hope
    Jonas 如何从 PostgreSQL 中的选择查询中将值插入表中? 2011-05-28 00:33:05 +0800 CST
  • Martin Hope
    Jonas 如何使用 psql 列出所有数据库和表? 2011-02-18 00:45:49 +0800 CST

热门标签

sql-server mysql postgresql sql-server-2014 sql-server-2016 oracle sql-server-2008 database-design query-performance sql-server-2017

Explore

  • 主页
  • 问题
    • 最新
    • 热门
  • 标签
  • 帮助

Footer

AskOverflow.Dev

关于我们

  • 关于我们
  • 联系我们

Legal Stuff

  • Privacy Policy

Language

  • Pt
  • Server
  • Unix

© 2023 AskOverflow.DEV All Rights Reserve