[英]Teradata string/text search
I have 2 tables in teradata.我在teradata中有2个表。 TableA and TableB表A和表B
TableA has 2 columns as shown below表A有2列,如下所示
Table A表A
Column 1 --> Databasename第 1 列 --> 数据库名称
Column 2 --> Tablename第 2 列 --> 表名
TableB has multiple columns including a text column. TableB 有多个列,包括一个文本列。
I want to search databasename.tablename from Table A in Table B's Text column.我想从表 B 的文本列中的表 A 中搜索 databasename.tablename。 The like operator cannot be used as there are around 2000 distinct table names in Table A. I have tried position join to do this as shown below but the query is extremely long running with high PJI and i had to manually abort it不能使用 like 运算符,因为表 A 中大约有 2000 个不同的表名。我尝试使用位置连接来执行此操作,如下所示,但查询运行时间非常长,PJI 高,我不得不手动中止它
select distinct a.Tablename ,b.text
from TableA a
inner join TableB b
on position(Trim(b.Text) in Trim('a.Databasename.'||a.tablename))>0
where b.theDate between add_months(date,-6) and date
UNION ALL
select distinct a.Tablename ,b.text
from TableA a
inner join TableB b
on position (Trim('a.Databasename.'||a.tablename) in Trim(b.Text)) >0
where b.theDate between add_months(date,-6) and date;
Is there an alternate way to do the above string search.是否有其他方法可以进行上述字符串搜索。 Kindly share the SQL.请分享SQL。
Thanks谢谢
REGEXP_SIMILAR: REGEXP_SIMILAR:
One option is to use REGEXP_SIMILAR()
which will be more precise than LIKE
.一种选择是使用REGEXP_SIMILAR()
,它比LIKE
更精确。 I'm not sure if will be quicker, but it's worth a shot:我不确定是否会更快,但值得一试:
CREATE MULTISET VOLATILE TABLE TABLEA
(databasename varchar(30), tablename varchar(30))
PRIMARY INDEX (databasename, tablename) ON COMMIT PRESERVE ROWS;
INSERT INTO TABLEA VALUES ('dba','tbla');
INSERT INTO TABLEA VALUES ('dba','tblb');
INSERT INTO TABLEA VALUES ('dbb','tbla');
CREATE MULTISET VOLATILE TABLE TABLEB
(id int, sqlqry VARCHAR(5000))
ON COMMIT PRESERVE ROWS;
INSERT INTO TABLEB VALUES (1, 'SELECT * FROM dba.tbla;');
INSERT INTO TABLEB VALUES (2, 'SELECT smoecolumn FROM dba.tblb INNER JOIN dba.tbla ON foo = bar WHERe 1=1;');
INSERT INTO TABLEB VALUES (3, 'SELECT * FROM dbb.tbla WHERE foo=bar');
SELECT *
FROM TABLEA
INNER JOIN TABLEB
ON REGEXP_SIMILAR(TABLEB.sqlqry, '^.*' || TABLEA.databasename || '\.' || TABLEA.tablename || '.*$', 'i') = 1;
+-----+------+---+-----------------------------------------------------------------------------+
| dbb | tbla | 3 | SELECT * FROM dbb.tbla WHERE foo=bar |
| dba | tbla | 2 | SELECT smoecolumn FROM dba.tblb INNER JOIN dba.tbla ON foo = bar WHERe 1=1; |
| dba | tbla | 1 | SELECT * FROM dba.tbla; |
| dba | tblb | 2 | SELECT smoecolumn FROM dba.tblb INNER JOIN dba.tbla ON foo = bar WHERe 1=1; |
+-----+------+---+-----------------------------------------------------------------------------+
STRTOK_SPLIT_TO_TABLE: STRTOK_SPLIT_TO_TABLE:
Here's where I was aiming with that strtok_split_to_tables
comment.这就是我用strtok_split_to_tables
评论瞄准的地方。 Basically you split your sql in TABLEB
into words (splitting by space and ;
characters).基本上,您将TABLEB
的 sql TABLEB
为单词(按空格和;
字符拆分)。 That will generate a row for every word.这将为每个单词生成一行。
From that list you just keep words that contain a period in them (like databasename.tablename
).从该列表中,您只需保留包含句点的单词(例如databasename.tablename
)。
Then you can do your join on that between TABLEB and TABLEA:然后你可以在 TABLEB 和 TABLEA 之间进行连接:
CREATE MULTISET VOLATILE TABLE TABLEA
(databasename varchar(30), tablename varchar(30))
PRIMARY INDEX (databasename, tablename) ON COMMIT PRESERVE ROWS;
INSERT INTO TABLEA VALUES ('dba','tbla');
INSERT INTO TABLEA VALUES ('dba','tblb');
INSERT INTO TABLEA VALUES ('dbb','tbla');
CREATE MULTISET VOLATILE TABLE TABLEB
(id int, sqlqry VARCHAR(5000))
ON COMMIT PRESERVE ROWS;
INSERT INTO TABLEB VALUES (1, 'SELECT * FROM dba.tbla;');
INSERT INTO TABLEB VALUES (2, 'SELECT smoecolumn FROM dba.tblb INNER JOIN dba.tbla ON foo = bar WHERe 1=1;');
INSERT INTO TABLEB VALUES (3, 'SELECT * FROM dbb.tbla WHERE foo=bar');
WITH sqlwords AS
(
SELECT tablebid, sqlwordnum, sqlword
FROM TABLE (strtok_split_to_table(TABLEB.id, TABLEB.sqlqry, ' ;')
RETURNS (tablebid integer, sqlwordnum integer, sqlword varchar(100)character set unicode) ) as sqlwordsplitter
WHERE sqlwordsplitter.sqlword like '%.%'
)
SELECT TABLEA.*, TABLEB.*
FROM TABLEA
INNER JOIN sqlwords
ON TABLEA.databasename = strtok(sqlwords.sqlword, '.', 1)
AND TABLEA.tablename = strtok(sqlwords.sqlword, '.', 2)
INNER JOIN TABLEB
ON sqlwords.tablebid = TABLEB.id;
+-----+------+---+-----------------------------------------------------------------------------+
| dbb | tbla | 3 | SELECT * FROM dbb.tbla WHERE foo=bar |
| dba | tbla | 2 | SELECT smoecolumn FROM dba.tblb INNER JOIN dba.tbla ON foo = bar WHERe 1=1; |
| dba | tbla | 1 | SELECT * FROM dba.tbla; |
| dba | tblb | 2 | SELECT smoecolumn FROM dba.tblb INNER JOIN dba.tbla ON foo = bar WHERe 1=1; |
+-----+------+---+-----------------------------------------------------------------------------+
This isn't going to be super fast since we have to do word splitting, but it will definitely get the job done.这不会很快,因为我们必须进行分词,但它肯定会完成工作。
If it's for extracting a single tablename from CREATE TABLE AS you can apply Regular Expressions for table/DB name:如果是为了从 CREATE TABLE AS 中提取单个表名,您可以对表/数据库名应用正则表达式:
RegExp_Substr(SqlTextInfo, 'AS\s+?(.*?[.])?\K.+?\s+?(?=WITH\s)',1,1,'i') AS TableName
RegExp_Substr(SqlTextInfo, 'AS\s+?\K.*?(?=[.](.+?\s+)?WITH\s)',1,1,'i') AS DatabaseName
If the database name is missing you can COALESCE QryLogV.DefaultDatabase
如果缺少数据库名称,您可以 COALESCE QryLogV.DefaultDatabase
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.